1
0
forked from GitHub/gf-core

a dialogue-like editor loop

This commit is contained in:
aarne
2008-12-11 15:36:40 +00:00
parent a13fa9d7c5
commit 8c4cc8fa5f
2 changed files with 45 additions and 11 deletions

View File

@@ -42,7 +42,8 @@ module PGF(
-- * Operations -- * Operations
-- ** Linearization -- ** Linearization
linearize, linearizeAllLang, linearizeAll, linearize, linearizeAllLang, linearizeAll,
showPrintName,
-- ** Parsing -- ** Parsing
parse, canParse, parseAllLang, parseAll, parse, canParse, parseAllLang, parseAll,
@@ -126,6 +127,9 @@ linearizeAll :: PGF -> Tree -> [String]
-- available in the grammar. -- available in the grammar.
linearizeAllLang :: PGF -> Tree -> [(Language,String)] linearizeAllLang :: PGF -> Tree -> [(Language,String)]
-- | Show the printname of a type
showPrintName :: PGF -> Language -> Type -> String
-- | The same as 'parseAllLang' but does not return -- | The same as 'parseAllLang' but does not return
-- the language. -- the language.
parseAll :: PGF -> Type -> String -> [[Tree]] parseAll :: PGF -> Type -> String -> [[Tree]]
@@ -237,6 +241,8 @@ linearizeAll mgr = map snd . linearizeAllLang mgr
linearizeAllLang mgr t = linearizeAllLang mgr t =
[(lang,PGF.linearize mgr lang t) | lang <- languages mgr] [(lang,PGF.linearize mgr lang t) | lang <- languages mgr]
showPrintName pgf lang (DTyp _ c _) = realize $ lookPrintName pgf lang c
parseAll mgr typ = map snd . parseAllLang mgr typ parseAll mgr typ = map snd . parseAllLang mgr typ
parseAllLang mgr typ s = parseAllLang mgr typ s =

View File

@@ -18,22 +18,28 @@ main = do
pgf <- readPGF file pgf <- readPGF file
let dict = pgf2dict pgf let dict = pgf2dict pgf
let st0 = new (startCat pgf) let st0 = new (startCat pgf)
editLoop pgf dict st0 let lang = head (languages pgf) ---- for printnames; enable choosing lang
editLoop pgf dict lang st0 -- alt 1: all editing commands
-- dialogueLoop pgf dict lang st0 -- alt 2: just refinement by parsing (see bottom)
editLoop :: PGF -> Dict -> State -> IO State editLoop :: PGF -> Dict -> Language -> State -> IO State
editLoop pgf dict st = do editLoop pgf dict lang st = do
putStrLn $ if isMetaFocus st putStrLn $
then "I want something of type " ++ showType (focusType st) ++ if null (allMetas st)
then unlines
(["The tree is complete:",prState st] ++ linearizeAll pgf (stateTree st))
else if isMetaFocus st
then "I want something of type " ++ showType (focusType st) ++
" (0 - " ++ show (length (refineMenu dict st)-1) ++ ")" " (0 - " ++ show (length (refineMenu dict st)-1) ++ ")"
else "Do you want to change this node?" else "Do you want to change this node?"
c <- getLine c <- getLine
st' <- interpret pgf dict st c st' <- interpret pgf dict st c
editLoop pgf dict st' editLoop pgf dict lang st'
interpret :: PGF -> Dict -> State -> String -> IO State interpret :: PGF -> Dict -> State -> String -> IO State
interpret pgf dict st c = case words c of interpret pgf dict st c = case words c of
"r":f:_ -> do "r":f:_ -> do
let st' = goNext (refine dict (mkCId f) st) let st' = goNextMeta (refine dict (mkCId f) st)
prLState pgf st' prLState pgf st'
return st' return st'
"p":ws -> do "p":ws -> do
@@ -43,7 +49,7 @@ interpret pgf dict st c = case words c of
return st' return st'
"a":_ -> do "a":_ -> do
t:_ <- generateRandom pgf (focusType st) t:_ <- generateRandom pgf (focusType st)
let st' = goNext (replace dict t st) let st' = goNextMeta (replace dict t st)
prLState pgf st' prLState pgf st'
return st' return st'
"d":_ -> do "d":_ -> do
@@ -85,13 +91,13 @@ prLState pgf st = do
selectReplace :: Dict -> [Tree] -> State -> IO State selectReplace :: Dict -> [Tree] -> State -> IO State
selectReplace dict ts st = case ts of selectReplace dict ts st = case ts of
[] -> putStrLn "no results" >> return st [] -> putStrLn "no results" >> return st
[t] -> return $ goNext $ replace dict t st [t] -> return $ goNextMeta $ replace dict t st
_ -> do _ -> do
mapM_ putStrLn $ "choose tree by entering its number:" : mapM_ putStrLn $ "choose tree by entering its number:" :
[show i ++ " : " ++ showTree t | (i,t) <- zip [0..] ts] [show i ++ " : " ++ showTree t | (i,t) <- zip [0..] ts]
d <- getLine d <- getLine
let t = ts !! read d let t = ts !! read d
return $ goNext $ replace dict t st return $ goNextMeta $ replace dict t st
commandHelp = unlines [ commandHelp = unlines [
"a -- refine with a random subtree", "a -- refine with a random subtree",
@@ -106,3 +112,25 @@ commandHelp = unlines [
"> -- go to next node" "> -- go to next node"
] ]
----------------
-- for a dialogue system, working just by parsing; questions are cat printnames
----------------
dialogueLoop :: PGF -> Dict -> Language -> State -> IO State
dialogueLoop pgf dict lang st = do
putStrLn $
if null (allMetas st)
then "Ready!\n " ++ unlines (linearizeAll pgf (stateTree st))
else if isMetaFocus st
then showPrintName pgf lang (focusType st)
else "Do you want to change this node?"
c <- getLine
st' <- interpretD pgf dict st c
dialogueLoop pgf dict lang st'
interpretD :: PGF -> Dict -> State -> String -> IO State
interpretD pgf dict st c = do
let tts = parseAll pgf (focusType st) c
st' <- selectReplace dict (concat tts) st
-- prLState pgf st'
return st'