From 804232aebc2cfe52fa2f9451dfc3b10516e92b98 Mon Sep 17 00:00:00 2001 From: bjorn Date: Fri, 26 Sep 2008 13:17:21 +0000 Subject: Added 'complete' function to the PGF API. This is a higher-level API to the completion functionality. --- src/PGF.hs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'src/PGF.hs') diff --git a/src/PGF.hs b/src/PGF.hs index f943a0b4c..8c64469db 100644 --- a/src/PGF.hs +++ b/src/PGF.hs @@ -47,6 +47,7 @@ module PGF( tree2expr, expr2tree, -- ** Word Completion (Incremental Parsing) + complete, Incremental.ParseState, initState, Incremental.nextState, Incremental.getCompletions, extractExps, @@ -70,6 +71,7 @@ import GF.Text.UTF8 import GF.Data.ErrM import GF.Data.Utilities +import Data.Char import qualified Data.Map as Map import Data.Maybe import System.Random (newStdGen) @@ -182,6 +184,15 @@ categories :: PGF -> [Category] -- definition is just for convenience. startCat :: PGF -> Category +-- | Complete the last word in the given string. If the input +-- is empty or ends in whitespace, the last word is considred +-- to be the empty string. This means that the completions +-- will be all possible next words. +complete :: PGF -> Language -> Category -> String + -> [String] -- ^ Possible word completions of, + -- including the given input. + + --------------------------------------------------- -- Implementation --------------------------------------------------- @@ -242,3 +253,16 @@ languageCode pgf lang = categories pgf = [prCId c | c <- Map.keys (cats (abstract pgf))] startCat pgf = lookStartCat pgf + +complete pgf from cat input = + let (ws,prefix) = tokensAndPrefix input + state0 = initState pgf from cat + state = foldl Incremental.nextState state0 ws + compls = Incremental.getCompletions state prefix + in [unwords (ws++[c]) ++ " " | c <- Map.keys compls] + where + tokensAndPrefix :: String -> ([String],String) + tokensAndPrefix s | not (null s) && isSpace (last s) = (words s, "") + | null ws = ([],"") + | otherwise = (init ws, last ws) + where ws = words s \ No newline at end of file -- cgit v1.2.3