added option to show the probabilities of results

This commit is contained in:
krangelov
2019-09-20 08:09:54 +02:00
parent a0cfe09e09
commit 084b345663
2 changed files with 16 additions and 7 deletions

View File

@@ -43,7 +43,7 @@ newtype CommandOutput = Piped (CommandArguments,String) ---- errors, etc
-- ** Converting command output -- ** Converting command output
fromStrings ss = Piped (Strings ss, unlines ss) fromStrings ss = Piped (Strings ss, unlines ss)
fromExprs es = Piped (Exprs es,unlines (map (showExpr [] . fst) es)) fromExprs show_p es = Piped (Exprs es,unlines (map (\(e,p) -> (if show_p then (++) ("["++show p++"] ") else id) (showExpr [] e)) es))
fromString s = Piped (Strings [s], s) fromString s = Piped (Strings [s], s)
pipeWithMessage es msg = Piped (Exprs es,msg) pipeWithMessage es msg = Piped (Exprs es,msg)
pipeMessage msg = Piped (Exprs [],msg) pipeMessage msg = Piped (Exprs [],msg)

View File

@@ -172,6 +172,9 @@ pgfCommands = Map.fromList [
"all metavariables in the tree. The generation can be biased by probabilities", "all metavariables in the tree. The generation can be biased by probabilities",
"if the grammar was compiled with option -probs" "if the grammar was compiled with option -probs"
], ],
options = [
("show_probs", "show the probability of each result")
],
flags = [ flags = [
("cat","generation category"), ("cat","generation category"),
("lang","uses only functions that have linearizations in all these languages"), ("lang","uses only functions that have linearizations in all these languages"),
@@ -182,7 +185,7 @@ pgfCommands = Map.fromList [
let ts = case mexp (toExprs arg) of let ts = case mexp (toExprs arg) of
Just ex -> generateRandomFrom gen pgf ex Just ex -> generateRandomFrom gen pgf ex
Nothing -> generateRandom gen pgf (optType pgf opts) Nothing -> generateRandom gen pgf (optType pgf opts)
returnFromExprs $ take (optNum opts) ts returnFromExprs (isOpt "show_probs" opts) $ take (optNum opts) ts
}), }),
("gt", emptyCommandInfo { ("gt", emptyCommandInfo {
@@ -193,6 +196,9 @@ pgfCommands = Map.fromList [
"If a Tree argument is given, the command completes the Tree with values", "If a Tree argument is given, the command completes the Tree with values",
"to all metavariables in the tree." "to all metavariables in the tree."
], ],
options = [
("show_probs", "show the probability of each result")
],
flags = [ flags = [
("cat","the generation category"), ("cat","the generation category"),
("lang","excludes functions that have no linearization in this language"), ("lang","excludes functions that have no linearization in this language"),
@@ -207,7 +213,7 @@ pgfCommands = Map.fromList [
let es = case mexp (toExprs arg) of let es = case mexp (toExprs arg) of
Just ex -> generateAllFrom pgf ex Just ex -> generateAllFrom pgf ex
Nothing -> generateAll pgf (optType pgf opts) Nothing -> generateAll pgf (optType pgf opts)
returnFromExprs $ takeOptNum opts es returnFromExprs (isOpt "show_probs" opts) $ takeOptNum opts es
}), }),
("i", emptyCommandInfo { ("i", emptyCommandInfo {
@@ -329,6 +335,9 @@ pgfCommands = Map.fromList [
[(s,parse concr (optType pgf opts) s) | [(s,parse concr (optType pgf opts) s) |
concr <- optLangs pgf opts] concr <- optLangs pgf opts]
| s <- toStrings ts]), | s <- toStrings ts]),
options = [
("show_probs", "show the probability of each result")
],
flags = [ flags = [
("cat","target category of parsing"), ("cat","target category of parsing"),
("lang","the languages of parsing (comma-separated, no spaces)"), ("lang","the languages of parsing (comma-separated, no spaces)"),
@@ -391,7 +400,7 @@ pgfCommands = Map.fromList [
mkEx "pt -compute (plus one two) -- compute value" mkEx "pt -compute (plus one two) -- compute value"
], ],
exec = needPGF $ \opts arg pgf -> exec = needPGF $ \opts arg pgf ->
returnFromExprs . takeOptNum opts . map (flip (,) 0) . treeOps pgf opts $ toExprs arg, returnFromExprs False . takeOptNum opts . map (flip (,) 0) . treeOps pgf opts $ toExprs arg,
options = treeOpOptions undefined{-pgf-}, options = treeOpOptions undefined{-pgf-},
flags = [("number","take at most this many trees")] ++ treeOpFlags undefined{-pgf-} flags = [("number","take at most this many trees")] ++ treeOpFlags undefined{-pgf-}
}), }),
@@ -690,7 +699,7 @@ pgfCommands = Map.fromList [
fromParse1 opts (s,po) = fromParse1 opts (s,po) =
case po of case po of
ParseOk ts -> fromExprs (takeOptNum opts ts) ParseOk ts -> fromExprs (isOpt "show_probs" opts) (takeOptNum opts ts)
ParseFailed i t -> pipeMessage $ "The parser failed at token " ParseFailed i t -> pipeMessage $ "The parser failed at token "
++ show i ++": " ++ show i ++": "
++ show t ++ show t
@@ -768,11 +777,11 @@ pgfCommands = Map.fromList [
optNumInf opts = valIntOpts "number" 1000000000 opts ---- 10^9 optNumInf opts = valIntOpts "number" 1000000000 opts ---- 10^9
takeOptNum opts = take (optNumInf opts) takeOptNum opts = take (optNumInf opts)
returnFromExprs es = returnFromExprs show_p es =
return $ return $
case es of case es of
[] -> pipeMessage "no trees found" [] -> pipeMessage "no trees found"
_ -> fromExprs es _ -> fromExprs show_p es
prGrammar pgf opts prGrammar pgf opts
| isOpt "pgf" opts = do | isOpt "pgf" opts = do