Created
October 10, 2011 15:50
-
-
Save dedan/1275662 to your computer and use it in GitHub Desktop.
simple senna output parsing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import csv | |
from StringIO import StringIO | |
import subprocess as sp | |
from nltk.draw.tree import Tree, TreeWidget | |
from nltk.draw.util import CanvasFrame | |
senna_path = '/Users/dedan/Downloads/senna/' | |
sentence = 'My brother has a dog' | |
# read senna output | |
p = sp.Popen(['blabla', '-path', senna_path], | |
executable=os.path.join(senna_path, 'senna-osx'), | |
stdin=sp.PIPE, | |
stdout=sp.PIPE) | |
tagged = StringIO(p.communicate(sentence)[0]) | |
table = csv.reader(tagged, dialect='excel-tab') | |
# extract last column | |
tree_string = '' | |
for line in table: | |
if line: | |
tree_string += line[-1] | |
print line | |
# replace stars by corresponding word | |
tree_string = tree_string.replace('*', ' %s ') | |
tree_string = tree_string % tuple(sentence.split()) | |
print tree_string | |
# use nltk to create parsetree | |
treetok = Tree.parse(tree_string) | |
# show parsetree | |
cf = CanvasFrame(width=550, height=450, closeenough=2) | |
tc = TreeWidget(cf.canvas(), treetok, draggable=1, | |
node_font=('helvetica', -14, 'bold'), | |
leaf_font=('helvetica', -12, 'italic'), | |
roof_fill='white', roof_color='black', | |
leaf_color='green4', node_color='blue2') | |
cf.add_widget(tc,10,10) | |
cf.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
I am getting the following error when i get the code in windows. Pls help
Traceback (most recent call last):
File "C:/Users/anithachacko/Downloads/senna_test.py", line 16, in
stdout=sp.PIPE)
File "C:\Python27\lib\subprocess.py", line 390, in init
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 640, in _execute_child
startupinfo)
WindowsError: [Error 3] The system cannot find the path specified