Spaces:
Runtime error
Runtime error
File size: 1,712 Bytes
8044721 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
from graph import Graph;
def read_matrix(file):
rows = [];
for line in file:
line = line.rstrip();
if len(line) == 0:
return rows;
else:
rows.append(line.split("\t"));
return rows or None
def read_matrices(file):
file.readline().rstrip();
matrix = read_matrix(file);
while matrix:
yield matrix;
matrix = read_matrix(file);
def matrix2graph(matrix, framework = None, text = None):
graph = Graph(matrix[0][0][1:], flavor = 0, framework = framework);
predicates = [];
for id, row in enumerate(matrix[1:]):
lemma, pos, frame, top = row[2], row[3], row[6], row[4] == '+';
if lemma == "_": lemma = row[1];
properties = {"pos": pos};
if frame != "_": properties["frame"] = frame;
node = graph.add_node(id, label = lemma,
properties = list(properties.keys()),
values = list(properties.values()),
top = top, anchors = [row[1]] if text else None);
if row[5] == '+':
predicates.append(id);
for tgt, row in enumerate(matrix[1:]):
for pred, label in enumerate(row[7:]):
if label != '_':
src = predicates[pred];
edge = graph.add_edge(src, tgt, label);
if text:
graph.add_input(text);
graph.anchor();
#
# finally, purge singleton (isolated) nodes
#
graph.nodes = [node for node in graph.nodes if not node.is_singleton()];
return graph;
def read(fp, framework = None, text = None):
for matrix in read_matrices(fp):
yield matrix2graph(matrix, framework, text), None;
|