summaryrefslogtreecommitdiff
path: root/python/pkg/cdec/sa/extract.py
diff options
context:
space:
mode:
authorPatrick Simianer <simianer@cl.uni-heidelberg.de>2012-08-01 17:32:37 +0200
committerPatrick Simianer <simianer@cl.uni-heidelberg.de>2012-08-01 17:32:37 +0200
commiteb3ea4fd5dff1c94b237af792c9f7bf421d79d96 (patch)
tree2acd7674f36e6dc6e815c5856519fdea1a2d6bf8 /python/pkg/cdec/sa/extract.py
parente816274e337a066df1b1e86ef00136a021a17caf (diff)
parent193d137056c3c4f73d66f8db84691d63307de894 (diff)
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'python/pkg/cdec/sa/extract.py')
-rw-r--r--python/pkg/cdec/sa/extract.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/python/pkg/cdec/sa/extract.py b/python/pkg/cdec/sa/extract.py
new file mode 100644
index 00000000..918aa3bb
--- /dev/null
+++ b/python/pkg/cdec/sa/extract.py
@@ -0,0 +1,31 @@
+#!/usr/bin/env python
+import sys
+import os
+import argparse
+import logging
+import cdec.sa
+
+def main():
+ logging.basicConfig(level=logging.INFO)
+ parser = argparse.ArgumentParser(description='Extract grammars from a compiled corpus.')
+ parser.add_argument('-c', '--config', required=True,
+ help='Extractor configuration')
+ parser.add_argument('-g', '--grammars', required=True,
+ help='Grammar output path')
+ args = parser.parse_args()
+
+ if not os.path.exists(args.grammars):
+ os.mkdir(args.grammars)
+
+ extractor = cdec.sa.GrammarExtractor(args.config)
+ for i, sentence in enumerate(sys.stdin):
+ sentence = sentence[:-1]
+ grammar_file = os.path.join(args.grammars, 'grammar.{0}'.format(i))
+ with open(grammar_file, 'w') as output:
+ for rule in extractor.grammar(sentence):
+ output.write(str(rule)+'\n')
+ grammar_file = os.path.abspath(grammar_file)
+ print('<seg grammar="{0}">{1}</seg>'.format(grammar_file, sentence))
+
+if __name__ == '__main__':
+ main()