forked from GitHub/gf-core
Added top-down filtering to the GSL printer.
This commit is contained in:
@@ -33,7 +33,7 @@ import Data.Char (toUpper,toLower)
|
|||||||
gslPrinter :: Ident -- ^ Grammar name
|
gslPrinter :: Ident -- ^ Grammar name
|
||||||
-> Options -> Maybe Probs -> CGrammar -> String
|
-> Options -> Maybe Probs -> CGrammar -> String
|
||||||
gslPrinter name opts probs cfg = prGSL srg ""
|
gslPrinter name opts probs cfg = prGSL srg ""
|
||||||
where srg = makeSimpleSRG name opts probs $ rmPunctCFG cfg
|
where srg = topDownFilter $ makeSimpleSRG name opts probs $ rmPunctCFG cfg
|
||||||
|
|
||||||
prGSL :: SRG -> ShowS
|
prGSL :: SRG -> ShowS
|
||||||
prGSL (SRG{grammarName=name,startCat=start,origStartCat=origStart,rules=rs})
|
prGSL (SRG{grammarName=name,startCat=start,origStartCat=origStart,rules=rs})
|
||||||
|
|||||||
@@ -13,7 +13,7 @@
|
|||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
|
|
||||||
module GF.Speech.Relation (Rel, mkRel
|
module GF.Speech.Relation (Rel, mkRel
|
||||||
, isRelatedTo
|
, allRelated , isRelatedTo
|
||||||
, transitiveClosure
|
, transitiveClosure
|
||||||
, reflexiveClosure, reflexiveClosure_
|
, reflexiveClosure, reflexiveClosure_
|
||||||
, symmetricClosure
|
, symmetricClosure
|
||||||
|
|||||||
@@ -20,17 +20,19 @@
|
|||||||
|
|
||||||
module GF.Speech.SRG (SRG(..), SRGRule(..), SRGAlt(..),
|
module GF.Speech.SRG (SRG(..), SRGRule(..), SRGAlt(..),
|
||||||
makeSimpleSRG, makeSRG
|
makeSimpleSRG, makeSRG
|
||||||
, lookupFM_, prtS) where
|
, lookupFM_, prtS
|
||||||
|
, topDownFilter) where
|
||||||
|
|
||||||
import GF.Data.Operations
|
import GF.Data.Operations
|
||||||
import GF.Data.Utilities
|
import GF.Data.Utilities
|
||||||
import GF.Infra.Ident
|
import GF.Infra.Ident
|
||||||
import GF.Formalism.CFG
|
import GF.Formalism.CFG
|
||||||
import GF.Formalism.Utilities (Symbol(..), NameProfile(..)
|
import GF.Formalism.Utilities (Symbol(..), NameProfile(..)
|
||||||
, Profile, SyntaxForest)
|
, Profile, SyntaxForest, filterCats)
|
||||||
import GF.Conversion.Types
|
import GF.Conversion.Types
|
||||||
import GF.Infra.Print
|
import GF.Infra.Print
|
||||||
import GF.Speech.TransformCFG
|
import GF.Speech.TransformCFG
|
||||||
|
import GF.Speech.Relation
|
||||||
import GF.Infra.Option
|
import GF.Infra.Option
|
||||||
import GF.Probabilistic.Probabilistic (Probs)
|
import GF.Probabilistic.Probabilistic (Probs)
|
||||||
|
|
||||||
@@ -38,6 +40,8 @@ import Data.List
|
|||||||
import Data.Maybe (fromMaybe)
|
import Data.Maybe (fromMaybe)
|
||||||
import Data.Map (Map)
|
import Data.Map (Map)
|
||||||
import qualified Data.Map as Map
|
import qualified Data.Map as Map
|
||||||
|
import Data.Set (Set)
|
||||||
|
import qualified Data.Set as Set
|
||||||
|
|
||||||
data SRG = SRG { grammarName :: String -- ^ grammar name
|
data SRG = SRG { grammarName :: String -- ^ grammar name
|
||||||
, startCat :: String -- ^ start category name
|
, startCat :: String -- ^ start category name
|
||||||
@@ -127,6 +131,17 @@ mkCatNames :: String -- ^ Category name prefix
|
|||||||
mkCatNames prefix origNames = Map.fromList (zip origNames names)
|
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]
|
||||||
|
keep = allRelated (transitiveClosure $ mkRel rhsCats) start
|
||||||
|
|
||||||
--
|
--
|
||||||
-- * Utilities for building and printing SRGs
|
-- * Utilities for building and printing SRGs
|
||||||
--
|
--
|
||||||
|
|||||||
Reference in New Issue
Block a user