Changed all SRG printer to take Options and StateGrammar arguments. This makes Custom a lot cleaner.

This commit is contained in:
bringert
2007-01-05 15:38:47 +00:00
parent 9af5b77102
commit 028f2e108e
10 changed files with 83 additions and 123 deletions

View File

@@ -27,6 +27,7 @@ import GF.Formalism.CFG
import GF.Formalism.Utilities (Symbol(..), mapSymbol, filterCats, symbol, NameProfile(..)) import GF.Formalism.Utilities (Symbol(..), mapSymbol, filterCats, symbol, NameProfile(..))
import GF.Conversion.Types import GF.Conversion.Types
import GF.Infra.Ident (Ident) import GF.Infra.Ident (Ident)
import GF.Infra.Option (Options)
import GF.Compile.ShellState (StateGrammar) import GF.Compile.ShellState (StateGrammar)
import GF.Speech.FiniteState import GF.Speech.FiniteState
@@ -57,8 +58,9 @@ data MFA a = MFA (DFA (MFALabel a)) [(String,DFA (MFALabel a))]
cfgToFA :: String -> StateGrammar -> DFA String cfgToFA :: Options -> StateGrammar -> DFA String
cfgToFA start = minimize . compileAutomaton start . makeSimpleRegular cfgToFA opts s = minimize $ compileAutomaton start $ makeSimpleRegular s
where start = getStartCatCF opts s
makeSimpleRegular :: StateGrammar -> CFRules makeSimpleRegular :: StateGrammar -> CFRules
makeSimpleRegular = makeRegular . removeIdenticalRules . removeEmptyCats . cfgToCFRules makeSimpleRegular = makeRegular . removeIdenticalRules . removeEmptyCats . cfgToCFRules
@@ -145,12 +147,13 @@ make_fa c@(g,ns) q0 alpha q1 fa =
-- * Compile a strongly regular grammar to a DFA with sub-automata -- * Compile a strongly regular grammar to a DFA with sub-automata
-- --
cfgToMFA :: String -> StateGrammar -> MFA String cfgToMFA :: Options -> StateGrammar -> MFA String
cfgToMFA start g = buildMFA start g cfgToMFA opts s = buildMFA start s
where start = getStartCatCF opts s
-- | Build a DFA by building and expanding an MFA -- | Build a DFA by building and expanding an MFA
cfgToFA' :: String -> StateGrammar -> DFA String cfgToFA' :: Options -> StateGrammar -> DFA String
cfgToFA' start = mfaToDFA . cfgToMFA start cfgToFA' opts s = mfaToDFA $ cfgToMFA opts s
buildMFA :: Cat_ -- ^ Start category buildMFA :: Cat_ -- ^ Start category
-> StateGrammar -> MFA String -> StateGrammar -> MFA String

View File

@@ -23,6 +23,7 @@ import GF.Conversion.Types
import GF.Formalism.CFG import GF.Formalism.CFG
import GF.Formalism.Utilities (Symbol(..),symbol) import GF.Formalism.Utilities (Symbol(..),symbol)
import GF.Infra.Ident import GF.Infra.Ident
import GF.Infra.Option (Options)
import GF.Infra.Print import GF.Infra.Print
import GF.Speech.CFGToFiniteState import GF.Speech.CFGToFiniteState
import GF.Speech.FiniteState import GF.Speech.FiniteState
@@ -36,23 +37,21 @@ import Data.Maybe (fromMaybe)
faGraphvizPrinter :: Ident -- ^ Grammar name faGraphvizPrinter :: Options -> StateGrammar -> String
-> String -> StateGrammar -> String faGraphvizPrinter opts s =
faGraphvizPrinter name start = prFAGraphviz $ mapStates (const "") $ cfgToFA opts s
prFAGraphviz . mapStates (const "") . cfgToFA start
-- | Convert the grammar to a regular grammar and print it in BNF -- | Convert the grammar to a regular grammar and print it in BNF
regularPrinter :: StateGrammar -> String regularPrinter :: Options -> StateGrammar -> String
regularPrinter = prCFRules . makeSimpleRegular regularPrinter opts s = prCFRules $ makeSimpleRegular s
where where
prCFRules :: CFRules -> String prCFRules :: CFRules -> String
prCFRules g = unlines [ c ++ " ::= " ++ join " | " (map (showRhs . ruleRhs) rs) | (c,rs) <- g] prCFRules g = unlines [ c ++ " ::= " ++ join " | " (map (showRhs . ruleRhs) rs) | (c,rs) <- g]
join g = concat . intersperse g join g = concat . intersperse g
showRhs = unwords . map (symbol id show) showRhs = unwords . map (symbol id show)
faCPrinter :: Ident -- ^ Grammar name faCPrinter :: Options -> StateGrammar -> String
-> String -> StateGrammar -> String faCPrinter opts s = fa2c $ cfgToFA opts s
faCPrinter name start = fa2c . cfgToFA start
fa2c :: DFA String -> String fa2c :: DFA String -> String
fa2c fa = undefined fa2c fa = undefined

