diff options
author | Patrick Simianer <simianer@cl.uni-heidelberg.de> | 2012-06-13 14:42:07 +0200 |
---|---|---|
committer | Patrick Simianer <simianer@cl.uni-heidelberg.de> | 2012-06-13 14:42:07 +0200 |
commit | e6d3c25191873ca0cf99db8e89702ed91d65277c (patch) | |
tree | b0697ece6f5e4a8229915758c68750793a23f776 /python/test.py | |
parent | 62c805c90c5347b844f92574e240db5c65578e12 (diff) | |
parent | 3acdf1e4b37637d6df86a7b54fb0f1b0464c172b (diff) |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'python/test.py')
-rw-r--r-- | python/test.py | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/python/test.py b/python/test.py new file mode 100644 index 00000000..1542dd4f --- /dev/null +++ b/python/test.py @@ -0,0 +1,49 @@ +#coding: utf8 +import cdec +import gzip + +config = 'formalism=scfg' +weights = '../tests/system_tests/australia/weights' +grammar_file = '../tests/system_tests/australia/australia.scfg.gz' + +# Load decoder width configuration +decoder = cdec.Decoder(config) +# Read weights +decoder.read_weights(weights) + +print dict(decoder.weights) + +# Read grammar +with gzip.open(grammar_file) as f: + grammar = f.read() + +# Input sentence +sentence = u'澳洲 是 与 北韩 有 邦交 的 少数 国家 之一 。' +print 'Input:', sentence + +# Decode +forest = decoder.translate(sentence, grammar=grammar) + +# Get viterbi translation +print 'Output[0]:', forest.viterbi().encode('utf8') +print ' Tree[0]:', forest.viterbi_tree().encode('utf8') + +# Get k-best translations +for i, (sentence, tree) in enumerate(zip(forest.kbest(5), forest.kbest_tree(5)), 1): + print 'Output[%d]:' % i, sentence.encode('utf8') + print ' Tree[%d]:' % i, tree.encode('utf8') + +# Sample translations from the forest +for sentence in forest.sample(5): + print 'Sample:', sentence.encode('utf8') + +# Reference lattice +lattice = ((('australia',0,1),),(('is',0,1),),(('one',0,1),),(('of',0,1),),(('the',0,4),('a',0,4),('a',0,1),('the',0,1),),(('small',0,1),('tiny',0,1),('miniscule',0,1),('handful',0,2),),(('number',0,1),('group',0,1),),(('of',0,2),),(('few',0,1),),(('countries',0,1),),(('that',0,1),),(('has',0,1),('have',0,1),),(('diplomatic',0,1),),(('relations',0,1),),(('with',0,1),),(('north',0,1),),(('korea',0,1),),(('.',0,1),),) + +lat = cdec.Lattice(lattice) +assert (lattice == tuple(lat)) + +# Intersect forest and lattice +forest.intersect(lat) +# Get best synchronous parse +print forest.viterbi_tree() |