Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nltk
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
nltk
Commits
d72c69d5
Commit
d72c69d5
authored
May 08, 2015
by
Long Duong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add MT Visualization
parent
2288eecd
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
52 additions
and
0 deletions
+52
-0
nltk/align/api.py
+52
-0
No files found.
nltk/align/api.py
View file @
d72c69d5
...
...
@@ -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``.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment