summaryrefslogtreecommitdiff
path: root/src/GF/Speech/PrJSGF.hs
diff options
context:
space:
mode:
authorbringert <unknown>2004-10-01 08:43:59 +0000
committerbringert <unknown>2004-10-01 08:43:59 +0000
commitf64502d4a7016864990432fd7135157c1d29ef0b (patch)
tree9ed90c4246229f6a8695694c5468d5647dea3bb7 /src/GF/Speech/PrJSGF.hs
parent653c903dc782ac39ea4e42daee0154f1d9fc9e8d (diff)
Generalized Speech Recognition Grammar generation. Added JSGF grammar printer.
Diffstat (limited to 'src/GF/Speech/PrJSGF.hs')
-rw-r--r--src/GF/Speech/PrJSGF.hs67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/GF/Speech/PrJSGF.hs b/src/GF/Speech/PrJSGF.hs
new file mode 100644
index 000000000..4c849e8c8
--- /dev/null
+++ b/src/GF/Speech/PrJSGF.hs
@@ -0,0 +1,67 @@
+{-
+ **************************************************************
+ GF Module
+
+ Description : This module prints a CFG as a JSGF grammar.
+
+ Author : Björn Bringert (bringert@cs.chalmers.se)
+
+ License : GPL (GNU General Public License)
+
+ Created : October 1, 2004
+
+ Modified :
+ **************************************************************
+-}
+
+-- FIXME: remove / warn / fail if there are int / string literal
+-- categories in the grammar
+
+-- FIXME: convert to UTF-8
+
+module PrJSGF (jsgfPrinter) where
+
+import SRG
+import Ident
+import CFGrammar
+import Parser (Symbol(..))
+import GrammarTypes
+import PrintParser
+import Option
+
+jsgfPrinter :: Ident -- ^ Grammar name
+ -> Options -> CFGrammar -> String
+jsgfPrinter name opts cfg = prJSGF srg ""
+ where srg = makeSRG name opts 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 <MAIN> = " . prCat start . showChar ';' . nl . nl
+ prRule (SRGRule cat origCat rhs) =
+ comments [origCat] . nl
+ . prCat cat . showString " = " . join " | " (map prAlt rhs) . nl
+ prAlt rhs | null rhs' = showString "<NULL>"
+ | 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 . ("// " ++))