forked from GitHub/gf-core
Added 'complete' function to the PGF API. This is a higher-level API to the completion functionality.
This commit is contained in:
24
src/PGF.hs
24
src/PGF.hs
@@ -47,6 +47,7 @@ module PGF(
|
|||||||
tree2expr, expr2tree,
|
tree2expr, expr2tree,
|
||||||
|
|
||||||
-- ** Word Completion (Incremental Parsing)
|
-- ** Word Completion (Incremental Parsing)
|
||||||
|
complete,
|
||||||
Incremental.ParseState,
|
Incremental.ParseState,
|
||||||
initState, Incremental.nextState, Incremental.getCompletions, extractExps,
|
initState, Incremental.nextState, Incremental.getCompletions, extractExps,
|
||||||
|
|
||||||
@@ -70,6 +71,7 @@ import GF.Text.UTF8
|
|||||||
import GF.Data.ErrM
|
import GF.Data.ErrM
|
||||||
import GF.Data.Utilities
|
import GF.Data.Utilities
|
||||||
|
|
||||||
|
import Data.Char
|
||||||
import qualified Data.Map as Map
|
import qualified Data.Map as Map
|
||||||
import Data.Maybe
|
import Data.Maybe
|
||||||
import System.Random (newStdGen)
|
import System.Random (newStdGen)
|
||||||
@@ -182,6 +184,15 @@ categories :: PGF -> [Category]
|
|||||||
-- definition is just for convenience.
|
-- definition is just for convenience.
|
||||||
startCat :: PGF -> Category
|
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
|
-- Implementation
|
||||||
---------------------------------------------------
|
---------------------------------------------------
|
||||||
@@ -242,3 +253,16 @@ languageCode pgf lang =
|
|||||||
categories pgf = [prCId c | c <- Map.keys (cats (abstract pgf))]
|
categories pgf = [prCId c | c <- Map.keys (cats (abstract pgf))]
|
||||||
|
|
||||||
startCat pgf = lookStartCat 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
|
||||||
@@ -123,24 +123,10 @@ parse' pgf input mcat mfrom =
|
|||||||
|
|
||||||
complete' :: PGF -> String -> Maybe PGF.Category -> Maybe PGF.Language -> [(PGF.Language,[String])]
|
complete' :: PGF -> String -> Maybe PGF.Category -> Maybe PGF.Language -> [(PGF.Language,[String])]
|
||||||
complete' pgf input mcat mfrom =
|
complete' pgf input mcat mfrom =
|
||||||
[(from,ss) | from <- froms, PGF.canParse pgf from, let ss = complete pgf from cat input, not (null ss)]
|
[(from,ss) | from <- froms, PGF.canParse pgf from, let ss = PGF.complete pgf from cat input, not (null ss)]
|
||||||
where froms = maybe (PGF.languages pgf) (:[]) mfrom
|
where froms = maybe (PGF.languages pgf) (:[]) mfrom
|
||||||
cat = fromMaybe (PGF.startCat pgf) mcat
|
cat = fromMaybe (PGF.startCat pgf) mcat
|
||||||
|
|
||||||
complete :: PGF -> PGF.Language -> PGF.Category -> String -> [String]
|
|
||||||
complete pgf from cat input =
|
|
||||||
let (ws,prefix) = tokensAndPrefix input
|
|
||||||
state0 = PGF.initState pgf from cat
|
|
||||||
state = foldl PGF.nextState state0 ws
|
|
||||||
compls = PGF.getCompletions state prefix
|
|
||||||
in [unwords (ws++[c]) ++ " " | c <- Map.keys compls]
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
linearize' :: PGF -> Maybe PGF.Language -> PGF.Tree -> [(PGF.Language,String)]
|
linearize' :: PGF -> Maybe PGF.Language -> PGF.Tree -> [(PGF.Language,String)]
|
||||||
linearize' pgf mto tree =
|
linearize' pgf mto tree =
|
||||||
case mto of
|
case mto of
|
||||||
|
|||||||
Reference in New Issue
Block a user