-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_sentence_degree_range.py
79 lines (60 loc) · 2.31 KB
/
get_sentence_degree_range.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
import sqlite3
import os
CWD = os.getcwd()
DATA = []
def split_sentence(line):
spl = str(line).split(" ", 2)
return spl[0], spl[1], spl[2]
def save_to_file():
with open("co_occur_stats_filtered_2.txt", "w", encoding="utf8") as f:
for item in DATA:
f.write("{} {} {}\n".format(item[0], item[1], item[2]))
def save_to_sqlite():
conn = sqlite3.connect('baike.db')
c = conn.cursor()
for item in DATA:
item[0] = str(item[0]).replace("'", "''")
item[1] = str(item[1]).replace("'", "''")
item[2] = str(item[2]).replace("'", "''")
# item[2] = str(item[2]).replace('"','""')
c.execute("select count(*) from Data where entity_a=? and entity_b=? and sentence=?",
(item[0], item[1], item[2]))
result = c.fetchone()
print(result)
if result[0] == 0:
sql = "insert into Data3(entity_a,entity_b,sentence,relation) VALUES('{}','{}','{}',0)".format(item[0],
item[1],
item[2])
print(sql)
c.execute(sql)
conn.commit()
conn.close()
def update_data():
conn = sqlite3.connect('baike.db')
c = conn.cursor()
c.execute("select entity_a,entity_b,sentence, relation from Data2 where relation!=0")
result = c.fetchall()
for row in result:
c.execute("update Data3 set relation=? where entity_a=? and entity_b=? and sentence=?",
(row[3], row[0], row[1], row[2]))
conn.commit()
conn.close()
def read_data():
entity_set = set()
with open("co_occur_stats_lite.txt", "r", encoding="utf8") as g:
for line in g:
spl = line.split("\t")
entity_set.add(spl[0])
with open("entity_sentences_lite.txt", "r", encoding="utf8") as f:
for line in f:
line = line.strip()
entity_a, entity_b, sentence = split_sentence(line)
if entity_a in entity_set or entity_b in entity_set:
DATA.append([entity_a, entity_b, sentence])
def main():
# read_data()
# # save_to_file()
# save_to_sqlite()
update_data()
if __name__ == '__main__':
main()