View File

@@ -31,11 +31,8 @@ import GF.Compile.ShellState (StateGrammar)
import Data.Char (toUpper,toLower) import Data.Char (toUpper,toLower)
gslPrinter :: Ident -- ^ Grammar name gslPrinter :: Options -> StateGrammar -> String
-> String -- ^ Start category gslPrinter opts s = prGSL $ topDownFilter $ makeSimpleSRG opts s
-> Options -> StateGrammar -> String
gslPrinter name start opts =
prGSL . topDownFilter . makeSimpleSRG name start opts
prGSL :: SRG -> String prGSL :: SRG -> String
prGSL (SRG{grammarName=name,startCat=start,origStartCat=origStart,rules=rs}) prGSL (SRG{grammarName=name,startCat=start,origStartCat=origStart,rules=rs})

View File

@@ -38,13 +38,10 @@ import Text.PrettyPrint.HughesPJ
import Debug.Trace import Debug.Trace
jsgfPrinter :: Ident -- ^ Grammar name jsgfPrinter :: Maybe SISRFormat
-> String -- ^ Start category
-> Options -> Options
-> Maybe SISRFormat
-> StateGrammar -> String -> StateGrammar -> String
jsgfPrinter name start opts sisr = jsgfPrinter sisr opts s = show $ prJSGF sisr $ makeSimpleSRG opts s
show . prJSGF sisr . makeSimpleSRG name start opts
prJSGF :: Maybe SISRFormat -> SRG -> Doc prJSGF :: Maybe SISRFormat -> SRG -> Doc
prJSGF sisr srg@(SRG{grammarName=name,startCat=start,origStartCat=origStart,rules=rs}) prJSGF sisr srg@(SRG{grammarName=name,startCat=start,origStartCat=origStart,rules=rs})

View File

@@ -12,11 +12,11 @@ module GF.Speech.PrRegExp (regexpPrinter) where
import GF.Conversion.Types import GF.Conversion.Types
import GF.Infra.Ident import GF.Infra.Ident
import GF.Infra.Option (Options)
import GF.Speech.CFGToFiniteState import GF.Speech.CFGToFiniteState
import GF.Speech.RegExp import GF.Speech.RegExp
import GF.Compile.ShellState (StateGrammar) import GF.Compile.ShellState (StateGrammar)
regexpPrinter :: Ident -- ^ Grammar name regexpPrinter :: Options -> StateGrammar -> String
-> String -> StateGrammar -> String regexpPrinter opts s = prRE $ dfa2re $ cfgToFA opts s
regexpPrinter name start = prRE . dfa2re . cfgToFA start

View File

