diff options
author | Victor Chahuneau <vchahune@cs.cmu.edu> | 2012-07-21 01:22:53 -0400 |
---|---|---|
committer | Victor Chahuneau <vchahune@cs.cmu.edu> | 2012-07-21 01:22:53 -0400 |
commit | 9a5e10322c82916a3c3fdfa0489ed1999bc988c5 (patch) | |
tree | 09de995eae349427d640ed3e392a75e0b9823461 /python/cdec/scfg | |
parent | 5b47c6aa65cd73e208dd941ec75229af5f9c0c67 (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/scfg')
-rw-r--r-- | python/cdec/scfg/extractor.py | 20 |
1 files changed, 14 insertions, 6 deletions
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) |