mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-23 03:32:51 -06:00
Use makeSimpleSRG everywhere and remove makeSRG. Reimplemented top-down filtering in terms of CFRules instead of SRG. Do top-down filtering in makeSimpleSRG.
This commit is contained in:
@@ -32,7 +32,7 @@ import GF.Compile.ShellState (StateGrammar)
|
|||||||
import Data.Char (toUpper,toLower)
|
import Data.Char (toUpper,toLower)
|
||||||
|
|
||||||
gslPrinter :: Options -> StateGrammar -> String
|
gslPrinter :: Options -> StateGrammar -> String
|
||||||
gslPrinter opts s = prGSL $ topDownFilter $ makeSimpleSRG opts s
|
gslPrinter opts s = prGSL $ makeSimpleSRG opts s
|
||||||
|
|
||||||
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})
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ srgsXmlPrinter :: Maybe SISRFormat
|
|||||||
-> Bool -- ^ Include probabilities
|
-> Bool -- ^ Include probabilities
|
||||||
-> Options
|
-> Options
|
||||||
-> StateGrammar -> String
|
-> StateGrammar -> String
|
||||||
srgsXmlPrinter sisr probs opts s = prSrgsXml sisr probs $ makeSRG opts s
|
srgsXmlPrinter sisr probs opts s = prSrgsXml sisr probs $ makeSimpleSRG 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,
|
||||||
|
|||||||
@@ -20,9 +20,9 @@
|
|||||||
|
|
||||||
module GF.Speech.SRG (SRG(..), SRGRule(..), SRGAlt(..),
|
module GF.Speech.SRG (SRG(..), SRGRule(..), SRGAlt(..),
|
||||||
SRGCat, SRGNT, CFTerm,
|
SRGCat, SRGNT, CFTerm,
|
||||||
makeSimpleSRG, makeSRG
|
makeSimpleSRG
|
||||||
, lookupFM_, prtS
|
, lookupFM_, prtS
|
||||||
, topDownFilter, cfgCatToGFCat, srgTopCats
|
, cfgCatToGFCat, srgTopCats
|
||||||
, EBnfSRGAlt(..), EBnfSRGItem
|
, EBnfSRGAlt(..), EBnfSRGItem
|
||||||
, ebnfSRGAlts
|
, ebnfSRGAlts
|
||||||
) where
|
) where
|
||||||
@@ -79,28 +79,12 @@ type CatName = (SRGCat,String)
|
|||||||
type CatNames = Map String String
|
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 in the returned
|
||||||
-- grammar may be meaningless.
|
-- grammar may be meaningless.
|
||||||
makeSimpleSRG :: Options -- ^ Grammar options
|
makeSimpleSRG :: Options -- ^ Grammar options
|
||||||
-> StateGrammar
|
-> StateGrammar
|
||||||
-> SRG
|
-> SRG
|
||||||
makeSimpleSRG opts s =
|
makeSimpleSRG opt s =
|
||||||
makeSRG_ (removeLeftRecursion origStart . removeIdenticalRules
|
|
||||||
. removeEmptyCats . removeCycles) opts s
|
|
||||||
where origStart = getStartCatCF opts s
|
|
||||||
|
|
||||||
-- | Create a SRG preserving the names, profiles and probabilities of the
|
|
||||||
-- input grammar. The returned grammar may be left-recursive.
|
|
||||||
makeSRG :: Options -- ^ Grammar options
|
|
||||||
-> StateGrammar
|
|
||||||
-> SRG
|
|
||||||
makeSRG = makeSRG_ removeEmptyCats
|
|
||||||
|
|
||||||
makeSRG_ :: (CFRules -> CFRules)
|
|
||||||
-> Options -- ^ Grammar options
|
|
||||||
-> StateGrammar
|
|
||||||
-> SRG
|
|
||||||
makeSRG_ preprocess opt s =
|
|
||||||
SRG { grammarName = name,
|
SRG { grammarName = name,
|
||||||
startCat = lookupFM_ names origStart,
|
startCat = lookupFM_ names origStart,
|
||||||
origStartCat = origStart,
|
origStartCat = origStart,
|
||||||
@@ -113,6 +97,11 @@ makeSRG_ preprocess opt s =
|
|||||||
probs = stateProbs s
|
probs = stateProbs s
|
||||||
l = fmap (replace '_' '-') $ getOptVal opts speechLanguage
|
l = fmap (replace '_' '-') $ getOptVal opts speechLanguage
|
||||||
(cats,cfgRules) = unzip $ preprocess $ cfgToCFRules s
|
(cats,cfgRules) = unzip $ preprocess $ cfgToCFRules s
|
||||||
|
preprocess = removeLeftRecursion origStart
|
||||||
|
. removeEmptyCats
|
||||||
|
. topDownFilter origStart
|
||||||
|
. removeIdenticalRules
|
||||||
|
. removeCycles
|
||||||
names = mkCatNames name cats
|
names = mkCatNames name cats
|
||||||
rs = map (cfgRulesToSRGRule names probs) cfgRules
|
rs = map (cfgRulesToSRGRule names probs) cfgRules
|
||||||
|
|
||||||
@@ -145,17 +134,6 @@ mkCatNames prefix origNames = Map.fromList (zip origNames names)
|
|||||||
where names = [prefix ++ "_" ++ show x | x <- [0..]]
|
where names = [prefix ++ "_" ++ show x | x <- [0..]]
|
||||||
|
|
||||||
|
|
||||||
-- | Remove categories which are not reachable from the start category.
|
|
||||||
topDownFilter :: SRG -> SRG
|
|
||||||
topDownFilter srg@(SRG { startCat = start, rules = rs }) = srg { rules = rs' }
|
|
||||||
where
|
|
||||||
rs' = [ r | r@(SRGRule c _ _) <- rs, c `Set.member` keep]
|
|
||||||
rhsCats = [ (c,c') | r@(SRGRule c _ ps) <- rs,
|
|
||||||
SRGAlt _ _ ss <- ps,
|
|
||||||
(c',_) <- filterCats ss]
|
|
||||||
uses = reflexiveClosure_ (allSRGCats srg) $ transitiveClosure $ mkRel rhsCats
|
|
||||||
keep = allRelated uses start
|
|
||||||
|
|
||||||
allSRGCats :: SRG -> [String]
|
allSRGCats :: SRG -> [String]
|
||||||
allSRGCats SRG { rules = rs } = [c | SRGRule c _ _ <- rs]
|
allSRGCats SRG { rules = rs } = [c | SRGRule c _ _ <- rs]
|
||||||
|
|
||||||
|
|||||||
@@ -227,6 +227,15 @@ removeCycles :: CFRules -> CFRules
|
|||||||
removeCycles = groupProds . removeCycles_ . ungroupProds
|
removeCycles = groupProds . removeCycles_ . ungroupProds
|
||||||
where removeCycles_ rs = [r | r@(CFRule c rhs _) <- rs, rhs /= [Cat c]]
|
where removeCycles_ rs = [r | r@(CFRule c rhs _) <- rs, rhs /= [Cat c]]
|
||||||
|
|
||||||
|
-- * Top-down filtering
|
||||||
|
|
||||||
|
-- | Remove categories which are not reachable from the start category.
|
||||||
|
topDownFilter :: Cat_ -> CFRules -> CFRules
|
||||||
|
topDownFilter start rules = filter ((`Set.member` keep) . fst) rules
|
||||||
|
where
|
||||||
|
rhsCats = [ (c, c') | (c,rs) <- rules, r <- rs, c' <- filterCats (ruleRhs r) ]
|
||||||
|
uses = reflexiveClosure_ (allCats rules) $ transitiveClosure $ mkRel rhsCats
|
||||||
|
keep = allRelated uses start
|
||||||
|
|
||||||
-- | Get the sets of mutually recursive non-terminals for a grammar.
|
-- | Get the sets of mutually recursive non-terminals for a grammar.
|
||||||
mutRecCats :: Bool -- ^ If true, all categories will be in some set.
|
mutRecCats :: Bool -- ^ If true, all categories will be in some set.
|
||||||
|
|||||||
Reference in New Issue
Block a user