diff options
| author | peb <unknown> | 2005-04-14 17:38:36 +0000 |
|---|---|---|
| committer | peb <unknown> | 2005-04-14 17:38:36 +0000 |
| commit | 5207c27bffd077f1ab322824b485fc6c92d554eb (patch) | |
| tree | f8a54bde5d80925f5590755859ad31c2e6f9ac3d /src/GF/Speech | |
| parent | f070a412a1256b39e60b3a819e18c61922a7fe79 (diff) | |
"Committed_by_peb"
Diffstat (limited to 'src/GF/Speech')
| -rw-r--r-- | src/GF/Speech/PrGSL.hs | 22 | ||||
| -rw-r--r-- | src/GF/Speech/PrJSGF.hs | 20 | ||||
| -rw-r--r-- | src/GF/Speech/SRG.hs | 28 | ||||
| -rw-r--r-- | src/GF/Speech/TransformCFG.hs | 58 |
4 files changed, 74 insertions, 54 deletions
diff --git a/src/GF/Speech/PrGSL.hs b/src/GF/Speech/PrGSL.hs index d59412ebd..84e3f2a74 100644 --- a/src/GF/Speech/PrGSL.hs +++ b/src/GF/Speech/PrGSL.hs @@ -5,9 +5,9 @@ -- Stability : (stable) -- Portability : (portable) -- --- > CVS $Date: 2005/04/11 13:53:38 $ +-- > CVS $Date: 2005/04/14 18:38:36 $ -- > CVS $Author: peb $ --- > CVS $Revision: 1.15 $ +-- > CVS $Revision: 1.16 $ -- -- This module prints a CFG as a Nuance GSL 2.0 grammar. -- @@ -19,16 +19,20 @@ module PrGSL (gslPrinter) where import SRG import Ident -import GF.OldParsing.CFGrammar -import GF.OldParsing.Utilities (Symbol(..)) -import GF.OldParsing.GrammarTypes -import GF.Printing.PrintParser +-- import GF.OldParsing.CFGrammar +-- import GF.OldParsing.Utilities (Symbol(..)) +-- import GF.OldParsing.GrammarTypes +-- import GF.Printing.PrintParser +import GF.Formalism.CFG +import GF.Formalism.Utilities (Symbol(..)) +import GF.Conversion.Types +import GF.Infra.Print import Option import Data.Char (toUpper,toLower) gslPrinter :: Ident -- ^ Grammar name - -> Options -> CFGrammar -> String + -> Options -> CGrammar -> String gslPrinter name opts cfg = prGSL srg "" where srg = makeSRG name opts cfg @@ -55,13 +59,13 @@ firstToUpper :: String -> String firstToUpper [] = [] firstToUpper (x:xs) = toUpper x : xs -rmPunct :: [Symbol String Tokn] -> [Symbol String Tokn] +rmPunct :: [Symbol String Token] -> [Symbol String Token] rmPunct [] = [] rmPunct (Tok t:ss) | all isPunct (prt t) = rmPunct ss rmPunct (s:ss) = s : rmPunct ss -- Nuance does not like upper case characters in tokens -showToken :: Tokn -> String +showToken :: Token -> String showToken t = map toLower (prt t) isPunct :: Char -> Bool diff --git a/src/GF/Speech/PrJSGF.hs b/src/GF/Speech/PrJSGF.hs index 9562ff5ac..975685d81 100644 --- a/src/GF/Speech/PrJSGF.hs +++ b/src/GF/Speech/PrJSGF.hs @@ -5,9 +5,9 @@ -- Stability : (stable) -- Portability : (portable) -- --- > CVS $Date: 2005/04/11 13:53:39 $ +-- > CVS $Date: 2005/04/14 18:38:36 $ -- > CVS $Author: peb $ --- > CVS $Revision: 1.9 $ +-- > CVS $Revision: 1.10 $ -- -- This module prints a CFG as a JSGF grammar. -- @@ -21,14 +21,18 @@ module PrJSGF (jsgfPrinter) where import SRG import Ident -import GF.OldParsing.CFGrammar -import GF.OldParsing.Utilities (Symbol(..)) -import GF.OldParsing.GrammarTypes -import GF.Printing.PrintParser +-- import GF.OldParsing.CFGrammar +-- import GF.OldParsing.Utilities (Symbol(..)) +-- import GF.OldParsing.GrammarTypes +-- import GF.Printing.PrintParser +import GF.Formalism.CFG +import GF.Formalism.Utilities (Symbol(..)) +import GF.Conversion.Types +import GF.Infra.Print import Option jsgfPrinter :: Ident -- ^ Grammar name - -> Options -> CFGrammar -> String + -> Options -> CGrammar -> String jsgfPrinter name opts cfg = prJSGF srg "" where srg = makeSRG name opts cfg @@ -53,7 +57,7 @@ prJSGF (SRG{grammarName=name,startCat=start,origStartCat=origStart,rules=rs}) prSymbol (Tok t) = wrap "\"" (prtS t) "\"" prCat c = showChar '<' . showString c . showChar '>' -rmPunct :: [Symbol String Tokn] -> [Symbol String Tokn] +rmPunct :: [Symbol String Token] -> [Symbol String Token] rmPunct [] = [] rmPunct (Tok t:ss) | all isPunct (prt t) = rmPunct ss rmPunct (s:ss) = s : rmPunct ss diff --git a/src/GF/Speech/SRG.hs b/src/GF/Speech/SRG.hs index 9ec684295..e1ac0efc4 100644 --- a/src/GF/Speech/SRG.hs +++ b/src/GF/Speech/SRG.hs @@ -5,9 +5,9 @@ -- Stability : (stable) -- Portability : (portable) -- --- > CVS $Date: 2005/04/11 13:53:39 $ +-- > CVS $Date: 2005/04/14 18:38:36 $ -- > CVS $Author: peb $ --- > CVS $Revision: 1.11 $ +-- > CVS $Revision: 1.12 $ -- -- Representation of, conversion to, and utilities for -- printing of a general Speech Recognition Grammar. @@ -21,10 +21,14 @@ module SRG where import Ident -import GF.OldParsing.CFGrammar -import GF.OldParsing.Utilities (Symbol(..)) -import GF.OldParsing.GrammarTypes -import GF.Printing.PrintParser +-- import GF.OldParsing.CFGrammar +-- import GF.OldParsing.Utilities (Symbol(..)) +-- import GF.OldParsing.GrammarTypes +-- import GF.Printing.PrintParser +import GF.Formalism.CFG +import GF.Formalism.Utilities (Symbol(..)) +import GF.Conversion.Types +import GF.Infra.Print import TransformCFG import Option @@ -40,7 +44,7 @@ data SRG = SRG { grammarName :: String -- ^ grammar name } data SRGRule = SRGRule String String [SRGAlt] -- ^ SRG category name, original category name -- and productions -type SRGAlt = [Symbol String Tokn] +type SRGAlt = [Symbol String Token] -- | SRG category name and original name type CatName = (String,String) @@ -49,7 +53,7 @@ type CatNames = FiniteMap String String makeSRG :: Ident -- ^ Grammar name -> Options -- ^ Grammar options - -> CFGrammar -- ^ A context-free grammar + -> CGrammar -- ^ A context-free grammar -> SRG makeSRG i opts gr = SRG { grammarName = name, startCat = start, @@ -71,11 +75,11 @@ cfgRulesToSRGRule names rs@(r:_) = SRGRule cat origCat rhs renameCat (Cat c) = Cat (lookupFM_ names c) renameCat t = t -ruleCat :: Rule n c t -> c -ruleCat (Rule c _ _) = c +ruleCat :: CFRule c n t -> c +ruleCat (CFRule c _ _) = c -ruleRhs :: Rule n c t -> [Symbol c t] -ruleRhs (Rule _ r _) = r +ruleRhs :: CFRule c n t -> [Symbol c t] +ruleRhs (CFRule _ r _) = r mkCatNames :: String -- ^ Category name prefix -> [String] -- ^ Original category names diff --git a/src/GF/Speech/TransformCFG.hs b/src/GF/Speech/TransformCFG.hs index 8dd81cb91..6a1b7c817 100644 --- a/src/GF/Speech/TransformCFG.hs +++ b/src/GF/Speech/TransformCFG.hs @@ -5,22 +5,28 @@ -- Stability : (stable) -- Portability : (portable) -- --- > CVS $Date: 2005/04/11 13:53:39 $ +-- > CVS $Date: 2005/04/14 18:38:36 $ -- > CVS $Author: peb $ --- > CVS $Revision: 1.9 $ +-- > CVS $Revision: 1.10 $ -- -- This module does some useful transformations on CFGs. -- -- FIXME: remove cycles +-- +-- peb thinks: most of this module should be moved to GF.Conversion... ----------------------------------------------------------------------------- module TransformCFG (makeNice, CFRule_) where import Ident -import GF.OldParsing.CFGrammar -import GF.OldParsing.Utilities (Symbol(..)) -import GF.OldParsing.GrammarTypes -import GF.Printing.PrintParser +-- import GF.OldParsing.CFGrammar +-- import GF.OldParsing.Utilities (Symbol(..)) +-- import GF.OldParsing.GrammarTypes +-- import GF.Printing.PrintParser +import GF.Formalism.CFG +import GF.Formalism.Utilities (Symbol(..), mapSymbol) +import GF.Conversion.Types +import GF.Infra.Print import Data.FiniteMap import Data.List @@ -30,63 +36,65 @@ import Debug.Trace -- | not very nice to get replace the structured CFCat type with a simple string -type CFRule_ = Rule CFName String Tokn +type CFRule_ = CFRule Cat_ Name Token +type Cat_ = String -type CFRules = FiniteMap String [CFRule_] +type CFRules = FiniteMap Cat_ [CFRule_] -makeNice :: CFGrammar -> [CFRule_] +makeNice :: CGrammar -> [CFRule_] makeNice = concat . eltsFM . makeNice' . groupProds . cfgToCFRules where makeNice' = removeLeftRecursion . removeEmptyCats -cfgToCFRules :: CFGrammar -> [CFRule_] -cfgToCFRules cfg = [Rule (catToString c) (map symb r) n | Rule c r n <- cfg] - where symb (Cat c) = Cat (catToString c) - symb (Tok t) = Tok t +cfgToCFRules :: CGrammar -> [CFRule_] +cfgToCFRules cfg = [CFRule (catToString c) (map symb r) n | CFRule c r n <- cfg] + where symb = mapSymbol catToString id + -- symb (Cat c) = Cat (catToString c) + -- symb (Tok t) = Tok t catToString = prt -- | Group productions by their lhs categories groupProds :: [CFRule_] -> CFRules groupProds = addListToFM_C (++) emptyFM . map (\rs -> (ruleCat rs,[rs])) - where ruleCat (Rule c _ _) = c + where ruleCat (CFRule c _ _) = c -- | Remove productions which use categories which have no productions removeEmptyCats :: CFRules -> CFRules removeEmptyCats rss = listToFM $ fix removeEmptyCats' $ fmToList rss where - removeEmptyCats' :: [(String,[CFRule_])] -> [(String,[CFRule_])] + removeEmptyCats' :: [(Cat_,[CFRule_])] -> [(Cat_,[CFRule_])] removeEmptyCats' rs = k' where keep = filter (not . null . snd) rs - allCats = nub [c | (_,r) <- rs, Rule _ rhs _ <- r, Cat c <- rhs] + allCats = nub [c | (_,r) <- rs, CFRule _ rhs _ <- r, Cat c <- rhs] emptyCats = filter (nothingOrNull . flip lookup rs) allCats k' = map (\ (c,xs) -> (c, filter (not . anyUsedBy emptyCats) xs)) keep -anyUsedBy :: [String] -> CFRule_ -> Bool -anyUsedBy ss (Rule _ r _) = or [c `elem` ss | Cat c <- r] +anyUsedBy :: [Cat_] -> CFRule_ -> Bool +anyUsedBy ss (CFRule _ r _) = or [c `elem` ss | Cat c <- r] removeLeftRecursion :: CFRules -> CFRules removeLeftRecursion rs = listToFM $ concatMap removeDirectLeftRecursion $ map handleProds $ fmToList rs where handleProds (c, r) = (c, concatMap handleProd r) - handleProd (Rule ai (Cat aj:alpha) n) | aj < ai = + handleProd (CFRule ai (Cat aj:alpha) n) | aj < ai = -- FIXME: this will give multiple rules with the same name - [Rule ai (beta ++ alpha) n | Rule _ beta _ <- fromJust (lookupFM rs aj)] + [CFRule ai (beta ++ alpha) n | CFRule _ beta _ <- fromJust (lookupFM rs aj)] handleProd r = [r] -removeDirectLeftRecursion :: (String,[CFRule_]) -- ^ All productions for a category - -> [(String,[CFRule_])] +removeDirectLeftRecursion :: (Cat_,[CFRule_]) -- ^ All productions for a category + -> [(Cat_,[CFRule_])] removeDirectLeftRecursion (a,rs) | null dr = [(a,rs)] | otherwise = [(a, as), (a', a's)] where a' = a ++ "'" -- FIXME: this might not be unique (dr,nr) = partition isDirectLeftRecursive rs as = maybeEndWithA' nr - is = [Rule a' (tail r) n | Rule _ r n <- dr] + is = [CFRule a' (tail r) n | CFRule _ r n <- dr] a's = maybeEndWithA' is - maybeEndWithA' xs = xs ++ [Rule c (r++[Cat a']) n | Rule c r n <- xs] + maybeEndWithA' xs = xs ++ [CFRule c (r++[Cat a']) n | CFRule c r n <- xs] isDirectLeftRecursive :: CFRule_ -> Bool -isDirectLeftRecursive (Rule c (Cat c':_) _) = c == c' +isDirectLeftRecursive (CFRule c (Cat c':_) _) = c == c' isDirectLeftRecursive _ = False |
