Commit d72c69d5 by Long Duong

Add MT Visualization

parent 2288eecd
......@@ -11,6 +11,8 @@ from __future__ import print_function, unicode_literals
from nltk.compat import python_2_unicode_compatible, string_types
from nltk.metrics import precision, recall
import subprocess
@python_2_unicode_compatible
class AlignedSent(object):
......@@ -92,6 +94,56 @@ class AlignedSent(object):
return "AlignedSent(%s, %s, %r)" % (words, mots, self._alignment)
def _to_dot(self):
"""
Dot representation of the aligned sentence
"""
s = 'graph align {\n'
s += 'node[shape=plaintext]\n'
# Declare node
for w in self._words:
s += '"%s_source" [label="%s"] \n' % (w, w)
for w in self._mots:
s += '"%s_target" [label="%s"] \n' % (w, w)
# Alignment
for u,v in self._alignment:
s += '"%s_source" -- "%s_target" \n' % (self._words[u] , self._mots[v] )
# Connect the source words
for i in range(len(self._words)-1) :
s += '"%s_source" -- "%s_source" [style=invis]\n' % (self._words[i] , self._words[i+1])
# Connect the target words
for i in range(len(self._mots)-1) :
s += '"%s_target" -- "%s_target" [style=invis]\n' % (self._mots[i] , self._mots[i+1])
# Put it in the same rank
s += '{rank = same; %s}\n' % (' '.join('"%s_source"' % w for w in self._words))
s += '{rank = same; %s}\n' % (' '.join('"%s_target"' % w for w in self._mots))
s += '}'
return s
def _repr_svg_(self):
"""
Ipython magic : show SVG representation of this ``AlignedSent``.
"""
dot_string = self._to_dot().encode('utf8')
output_format = 'svg'
try:
process = subprocess.Popen(['dot', '-T%s' % output_format], stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except OSError:
raise Exception('Cannot find the dot binary from Graphviz package')
out, err = process.communicate(dot_string)
return out
def __str__(self):
"""
Return a human-readable string representation for this ``AlignedSent``.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment