-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathann2nx.py
103 lines (71 loc) · 2.11 KB
/
ann2nx.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# coding: utf-8
# # Ann2nx
#
# Convertit les fichiers "ann" du corpus annoté de Stab & Gurevych en un fichier networkx
# In[10]:
import networkx
import zipfile
import re
# In[20]:
annfiles = re.compile("brat-project/essay[0-9]+.ann$")
rawdata = []
with zipfile.ZipFile("ArgumentAnnotatedEssays-1.0/brat-project.zip", "r") as f:
for i in list(filter(annfiles.match,f.namelist())):
ann = f.read(i).decode()
txt = f.read(i[:-3]+"txt").decode()
rawdata += [(ann,txt)]
# In[22]:
G = networkx.DiGraph()
# In[54]:
def filterType(t, letter):
f = filter(None, t)
return filter(lambda x: x[0]==letter, f)
# In[55]:
def get_whole_sentence(txt, start, end):
bound = re.compile("[.!?]+")
pre = bound.split(txt[:start])[-1].lstrip()
post = bound.split(txt[end:])[0].rstrip()
return pre + txt[start:end] + post
# In[94]:
def addnodes(ann, txt, num):
global G
for ln in filterType(ann.split("\n"), "T"):
s = ln.split()
name = s[0]
node_type = s[1]
start, end = map(int, s[2:4])
text = " ".join(s[4:])
G.add_node("Arg%02d_%s" % (num, name),
type = node_type,
text = text,
start = start,
end = end,
essay = num,
**extra_attrs)
# In[91]:
def addstances(ann, num):
global G
for ln in filterType(ann.split("\n"), "A"):
s = ln.split()
stance = s[1].lower()
node_name = s[2]
stance_type = s[3].lower()
G.node["Arg%02d_%s" % (num, node_name)][stance] = stance_type
# In[80]:
def addedges(ann, num):
global G
for ln in filterType(ann.split("\n"), "R"):
s = ln.split()
n1 = re.search(r"Arg1:(\w+)\b",s[2]).group(1)
n2 = re.search(r"Arg2:(\w+)\b",s[3]).group(1)
G.add_edge("Arg%02d_%s" % (num, n1), "Arg%02d_%s" % (num, n2), {"edge_type": s[1]})
# In[92]:
G = networkx.DiGraph()
n = 1
for ann, txt in rawdata:
addnodes(ann, txt, n)
addstances(ann, n)
addedges(ann, n)
n += 1
# In[106]:
networkx.write_gpickle(G,"corpus.gpickle")