---------------------------------------------------------------------- -- | -- Module : PrJSGF -- Maintainer : BB -- Stability : (stable) -- Portability : (portable) -- -- > CVS $Date: 2005/11/01 20:09:04 $ -- > CVS $Author: bringert $ -- > CVS $Revision: 1.16 $ -- -- This module prints a CFG as a JSGF grammar. -- -- FIXME: remove \/ warn \/ fail if there are int \/ string literal -- categories in the grammar -- -- FIXME: convert to UTF-8 ----------------------------------------------------------------------------- module GF.Speech.PrJSGF (jsgfPrinter) where import GF.Conversion.Types import GF.Data.Utilities import GF.Formalism.CFG import GF.Formalism.Utilities (Symbol(..)) import GF.Infra.Ident import GF.Infra.Print import GF.Infra.Option import GF.Probabilistic.Probabilistic (Probs) import GF.Speech.SRG jsgfPrinter :: Ident -- ^ Grammar name -> Options -> Maybe Probs -> CGrammar -> String jsgfPrinter name opts probs cfg = prJSGF srg "" where srg = makeSRG name opts probs cfg prJSGF :: SRG -> ShowS prJSGF (SRG{grammarName=name,startCat=start,origStartCat=origStart,rules=rs}) = header . mainCat . unlinesS (map prRule rs) where header = showString "#JSGF V1.0 UTF-8;" . nl . comments ["JSGF speech recognition grammar for " ++ name, "Generated by GF"] . nl . showString ("grammar " ++ name ++ ";") . nl . nl mainCat = comments ["Start category: " ++ origStart] . nl . showString "public
= " . prCat start . showChar ';' . nl . nl prRule (SRGRule cat origCat rhs) = comments [origCat] . nl . prCat cat . showString " = " . joinS " | " (map prAlt rhs) . nl -- FIXME: use the probability prAlt (SRGAlt mp rhs) | null rhs' = showString "" | otherwise = wrap "(" (unwordsS (map prSymbol rhs')) ")" where rhs' = rmPunct rhs prSymbol (Cat c) = prCat c prSymbol (Tok t) = wrap "\"" (prtS t) "\"" prCat c = showChar '<' . showString c . showChar '>' rmPunct :: [Symbol String Token] -> [Symbol String Token] rmPunct [] = [] rmPunct (Tok t:ss) | all isPunct (prt t) = rmPunct ss rmPunct (s:ss) = s : rmPunct ss isPunct :: Char -> Bool isPunct c = c `elem` "-_.;.,?!" comments :: [String] -> ShowS comments = unlinesS . map (showString . ("// " ++))