summaryrefslogtreecommitdiff
path: root/python/cdec
diff options
context:
space:
mode:
authorVictor Chahuneau <vchahune@cs.cmu.edu>2012-07-21 01:22:53 -0400
committerVictor Chahuneau <vchahune@cs.cmu.edu>2012-07-21 01:22:53 -0400
commit9a5e10322c82916a3c3fdfa0489ed1999bc988c5 (patch)
tree09de995eae349427d640ed3e392a75e0b9823461 /python/cdec
parent5b47c6aa65cd73e208dd941ec75229af5f9c0c67 (diff)
[python] Support for grammars
- Translation rules can now be create programatically - Grammars = list of translation rules can be used for translation - Feature expectations on the hypergraph (inside_outside)
Diffstat (limited to 'python/cdec')
-rw-r--r--python/cdec/__init__.py3
-rw-r--r--python/cdec/scfg/extractor.py20
2 files changed, 15 insertions, 8 deletions
diff --git a/python/cdec/__init__.py b/python/cdec/__init__.py
index 0d7b8782..89c323ba 100644
--- a/python/cdec/__init__.py
+++ b/python/cdec/__init__.py
@@ -1,2 +1 @@
-from _cdec import Decoder, Lattice
-import score
+from _cdec import Decoder, Lattice, TRule, NT, NTRef
diff --git a/python/cdec/scfg/extractor.py b/python/cdec/scfg/extractor.py
index 0a45ddb8..1dfa2421 100644
--- a/python/cdec/scfg/extractor.py
+++ b/python/cdec/scfg/extractor.py
@@ -1,3 +1,5 @@
+import sys, os
+import re
import StringIO
from itertools import chain
@@ -32,7 +34,16 @@ class PhonyGrammar:
pass
class GrammarExtractor:
- def __init__(self, config):
+ def __init__(self, cfg):
+ if isinstance(cfg, dict):
+ config = cfg
+ elif isinstance(cfg, str):
+ cfg_file = os.path.basename(cfg)
+ if not re.match(r'^\w+\.py$', cfg_file):
+ raise ValueError('Config must be a *.py file')
+ sys.path.append(os.path.dirname(cfg))
+ config = __import__(cfg_file.replace('.py', '')).__dict__
+ sys.path.pop()
alignment = calignment.Alignment(config['a_file'], from_binary=True)
self.factory = rulefactory.HieroCachingRuleFactory(
# compiled alignment object (REQUIRED)
@@ -99,13 +110,10 @@ class GrammarExtractor:
return str(out)
def main(config):
- sys.path.append(os.path.dirname(config))
- module = __import__(os.path.basename(config).replace('.py', ''))
- extractor = GrammarExtractor(module.__dict__)
- print extractor.grammar(next(sys.stdin))
+ extractor = GrammarExtractor(config)
+ sys.stdout.write(extractor.grammar(next(sys.stdin)))
if __name__ == '__main__':
- import sys, os
if len(sys.argv) != 2 or not sys.argv[1].endswith('.py'):
sys.stderr.write('Usage: %s config.py\n' % sys.argv[0])
sys.exit(1)