-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyword_generator.py
141 lines (115 loc) · 4.02 KB
/
keyword_generator.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
import rdflib
class Keywords:
def __init__(self):
self.g = rdflib.Graph()
self.g.load("data/three_kingdoms.rdf", format="turtle")
self.place_list = []
self.loyalty_list = []
self.event_list = []
self.chapter_list = []
self.time_list = []
self.words = []
def write_to_file(self):
write_string = '\n'.join(self.words)
keywords_file = open('data/keywords.txt', 'w')
keywords_file.write(write_string)
keywords_file.close()
def character_names(self):
qres = self.g.query(
"""SELECT DISTINCT ?name
WHERE {
?person foaf:name ?name .
}""")
for row in qres:
self.words.append(str(row[0]) + "\tCHARACTER")
def birthplace_names(self):
qres = self.g.query(
"""SELECT DISTINCT ?ancient_birthplace ?modern_birthplace
WHERE {
?person foaf:ancient_birthplace ?ancient_birthplace .
?person foaf:modern_birthplace ?modern_birthplace .
}""")
for row in qres:
a_bplace = str(row[0])
m_bplace = str(row[1])
if a_bplace not in self.place_list:
self.words.append(a_bplace + "\tPLACE")
self.place_list.append(a_bplace)
if m_bplace not in self.place_list:
self.words.append(m_bplace + "\tPLACE")
self.place_list.append(m_bplace)
def belonging(self):
qres = self.g.query(
"""SELECT DISTINCT ?loyalty
WHERE {
?person foaf:loyalty ?loyalty .
}""")
for row in qres:
loyalty = str(row[0])
if loyalty not in self.loyalty_list:
self.words.append(loyalty + "\tLOYALTY")
self.loyalty_list.append(loyalty)
def gender(self):
self.words.append("男\tGENDER")
self.words.append("女\tGENDER")
def fictional(self):
self.words.append("史实人物\tFICTIONAL")
self.words.append("虚构人物\tFICTIONAL")
self.words.append("史实\tFICTIONAL")
self.words.append("虚构\tFICTIONAL")
def events(self):
qres = self.g.query(
"""SELECT DISTINCT ?eventname
WHERE {
?event foaf:event_name ?eventname .
}""")
for row in qres:
event = str(row[0])
if event not in self.event_list:
self.words.append(event + "\tEVENT")
self.event_list.append(event)
def chapters(self):
qres = self.g.query(
"""SELECT DISTINCT ?chapter
WHERE {
?event foaf:chapter ?chapter .
}""")
for row in qres:
chapter = str(row[0])
if chapter not in self.chapter_list:
self.words.append(chapter + "\tCHAPTER")
self.chapter_list.append(chapter)
def event_locations(self):
qres = self.g.query(
"""SELECT DISTINCT ?location
WHERE {
?event foaf:location ?location .
}""")
for row in qres:
location = str(row[0])
if location not in self.place_list:
self.words.append(location + "\tPLACE")
self.place_list.append(location)
def event_time(self):
qres = self.g.query(
"""SELECT DISTINCT ?time
WHERE {
?event foaf:time ?time
}""")
for row in qres:
time = row[0]
if time not in self.time_list:
self.words.append(time + "\tTIME")
self.time_list.append(time)
if __name__ == '__main__':
keywords = Keywords()
keywords.character_names()
keywords.birthplace_names()
keywords.belonging()
keywords.gender()
keywords.fictional()
keywords.events()
keywords.chapters()
keywords.event_locations()
keywords.event_time()
keywords.write_to_file()