Skip to content

Instantly share code, notes, and snippets.

@dedan
Created October 10, 2011 15:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dedan/1275662 to your computer and use it in GitHub Desktop.
Save dedan/1275662 to your computer and use it in GitHub Desktop.
simple senna output parsing
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()
@anithachacko
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment