forked from GitHub/gf-core
the PGF service now can do both random and exhaustive generation. these functions now return both the generated tree and its linearization
This commit is contained in:
@@ -88,7 +88,8 @@ pgfMain pgf command =
|
|||||||
"parse" -> outputJSONP =<< doParse pgf `fmap` getText `ap` getCat `ap` getFrom
|
"parse" -> outputJSONP =<< doParse pgf `fmap` getText `ap` getCat `ap` getFrom
|
||||||
"complete" -> outputJSONP =<< doComplete pgf `fmap` getText `ap` getCat `ap` getFrom `ap` getLimit
|
"complete" -> outputJSONP =<< doComplete pgf `fmap` getText `ap` getCat `ap` getFrom `ap` getLimit
|
||||||
"linearize" -> outputJSONP =<< doLinearize pgf `fmap` getTree `ap` getTo
|
"linearize" -> outputJSONP =<< doLinearize pgf `fmap` getTree `ap` getTo
|
||||||
"random" -> getCat >>= \c -> getLimit >>= liftIO . doRandom pgf c >>= outputJSONP
|
"random" -> getCat >>= \c -> getDepth >>= \dp -> getLimit >>= \l -> getTo >>= \to -> liftIO (doRandom pgf c dp l to) >>= outputJSONP
|
||||||
|
"generate" -> outputJSONP =<< doGenerate pgf `fmap` getCat `ap` getDepth `ap` getLimit `ap` getTo
|
||||||
"translate" -> outputJSONP =<< doTranslate pgf `fmap` getText `ap` getCat `ap` getFrom `ap` getTo
|
"translate" -> outputJSONP =<< doTranslate pgf `fmap` getText `ap` getCat `ap` getFrom `ap` getTo
|
||||||
"translategroup" -> outputJSONP =<< doTranslateGroup pgf `fmap` getText `ap` getCat `ap` getFrom `ap` getTo
|
"translategroup" -> outputJSONP =<< doTranslateGroup pgf `fmap` getText `ap` getCat `ap` getFrom `ap` getTo
|
||||||
"grammar" -> outputJSONP =<< doGrammar pgf `fmap` requestAcceptLanguage
|
"grammar" -> outputJSONP =<< doGrammar pgf `fmap` requestAcceptLanguage
|
||||||
@@ -144,6 +145,9 @@ pgfMain pgf command =
|
|||||||
getLimit :: CGI (Maybe Int)
|
getLimit :: CGI (Maybe Int)
|
||||||
getLimit = readInput "limit"
|
getLimit = readInput "limit"
|
||||||
|
|
||||||
|
getDepth :: CGI (Maybe Int)
|
||||||
|
getDepth = readInput "depth"
|
||||||
|
|
||||||
getLang :: String -> CGI (Maybe PGF.Language)
|
getLang :: String -> CGI (Maybe PGF.Language)
|
||||||
getLang i =
|
getLang i =
|
||||||
do mlang <- getInput i
|
do mlang <- getInput i
|
||||||
@@ -264,11 +268,30 @@ doLinearize :: PGF -> PGF.Tree -> Maybe PGF.Language -> JSValue
|
|||||||
doLinearize pgf tree mto = showJSON $ map toJSObject
|
doLinearize pgf tree mto = showJSON $ map toJSObject
|
||||||
[[("to", PGF.showLanguage to),("text",text)] | (to,text) <- linearize' pgf mto tree]
|
[[("to", PGF.showLanguage to),("text",text)] | (to,text) <- linearize' pgf mto tree]
|
||||||
|
|
||||||
doRandom :: PGF -> Maybe PGF.Type -> Maybe Int -> IO JSValue
|
doRandom :: PGF -> Maybe PGF.Type -> Maybe Int -> Maybe Int -> Maybe PGF.Language -> IO JSValue
|
||||||
doRandom pgf mcat mlimit =
|
doRandom pgf mcat mdepth mlimit mto =
|
||||||
do trees <- random' pgf mcat
|
do g <- newStdGen
|
||||||
return $ showJSON $ map toJSObject [[("tree", PGF.showExpr [] tree)] | tree <- limit trees]
|
let trees = PGF.generateRandomDepth g pgf cat (Just depth)
|
||||||
where limit = take (fromMaybe 1 mlimit)
|
return $ showJSON $
|
||||||
|
[toJSObject [("tree", showJSON (PGF.showExpr [] tree)),
|
||||||
|
("linearizations", showJSON [toJSObject [("to", PGF.showLanguage to),("text",text)]
|
||||||
|
| (to,text) <- linearize' pgf mto tree])]
|
||||||
|
| tree <- limit trees]
|
||||||
|
where cat = fromMaybe (PGF.startCat pgf) mcat
|
||||||
|
limit = take (fromMaybe 1 mlimit)
|
||||||
|
depth = fromMaybe 4 mdepth
|
||||||
|
|
||||||
|
doGenerate :: PGF -> Maybe PGF.Type -> Maybe Int -> Maybe Int -> Maybe PGF.Language -> JSValue
|
||||||
|
doGenerate pgf mcat mdepth mlimit mto =
|
||||||
|
let trees = PGF.generateAllDepth pgf cat (Just depth)
|
||||||
|
in showJSON $
|
||||||
|
[toJSObject [("tree", showJSON (PGF.showExpr [] tree)),
|
||||||
|
("linearizations", showJSON [toJSObject [("to", PGF.showLanguage to),("text",text)]
|
||||||
|
| (to,text) <- linearize' pgf mto tree])]
|
||||||
|
| tree <- limit trees]
|
||||||
|
where cat = fromMaybe (PGF.startCat pgf) mcat
|
||||||
|
limit = take (fromMaybe 1 mlimit)
|
||||||
|
depth = fromMaybe 4 mdepth
|
||||||
|
|
||||||
doGrammar :: PGF -> Maybe (Accept Language) -> JSValue
|
doGrammar :: PGF -> Maybe (Accept Language) -> JSValue
|
||||||
doGrammar pgf macc = showJSON $ toJSObject
|
doGrammar pgf macc = showJSON $ toJSObject
|
||||||
@@ -466,11 +489,6 @@ linearizeAndBind pgf mto t = [(la, binds s) | (la,s) <- linearize' pgf mto t]
|
|||||||
u:ws2 -> u : bs ws2
|
u:ws2 -> u : bs ws2
|
||||||
_ -> []
|
_ -> []
|
||||||
|
|
||||||
random' :: PGF -> Maybe PGF.Type -> IO [PGF.Tree]
|
|
||||||
random' pgf mcat = do
|
|
||||||
g <- newStdGen
|
|
||||||
return $ PGF.generateRandom g pgf (fromMaybe (PGF.startCat pgf) mcat)
|
|
||||||
|
|
||||||
selectLanguage :: PGF -> Maybe (Accept Language) -> PGF.Language
|
selectLanguage :: PGF -> Maybe (Accept Language) -> PGF.Language
|
||||||
selectLanguage pgf macc = case acceptable of
|
selectLanguage pgf macc = case acceptable of
|
||||||
[] -> case PGF.languages pgf of
|
[] -> case PGF.languages pgf of
|
||||||
|
|||||||
Reference in New Issue
Block a user