From 06f90d83a1feafad301d365a4a437e44f68be45b Mon Sep 17 00:00:00 2001 From: Victor Chahuneau Date: Sat, 21 Jul 2012 01:22:53 -0400 Subject: [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) --- python/cdec/scfg/extractor.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'python/cdec/scfg') 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) -- cgit v1.2.3