Skip to content

Commit 3e0c0fa

Browse files
committed
define default depths for shell and server only once
1 parent af09351 commit 3e0c0fa

File tree

3 files changed

+33
-23
lines changed

3 files changed

+33
-23
lines changed

src/compiler/GF/Command/Commands.hs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import GF.Infra.SIO
2222
import GF.Command.Abstract
2323
import GF.Command.CommandInfo
2424
import GF.Command.CommonCommands
25+
import qualified GF.Command.CommonCommands as Common
2526
import GF.Text.Clitics
2627
import GF.Quiz
2728

@@ -166,14 +167,15 @@ pgfCommands = Map.fromList [
166167
synopsis = "generate random trees in the current abstract syntax",
167168
syntax = "gr [-cat=CAT] [-number=INT]",
168169
examples = [
169-
mkEx "gr -- one tree in the startcat of the current grammar",
170-
mkEx "gr -cat=NP -number=16 -- 16 trees in the category NP",
171-
mkEx "gr -lang=LangHin,LangTha -cat=Cl -- Cl, both in LangHin and LangTha",
172-
mkEx "gr -probs=FILE -- generate with bias",
173-
mkEx "gr (AdjCN ? (UseN ?)) -- generate trees of form (AdjCN ? (UseN ?))"
170+
mkEx $ "gr -- one tree in the startcat of the current grammar, up to depth " ++ Common.default_depth_str,
171+
mkEx "gr -cat=NP -number=16 -- 16 trees in the category NP",
172+
mkEx "gr -cat=NP -depth=2 -- one tree in the category NP, up to depth 2",
173+
mkEx "gr -lang=LangHin,LangTha -cat=Cl -- Cl, both in LangHin and LangTha",
174+
mkEx "gr -probs=FILE -- generate with bias",
175+
mkEx "gr (AdjCN ? (UseN ?)) -- generate trees of form (AdjCN ? (UseN ?))"
174176
],
175177
explanation = unlines [
176-
"Generates a list of random trees, by default one tree.",
178+
"Generates a list of random trees, by default one tree up to depth " ++ Common.default_depth_str ++ ".",
177179
"If a tree argument is given, the command completes the Tree with values to",
178180
"all metavariables in the tree. The generation can be biased by probabilities,",
179181
"given in a file in the -probs flag."
@@ -182,13 +184,13 @@ pgfCommands = Map.fromList [
182184
("cat","generation category"),
183185
("lang","uses only functions that have linearizations in all these languages"),
184186
("number","number of trees generated"),
185-
("depth","the maximum generation depth"),
187+
("depth","the maximum generation depth (default: " ++ Common.default_depth_str ++ ")"),
186188
("probs", "file with biased probabilities (format 'f 0.4' one by line)")
187189
],
188190
exec = getEnv $ \ opts arg (Env pgf mos) -> do
189191
pgf <- optProbs opts (optRestricted opts pgf)
190192
gen <- newStdGen
191-
let dp = valIntOpts "depth" 4 opts
193+
let dp = valIntOpts "depth" Common.default_depth opts
192194
let ts = case mexp (toExprs arg) of
193195
Just ex -> generateRandomFromDepth gen pgf ex (Just dp)
194196
Nothing -> generateRandomDepth gen pgf (optType pgf opts) (Just dp)
@@ -199,25 +201,25 @@ pgfCommands = Map.fromList [
199201
synopsis = "generates a list of trees, by default exhaustive",
200202
explanation = unlines [
201203
"Generates all trees of a given category. By default, ",
202-
"the depth is limited to 4, but this can be changed by a flag.",
204+
"the depth is limited to " ++ Common.default_depth_str ++ ", but this can be changed by a flag.",
203205
"If a Tree argument is given, the command completes the Tree with values",
204206
"to all metavariables in the tree."
205207
],
206208
flags = [
207209
("cat","the generation category"),
208-
("depth","the maximum generation depth"),
210+
("depth","the maximum generation depth (default: " ++ Common.default_depth_str ++ ")"),
209211
("lang","excludes functions that have no linearization in this language"),
210212
("number","the number of trees generated")
211213
],
212214
examples = [
213-
mkEx "gt -- all trees in the startcat, to depth 4",
214-
mkEx "gt -cat=NP -number=16 -- 16 trees in the category NP",
215-
mkEx "gt -cat=NP -depth=2 -- trees in the category NP to depth 2",
216-
mkEx "gt (AdjCN ? (UseN ?)) -- trees of form (AdjCN ? (UseN ?))"
215+
mkEx $ "gt -- all trees in the startcat, to depth " ++ Common.default_depth_str,
216+
mkEx "gt -cat=NP -number=16 -- 16 trees in the category NP",
217+
mkEx "gt -cat=NP -depth=2 -- trees in the category NP to depth 2",
218+
mkEx "gt (AdjCN ? (UseN ?)) -- trees of form (AdjCN ? (UseN ?))"
217219
],
218220
exec = getEnv $ \ opts arg (Env pgf mos) -> do
219221
let pgfr = optRestricted opts pgf
220-
let dp = valIntOpts "depth" 4 opts
222+
let dp = valIntOpts "depth" Common.default_depth opts
221223
let ts = case toExprs arg of
222224
[] -> generateAllDepth pgfr (optType pgf opts) (Just dp)
223225
es -> concat [generateFromDepth pgfr e (Just dp) | e <- es]
@@ -547,7 +549,7 @@ pgfCommands = Map.fromList [
547549
"which is processed by dot (graphviz) and displayed by the program indicated",
548550
"by the view flag. The target format is png, unless overridden by the",
549551
"flag -format. Results from multiple trees are combined to pdf with convert (ImageMagick).",
550-
"See also 'vp -showdep' for another visualization of dependencies."
552+
"See also 'vp -showdep' for another visualization of dependencies."
551553
],
552554
exec = getEnv $ \ opts arg (Env pgf mos) -> do
553555
let absname = abstractName pgf
@@ -760,7 +762,7 @@ pgfCommands = Map.fromList [
760762
[] -> [parse_ pgf lang (optType pgf opts) (Just dp) s | lang <- optLangs pgf opts]
761763
open_typs -> [parseWithRecovery pgf lang (optType pgf opts) open_typs (Just dp) s | lang <- optLangs pgf opts]
762764
where
763-
dp = valIntOpts "depth" 4 opts
765+
dp = valIntOpts "depth" Common.default_depth opts
764766

765767
fromParse opts = foldr (joinPiped . fromParse1 opts) void
766768

@@ -800,9 +802,9 @@ pgfCommands = Map.fromList [
800802
_ | isOpt "tabtreebank" opts ->
801803
return $ concat $ intersperse "\t" $ (showExpr [] t) :
802804
[s | lang <- optLangs pgf opts, s <- linear pgf opts lang t]
803-
_ | isOpt "chunks" opts -> map snd $ linChunks pgf opts t
805+
_ | isOpt "chunks" opts -> map snd $ linChunks pgf opts t
804806
_ -> [s | lang <- optLangs pgf opts, s<-linear pgf opts lang t]
805-
linChunks pgf opts t =
807+
linChunks pgf opts t =
806808
[(lang, unwords (intersperse "<+>" (map (unlines . linear pgf opts lang) (treeChunks t)))) | lang <- optLangs pgf opts]
807809

808810
linear :: PGF -> [Option] -> CId -> Expr -> [String]
@@ -1006,13 +1008,13 @@ viewLatex view name grphs = do
10061008
restrictedSystem $ "pdflatex " ++ texfile
10071009
restrictedSystem $ view ++ " " ++ pdffile
10081010
return void
1009-
1011+
10101012
---- copied from VisualizeTree ; not sure about proper place AR Nov 2015
10111013
latexDoc :: [String] -> String
10121014
latexDoc body = unlines $
10131015
"\\batchmode"
10141016
: "\\documentclass{article}"
1015-
: "\\usepackage[utf8]{inputenc}"
1017+
: "\\usepackage[utf8]{inputenc}"
10161018
: "\\begin{document}"
10171019
: spaces body
10181020
++ ["\\end{document}"]

src/compiler/GF/Command/CommonCommands.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ import Data.Char (isSpace)
1919

2020
import qualified PGF as H(showCId,showExpr,toATree,toTrie,Trie(..))
2121

22+
-- store default generation depth in a variable and use everywhere
23+
default_depth :: Int
24+
default_depth = 5
25+
default_depth_str = show default_depth
26+
27+
2228
extend old new = Map.union (Map.fromList new) old -- Map.union is left-biased
2329

2430
commonCommands :: (Monad m,MonadSIO m) => Map.Map String (CommandInfo m)

src/server/PGFService.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,8 @@ limit, depth :: CGI (Maybe Int)
571571
limit = readInput "limit"
572572
depth = readInput "depth"
573573

574+
default_depth_server = 4
575+
574576
start :: CGI Int
575577
start = maybe 0 id # readInput "start"
576578

@@ -781,7 +783,7 @@ doRandom pgf mcat mdepth mlimit to =
781783
| tree <- limit trees]
782784
where cat = fromMaybe (PGF.startCat pgf) mcat
783785
limit = take (fromMaybe 1 mlimit)
784-
depth = fromMaybe 4 mdepth
786+
depth = fromMaybe default_depth_server mdepth
785787

786788
doGenerate :: PGF -> Maybe PGF.Type -> Maybe Int -> Maybe Int -> To -> JSValue
787789
doGenerate pgf mcat mdepth mlimit tos =
@@ -794,7 +796,7 @@ doGenerate pgf mcat mdepth mlimit tos =
794796
trees = PGF.generateAllDepth pgf cat (Just depth)
795797
cat = fromMaybe (PGF.startCat pgf) mcat
796798
limit = take (fromMaybe 1 mlimit)
797-
depth = fromMaybe 4 mdepth
799+
depth = fromMaybe default_depth_server mdepth
798800

799801
doGrammar :: (UTCTime,PGF) -> Either IOError (UTCTime,l) -> Maybe (Accept Language) -> CGI CGIResult
800802
doGrammar (t1,pgf) elbls macc = out t $ showJSON $ makeObj

0 commit comments

Comments
 (0)