Commit c09ae1a1 by Alex Rudnick

Merge pull request #291 from pflaquerre/plug-dependencygraph-fd-leak

Plug DependencyGraph file descriptor leak
parents d45d76f5 3613d831
...@@ -15,6 +15,9 @@ The input is assumed to be in Malt-TAB format ...@@ -15,6 +15,9 @@ The input is assumed to be in Malt-TAB format
Currently only reads the first tree in a file. Currently only reads the first tree in a file.
""" """
# python2.5 compatibility
from __future__ import with_statement
from nltk.tree import Tree from nltk.tree import Tree
from pprint import pformat from pprint import pformat
import re import re
...@@ -118,7 +121,8 @@ class DependencyGraph(object): ...@@ -118,7 +121,8 @@ class DependencyGraph(object):
""" """
:param file: a file in Malt-TAB format :param file: a file in Malt-TAB format
""" """
return DependencyGraph(open(file).read()) with open(file) as f:
return DependencyGraph(f.read())
@staticmethod @staticmethod
def _normalize(line): def _normalize(line):
...@@ -239,7 +243,6 @@ class DependencyGraph(object): ...@@ -239,7 +243,6 @@ class DependencyGraph(object):
for dep in node['deps']: for dep in node['deps']:
key = tuple([node['address'], dep]) #'%d -> %d' % (node['address'], dep) key = tuple([node['address'], dep]) #'%d -> %d' % (node['address'], dep)
distances[key] = 1 distances[key] = 1
window = 0
for n in range(len(self.nodelist)): for n in range(len(self.nodelist)):
new_entries = {} new_entries = {}
for pair1 in distances: for pair1 in distances:
...@@ -343,12 +346,10 @@ Nov. NNP 9 VMOD ...@@ -343,12 +346,10 @@ Nov. NNP 9 VMOD
print tree.pprint() print tree.pprint()
if nx: if nx:
#currently doesn't work #currently doesn't work
try: import networkx as NX
import networkx as NX import pylab as P
import pylab as P
except ImportError: g = dg.nx_graph()
raise
g = dg.nx_graph()
g.info() g.info()
pos = NX.spring_layout(g, dim=1) pos = NX.spring_layout(g, dim=1)
NX.draw_networkx_nodes(g, pos, node_size=50) NX.draw_networkx_nodes(g, pos, node_size=50)
......
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