@@ -26,6 +26,7 @@ import GF.Conversion.Types
import GF.Formalism.CFG import GF.Formalism.CFG
import GF.Formalism.Utilities (Symbol(..),symbol) import GF.Formalism.Utilities (Symbol(..),symbol)
import GF.Infra.Ident import GF.Infra.Ident
import GF.Infra.Option (Options)
import GF.Infra.Print import GF.Infra.Print
import GF.Speech.CFGToFiniteState import GF.Speech.CFGToFiniteState
import GF.Speech.FiniteState import GF.Speech.FiniteState
@@ -54,9 +55,9 @@ data SLFEdge = SLFEdge { eId :: Int, eStart :: Int, eEnd :: Int }
type SLF_FA = FA State (Maybe (MFALabel String)) () type SLF_FA = FA State (Maybe (MFALabel String)) ()
mkFAs :: String -> StateGrammar -> (SLF_FA, [(String,SLF_FA)]) mkFAs :: Options -> StateGrammar -> (SLF_FA, [(String,SLF_FA)])
mkFAs start s = (slfStyleFA main, [(c,slfStyleFA n) | (c,n) <- subs]) mkFAs opts s = (slfStyleFA main, [(c,slfStyleFA n) | (c,n) <- subs])
where MFA main subs = {- renameSubs $ -} cfgToMFA start s where MFA main subs = {- renameSubs $ -} cfgToMFA opts s
slfStyleFA :: Eq a => DFA a -> FA State (Maybe a) () slfStyleFA :: Eq a => DFA a -> FA State (Maybe a) ()
slfStyleFA = renameStates [0..] . removeTrivialEmptyNodes . oneFinalState Nothing () slfStyleFA = renameStates [0..] . removeTrivialEmptyNodes . oneFinalState Nothing ()
@@ -76,9 +77,9 @@ renameSubs (MFA main subs) = MFA (renameLabels main) subs'
-- * SLF graphviz printing (without sub-networks) -- * SLF graphviz printing (without sub-networks)
-- --
slfGraphvizPrinter :: Ident -> String -> StateGrammar -> String slfGraphvizPrinter :: Options -> StateGrammar -> String
slfGraphvizPrinter name start slfGraphvizPrinter opts s
= prFAGraphviz . gvFA . slfStyleFA . cfgToFA' start = prFAGraphviz $ gvFA $ slfStyleFA $ cfgToFA' opts s
where where
gvFA = mapStates (fromMaybe "") . mapTransitions (const "") gvFA = mapStates (fromMaybe "") . mapTransitions (const "")
@@ -86,10 +87,9 @@ slfGraphvizPrinter name start
-- * SLF graphviz printing (with sub-networks) -- * SLF graphviz printing (with sub-networks)
-- --
slfSubGraphvizPrinter :: Ident -- ^ Grammar name slfSubGraphvizPrinter :: Options -> StateGrammar -> String
-> String -> StateGrammar -> String slfSubGraphvizPrinter opts s = Dot.prGraphviz g
slfSubGraphvizPrinter name start s = Dot.prGraphviz g where (main, subs) = mkFAs opts s
where (main, subs) = mkFAs start s
g = STM.evalState (liftM2 Dot.addSubGraphs ss m) [0..] g = STM.evalState (liftM2 Dot.addSubGraphs ss m) [0..]
ss = mapM (\ (c,f) -> gvSLFFA (Just c) f) subs ss = mapM (\ (c,f) -> gvSLFFA (Just c) f) subs
m = gvSLFFA Nothing main m = gvSLFFA Nothing main
@@ -114,20 +114,19 @@ gvSLFFA n fa =
-- * SLF printing (without sub-networks) -- * SLF printing (without sub-networks)
-- --
slfPrinter :: Ident -> String -> StateGrammar -> String slfPrinter :: Options -> StateGrammar -> String
slfPrinter name start slfPrinter opts s
= prSLF . automatonToSLF mkSLFNode . slfStyleFA . cfgToFA' start = prSLF $ automatonToSLF mkSLFNode $ slfStyleFA $ cfgToFA' opts s
-- --
-- * SLF printing (with sub-networks) -- * SLF printing (with sub-networks)
-- --
-- | Make a network with subnetworks in SLF -- | Make a network with subnetworks in SLF
slfSubPrinter :: Ident -- ^ Grammar name slfSubPrinter :: Options -> StateGrammar -> String
-> String -> StateGrammar -> String slfSubPrinter opts s = prSLFs slfs
slfSubPrinter name start s = prSLFs slfs
where where
(main,subs) = mkFAs start s (main,subs) = mkFAs opts s
slfs = SLFs [(c, faToSLF fa) | (c,fa) <- subs] (faToSLF main) slfs = SLFs [(c, faToSLF fa) | (c,fa) <- subs] (faToSLF main)
faToSLF = automatonToSLF mfaNodeToSLFNode faToSLF = automatonToSLF mfaNodeToSLFNode

View File

@@ -36,14 +36,11 @@ import qualified Data.Map as Map
import qualified Data.Set as Set import qualified Data.Set as Set
srgsXmlPrinter :: Ident -- ^ Grammar name srgsXmlPrinter :: Maybe SISRFormat
-> String -- ^ Start category
-> Options
-> Maybe SISRFormat
-> Bool -- ^ Include probabilities -> Bool -- ^ Include probabilities
-> StateGrammar -> String -> Options
srgsXmlPrinter name start opts sisr probs = -> StateGrammar -> String
prSrgsXml sisr probs . makeSRG name start opts srgsXmlPrinter sisr probs opts s = prSrgsXml sisr probs $ makeSRG opts s
prSrgsXml :: Maybe SISRFormat -> Bool -> SRG -> String prSrgsXml :: Maybe SISRFormat -> Bool -> SRG -> String
prSrgsXml sisr probs srg@(SRG{grammarName=name,startCat=start, prSrgsXml sisr probs srg@(SRG{grammarName=name,startCat=start,

View File

@@ -42,7 +42,7 @@ import GF.Speech.FiniteState
import GF.Speech.RegExp import GF.Speech.RegExp
import GF.Infra.Option import GF.Infra.Option
import GF.Probabilistic.Probabilistic (Probs) import GF.Probabilistic.Probabilistic (Probs)
import GF.Compile.ShellState (StateGrammar, stateProbs) import GF.Compile.ShellState (StateGrammar, stateProbs, cncId)
import Data.List import Data.List
import Data.Maybe (fromMaybe, maybeToList) import Data.Maybe (fromMaybe, maybeToList)
@@ -81,45 +81,37 @@ type CatNames = Map String String
-- | Create a non-left-recursive SRG. -- | Create a non-left-recursive SRG.
-- FIXME: the probabilities, names and profiles in the returned -- FIXME: the probabilities, names and profiles in the returned
-- grammar may be meaningless. -- grammar may be meaningless.
makeSimpleSRG :: Ident -- ^ Grammar name makeSimpleSRG :: Options -- ^ Grammar options
-> String -- ^ Start category
-> Options -- ^ Grammar options
-> StateGrammar -> StateGrammar
-> SRG -> SRG
makeSimpleSRG i origStart opts s = makeSimpleSRG opts s =
makeSRG_ i origStart opts probs $ preprocess $ cfgToCFRules s makeSRG_ (removeLeftRecursion origStart . removeIdenticalRules
where preprocess = removeLeftRecursion origStart . removeIdenticalRules . removeEmptyCats . removeCycles) opts s
. removeEmptyCats . removeCycles where origStart = getStartCatCF opts s
probs = stateProbs s
-- | Create a SRG preserving the names, profiles and probabilities of the -- | Create a SRG preserving the names, profiles and probabilities of the
-- input grammar. The returned grammar may be left-recursive. -- input grammar. The returned grammar may be left-recursive.
makeSRG :: Ident -- ^ Grammar name makeSRG :: Options -- ^ Grammar options
-> String -- ^ Start category
-> Options -- ^ Grammar options
-> StateGrammar -> StateGrammar
-> SRG -> SRG
makeSRG i origStart opts s = makeSRG = makeSRG_ removeEmptyCats
makeSRG_ i origStart opts probs $ preprocess $ cfgToCFRules s
where preprocess = removeEmptyCats
probs = stateProbs s
makeSRG_ :: Ident -- ^ Grammar name makeSRG_ :: (CFRules -> CFRules)
-> String -- ^ Start category -> Options -- ^ Grammar options
-> Options -- ^ Grammar options -> StateGrammar
-> Probs -- ^ Probabilities
-> CFRules -- ^ A context-free grammar
-> SRG -> SRG
makeSRG_ i origStart opts probs gr = makeSRG_ preprocess opts s =
SRG { grammarName = name, SRG { grammarName = name,
startCat = lookupFM_ names origStart, startCat = lookupFM_ names origStart,
origStartCat = origStart, origStartCat = origStart,
grammarLanguage = l, grammarLanguage = l,
rules = rs } rules = rs }
where where
name = prIdent i name = prIdent (cncId s)
origStart = getStartCatCF opts s
probs = stateProbs s
l = fromMaybe "en_UK" (getOptVal opts speechLanguage) l = fromMaybe "en_UK" (getOptVal opts speechLanguage)
(cats,cfgRules) = unzip gr (cats,cfgRules) = unzip $ preprocess $ cfgToCFRules s
names = mkCatNames name cats names = mkCatNames name cats
rs = map (cfgRulesToSRGRule names probs) cfgRules rs = map (cfgRulesToSRGRule names probs) cfgRules

View File

@@ -24,6 +24,7 @@ module GF.Speech.TransformCFG {- (CFRule_, CFRules,
removeEmptyCats, removeIdenticalRules) -} where removeEmptyCats, removeIdenticalRules) -} where
import GF.Conversion.Types import GF.Conversion.Types
import GF.CF.PPrCF (prCFCat)
import GF.Data.Utilities import GF.Data.Utilities
import GF.Formalism.CFG import GF.Formalism.CFG
import GF.Formalism.Utilities (Symbol(..), mapSymbol, filterCats, symbol, import GF.Formalism.Utilities (Symbol(..), mapSymbol, filterCats, symbol,
@@ -32,7 +33,7 @@ import GF.Infra.Ident
import GF.Infra.Option import GF.Infra.Option
import GF.Infra.Print import GF.Infra.Print
import GF.Speech.Relation import GF.Speech.Relation
import GF.Compile.ShellState (StateGrammar, stateCFG) import GF.Compile.ShellState (StateGrammar, stateCFG, startCatStateOpts)
import Control.Monad import Control.Monad
import Control.Monad.State (State, get, put, evalState) import Control.Monad.State (State, get, put, evalState)
@@ -76,6 +77,13 @@ cfgToCFRules s =
profileToTerm (Unify xs) = CFRes (last xs) -- FIXME: unify profileToTerm (Unify xs) = CFRes (last xs) -- FIXME: unify
profileToTerm (Constant f) = maybe CFMeta (\x -> CFObj x []) (forestName f) profileToTerm (Constant f) = maybe CFMeta (\x -> CFObj x []) (forestName f)
getStartCat :: Options -> StateGrammar -> String
getStartCat opts sgr = prCFCat (startCatStateOpts opts sgr)
getStartCatCF :: Options -> StateGrammar -> String
getStartCatCF opts sgr = getStartCat opts sgr ++ "{}.s"
-- | Remove productions which use categories which have no productions -- | Remove productions which use categories which have no productions
removeEmptyCats :: CFRules -> CFRules removeEmptyCats :: CFRules -> CFRules
removeEmptyCats = fix removeEmptyCats' removeEmptyCats = fix removeEmptyCats'

View File

@@ -248,51 +248,22 @@ customGrammarPrinter =
,(strCI "cf", \_ -> prCF . stateCF) ,(strCI "cf", \_ -> prCF . stateCF)
,(strCI "old", \_ -> printGrammarOld . stateGrammarST) ,(strCI "old", \_ -> printGrammarOld . stateGrammarST)
,(strCI "srg", \_ -> prSRG . stateCF) ,(strCI "srg", \_ -> prSRG . stateCF)
,(strCI "gsl", \opts s -> let name = cncId s ,(strCI "gsl", gslPrinter)
start = getStartCatCF opts s ,(strCI "jsgf", jsgfPrinter Nothing)
in gslPrinter name start opts s) ,(strCI "jsgf_sisr_old", jsgfPrinter (Just SISR.SISROld))
,(strCI "jsgf", \opts s -> let name = cncId s ,(strCI "srgs_xml", SRGS.srgsXmlPrinter Nothing False)
start = getStartCatCF opts s ,(strCI "srgs_xml_prob", SRGS.srgsXmlPrinter Nothing True)
in jsgfPrinter name start opts Nothing s) ,(strCI "srgs_xml_sisr_old", SRGS.srgsXmlPrinter (Just SISR.SISROld) False)
,(strCI "jsgf_sisr_old",
\opts s -> let name = cncId s
start = getStartCatCF opts s
in jsgfPrinter name start opts (Just SISR.SISROld) s)
,(strCI "srgs_xml", \opts s -> let name = cncId s
start = getStartCatCF opts s
in SRGS.srgsXmlPrinter name start opts Nothing False s)
,(strCI "srgs_xml_prob",
\opts s -> let name = cncId s
start = getStartCatCF opts s
in SRGS.srgsXmlPrinter name start opts Nothing True s)
,(strCI "srgs_xml_sisr_old",
\opts s -> let name = cncId s
start = getStartCatCF opts s
in SRGS.srgsXmlPrinter name start opts (Just SISR.SISROld) False s)
,(strCI "vxml", \opts s -> let start = cfCat2Ident (startCatStateOpts opts s) ,(strCI "vxml", \opts s -> let start = cfCat2Ident (startCatStateOpts opts s)
in grammar2vxml start s) in grammar2vxml start s)
,(strCI "slf", \opts s -> let start = getStartCatCF opts s ,(strCI "slf", slfPrinter)
name = cncId s ,(strCI "slf_graphviz", slfGraphvizPrinter)
in slfPrinter name start s) ,(strCI "slf_sub", slfSubPrinter)
,(strCI "slf_graphviz", \opts s -> let start = getStartCatCF opts s ,(strCI "slf_sub_graphviz", slfSubGraphvizPrinter)
name = cncId s ,(strCI "fa_graphviz", faGraphvizPrinter)
in slfGraphvizPrinter name start s) ,(strCI "fa_c", faCPrinter)
,(strCI "slf_sub", \opts s -> let start = getStartCatCF opts s ,(strCI "regexp", regexpPrinter)
name = cncId s ,(strCI "regular", regularPrinter)
in slfSubPrinter name start s)
,(strCI "slf_sub_graphviz", \opts s -> let start = getStartCatCF opts s
name = cncId s
in slfSubGraphvizPrinter name start s)
,(strCI "fa_graphviz", \opts s -> let start = getStartCatCF opts s
name = cncId s
in faGraphvizPrinter name start s)
,(strCI "fa_c", \opts s -> let start = getStartCatCF opts s
name = cncId s
in faCPrinter name start s)
,(strCI "regexp", \opts s -> let start = getStartCatCF opts s
name = cncId s
in regexpPrinter name start s)
,(strCI "regular", \_ -> regularPrinter)
,(strCI "plbnf", \_ -> prLBNF True) ,(strCI "plbnf", \_ -> prLBNF True)
,(strCI "lbnf", \_ -> prLBNF False) ,(strCI "lbnf", \_ -> prLBNF False)
,(strCI "bnf", \_ -> prBNF False) ,(strCI "bnf", \_ -> prBNF False)
@@ -345,9 +316,6 @@ customGrammarPrinter =
-- ,(strCI "cfg-old", PrtOld.prt . CnvOld.cfg . statePInfoOld) -- ,(strCI "cfg-old", PrtOld.prt . CnvOld.cfg . statePInfoOld)
] ]
where stateGrammarLangOpts s = (stateOptions s, stateGrammarLang s) where stateGrammarLangOpts s = (stateOptions s, stateGrammarLang s)
getStartCat,getStartCatCF :: Options -> StateGrammar -> String
getStartCat opts sgr = prCFCat (startCatStateOpts opts sgr)
getStartCatCF opts sgr = getStartCat opts sgr ++ "{}.s"
customMultiGrammarPrinter = customMultiGrammarPrinter =
customData "Printers for multiple grammars, selected by option -printer=x" $ customData "Printers for multiple grammars, selected by option -printer=x" $