-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiko.py
executable file
·256 lines (228 loc) · 7.96 KB
/
diko.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Scipt for checking the formal consistency of Diko
# author: Paul Bédaride <[email protected]>
# date: 30/09/2012
#
# eid=336:n="Astrakhan":t=1:w=50
# rid=133275:n1=141480:n2=18280:t=10:w=50
#
# eid=336|n="Astrakhan"|t=1|w=50
# rid=133275|n1=141480|n2=18280|t=10|w=50
import re, sys, codecs
import sqlite3
nodeRE = re.compile(r'''^
eid=(?P<eid>\d+)\| # entry id
n="(?P<n>.*?)"\| # name
t=(?P<t>\d+)\| # type
w=(?P<w>\d+) # weight
(?:\|nf(?P<nf>.*))? # formated name
\n$''', re.VERBOSE)
termSpecRE = re.compile(r'''^
.*? # name
(?:>(\d+))+ # specs
$''', re.VERBOSE)
chunkRE = re.compile(r'''^
::>(?P<a>\d+) # type of chunk
:(?P<b>\d+) # base id
>(?P<c>\d+) # type of relation
:(?P<d>\d+) # destination id
$''', re.VERBOSE)
questionRE = re.compile(r'''^
::>(?P<a>\d+) # type of argument
:(?P<b>\d+) # argument id
>(?P<c>\d+) # type
:(?P<d>\d+) # predicate id
>(?P<e>\d+) # missing type
$''', re.VERBOSE)
relationRE = re.compile(r'''^
rid=(?P<rid>\d+)\| # relation id
n1=(?P<n1>\d+)\| # starting node
n2=(?P<n2>\d+)\| # ending node
t=(?P<t>\d+)\| # type
w=(?P<w>-?\d+) # weight
\n$''', re.VERBOSE)
relationTypeRE = re.compile(r'''^
rtid=(?P<rtid>\d+)\| # relation type id
name="(?P<name>.*?)"\| # relation type name
nom_etendu="(?P<extended_name>.*?)"\| # relation type extended name
info="(?P<info>.*?)" # relation type info
\n$''', re.VERBOSE)
class Diko(object):
def __init__(self, sqlite, xml=None):
self.conn = sqlite3.connect(sqlite)
self.conn.text_factory = str
self.c = self.conn.cursor()
self.c.execute("SELECT name FROM sqlite_master WHERE type='table';")
if xml is not None and len(self.c.fetchall())==0:
self.createDatabase()
self.fillDatabase(xml)
def __del__(self):
self.conn.close()
def createDatabase(self):
self.c.executescript('''
CREATE TABLE nodes(
id integer PRIMARY KEY,
name text,
type integer,
weight integer,
formated_name text);
CREATE TABLE relations(
id integer PRIMARY KEY ,
src integer,
dst integer,
type integer,
weight integer);
CREATE TABLE chunks(
id integer PRIMARY KEY,
type integer,
base integer,
relation integer,
dest integer);
CREATE TABLE questions(
id integer PRIMARY KEY,
argument_type integer,
argument_id integer,
question_type integer,
pred_id integer,
missing_type integer);
CREATE TABLE terms(
id integer,
place integer,
term integer,
PRIMARY KEY (id, place));
''')
def fillDatabase(self, xml):
with codecs.open(xml) as diko:
# reading lines
for line in diko:
if line.startswith('//') or line=='\n':
continue
node = nodeRE.match(line)
if node:
name = node.group('n')
nid = int(node.group('eid'))
if '>' in name:
chunk = chunkRE.match(name)
if chunk:
self.c.execute(u"INSERT INTO chunks VALUES (?,?,?,?,?)",
(nid,
int(chunk.group('a')),
int(chunk.group('b')),
int(chunk.group('c')),
int(chunk.group('d'))))
name = ''
pass
question = questionRE.match(name)
if question:
self.c.execute(u"INSERT INTO questions VALUES (?,?,?,?,?,?)",
(nid,
int(question.group('a')),
int(question.group('b')),
int(question.group('c')),
int(question.group('d')),
int(question.group('e'))))
name = ''
pass
termSpec = termSpecRE.match(name)
if termSpec:
self.c.executemany("INSERT INTO terms VALUES (?,?,?)",
[(nid, i, int(id)) for i,id in enumerate(name.split('>')[1:])])
name = ''
self.c.execute(u"INSERT INTO nodes VALUES (?,?,?,?,?)",
(nid,
name,
int(node.group('t')),
int(node.group('w')),
node.group('nf')))
continue
relation = relationRE.match(line)
if relation:
self.c.execute(u"INSERT INTO relations VALUES (?,?,?,?,?)",
(int(relation.group('rid')),
int(relation.group('n1')),
int(relation.group('n2')),
int(relation.group('t')),
int(relation.group('w'))))
continue
relationType = relationTypeRE.match(line)
if relationType:
continue
print "the line %s was not parsed"%repr(line)
self.conn.commit()
def nodes(self, word):
return [Node(self, *node) for node in self.c.execute(u"""SELECT * FROM nodes
WHERE ? = name """, (word,)).fetchall()]
def check(self):
print "Checking relations src:"
r = self.c.execute(u"""SELECT id, src FROM relations
WHERE src NOT IN (SELECT id FROM nodes)""").fetchall()
print " ", "OK" if len(r)==0 else r
print "Checking relations dst:"
r = self.c.execute(u"""SELECT id, dst FROM relations
WHERE dst NOT IN (SELECT id FROM nodes)""").fetchall()
print " ", "OK" if len(r)==0 else r
print "Checking chunks type:"
r = self.c.execute(u"""SELECT nodes.id, nodes.type
FROM chunks
JOIN nodes ON nodes.id = chunks.id
WHERE nodes.type != 8""").fetchall()
print " ", "OK" if len(r)==0 else r
print "Checking chunks base:"
r = self.c.execute(u"""SELECT id, base FROM chunks
WHERE base NOT IN (SELECT id FROM nodes)""").fetchall()
print " ", "OK" if len(r)==0 else r
print "Checking chunks dest:"
r = self.c.execute(u"""SELECT id, dest FROM chunks
WHERE dest NOT IN (SELECT id FROM nodes)""").fetchall()
print " ", "OK" if len(r)==0 else r
print "Checking questions type:"
r = self.c.execute(u"""SELECT nodes.id, nodes.type
FROM questions
JOIN nodes ON nodes.id = questions.id
WHERE nodes.type != 9""").fetchall()
print " ", "OK" if len(r)==0 else r
print "Checking questions pred:"
r = self.c.execute(u"""SELECT id, pred_id FROM questions
WHERE pred_id NOT IN (SELECT id FROM nodes)""").fetchall()
print " ", "OK" if len(r)==0 else r
print "Checking questions argument:"
r = self.c.execute(u"""SELECT id, argument_id FROM questions
WHERE argument_id NOT IN (SELECT id FROM nodes)""").fetchall()
print " ", "OK" if len(r)==0 else r
print "Checking terms:"
r = self.c.execute(u"""SELECT * FROM terms
WHERE term NOT IN (SELECT id FROM nodes)""").fetchall()
print " ", "OK" if len(r)==0 else r
class Node(object):
types = {0: "generic",
1: "term",
2: "acception",
3: "definition",
4: "POS",
5: "concept",
6: "flpot",
7: "hub",
8: "chunk",
9: "question",
10: "relation",
18: "data",
36: "data_pot",
666: "AKI",
777: "wikipedia"}
def __init__(self, diko, id, name, type, weight, formated_name):
self.diko = diko
self.id = id
self.name = name
self.type = self.types[type]
self.weight = weight
self.formated_name = formated_name
def relationsFrom(self):
return self.diko.c.execute(u"""SELECT * FROM relations
WHERE src = ?""", (self.id,)).fetchall()
if __name__=="__main__":
diko = Diko("diko.db", sys.argv[1])
#diko.check()
table = diko.nodes("table")[0]
print table.type
print table.relationsFrom()