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 | 06f90d83a1feafad301d365a4a437e44f68be45b (patch) | |
tree | 24128de1cb5a4767151f9380c46104a26121535d /python/cdec/scfg | |
parent | c4c9c2febd5af552ecddc215758e32b88013fbc7 (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) |