forked from digling/shijing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC_refine_network.py
227 lines (184 loc) · 7.23 KB
/
C_refine_network.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
from lingpy import *
import networkx as nx
import igraph
from sys import argv
from lingpy.thirdparty import linkcomm
# preprocessing, load data, and modify the rimes
wl = Wordlist('O_shijing.tsv', col='shijing', row='stanza')
# define colors for vowels
colors = {
'ə' : ['black', 'red'],
'a' : ['white', 'green'],
'u' : ['white', 'blue'],
'e' : ['white', 'gray'],
'i' : ['black', 'yellow'],
'o' : ['black', 'cyan']
}
D = {}
for k in wl:
yunbu = wl[k,'ocbsyun']
yunbu = yunbu.replace('A','a')
yunbu = yunbu.replace('\u0325', '')
yunbu = ''.join([x for x in yunbu if x not in '[]()ʔ'])
yunbu2 = wl[k,'yun']
if 'ʔ' in wl[k,'yun']:
shang = yunbu + 'ʔ'
else:
shang = yunbu
if '[' in yunbu2 or '(' in yunbu2:
yunbu2 = '?'
else:
yunbu2 = '!'
D[k] = (yunbu,yunbu2, shang)
wl.add_entries('rime', D, lambda x: x[0])
wl.add_entries('certainty', D, lambda x: x[1])
wl.add_entries('shangsheng', D, lambda x: x[2])
# make the graph
_G = nx.Graph()
visited = []
excluded = 0
etd = wl.get_etymdict(ref='rhyme')
for row in wl.rows:
print('[i] Analyzing stanza {0}...'.format(row))
# get all stanzas
idxs = wl.get_list(row=row, flat=True)
# get the rime data
rimes = [wl[idx, 'rime'] for idx in idxs]
# get the words judged to be riming
cogids = [wl[idx,'rhyme'] for idx in idxs]
# get the readings
readings = [wl[idx, 'ocbs'] for idx in idxs]
# get the character
chars = [wl[idx, 'character'] for idx in idxs]
# get the sections
sections = [wl[idx, 'raw_section'] for idx in idxs]
# mch
mchs = [wl[idx, 'mchascii'] for idx in idxs]
shangsheng = [wl[idx,'shangsheng'] for idx in idxs]
# certainty
certainties = [wl[idx, 'certainty'] for idx in idxs]
# visited_tmp to trace multi-chars in one set
visited_tmp = []
# now start to populate the graph
for i,(idxA, rimeA,cogidA,readingA,charA,secA, certA, sA) in enumerate(
zip(idxs, rimes, cogids, readings, chars, sections, certainties,
shangsheng)):
for j,(idxB, rimeB,cogidB,readingB,charB, secB, certB, sB) in enumerate(
zip(idxs, rimes, cogids, readings, chars, sections,
certainties, shangsheng)):
if i < j:
if charA not in _G:
_G.add_node(
charA,
rime = rimeA,
cogid = cogidA,
reading = readingA,
color = colors[rimeA[0]][1] if rimeA else '',
labelcolor = colors[rimeA[0]][0] if rimeA else '',
occurrence = len([k for k in wl if
wl[k,'character'] == charA]),
mch = mchs[i],
certainty = certA,
shangsheng=sA,
stanza = ','.join([wl[k,'stanza'] for k in wl if
wl[k,'character'] == charA]),
)
if charB not in _G:
_G.add_node(
charB,
rime = rimeB,
cogid = cogidB,
reading = readingB,
color = colors[rimeB[0]][1] if rimeB else '',
labelcolor = colors[rimeB[0]][0] if rimeB else '',
occurrence = len([k for k in wl if
wl[k,'character'] == charB]),
stanza = ','.join([wl[k,'stanza'] for k in wl if
wl[k,'character'] == charB]),
mch = mchs[j],
certainty = certB,
shangsheng=sB
)
# check for section identity
if (secA, secB) not in visited and charA != charB and cogidA == \
cogidB and cogidA and (charA,charB) not in visited_tmp and \
(charB,charA) not in visited_tmp:
visited += [(secA, secB)]
visited_tmp += [(charA, charB), (charB, charA)]
try:
_G.edge[charA][charB]['occurrence'] += 1
_G.edge[charA][charB]['stanza'] += [row]
_G.edge[charA][charB]['woccurrence'] += 1 / len(etd[cogidA][0])
except KeyError:
_G.add_edge(charA, charB, occurrence=1,
stanza=[row],
woccurrence = 1 / len(etd[cogidA][0])
)
else:
excluded += 1
# check for external and internal edges
N = nx.Graph()
for nA, nB, data in _G.edges(data=True):
rA = _G.node[nA]['rime']
rB = _G.node[nB]['rime']
if rA and rB:
if rA not in N:
N.add_node(rA, occurrence=1, color = _G.node[nA]['color'],
lcolor=_G.node[nA]['labelcolor'])
else:
N.node[rA]['occurrence'] += 1
if rB not in N:
N.add_node(rB, occurrence=1, color =_G.node[nB]['color'],
lcolor=_G.node[nB]['labelcolor'])
else:
N.node[rB]['occurrence'] += 1
# add edges, if rA not equal to rB
if rA != rB:
try:
N.edge[rA][rB]['occurrence'] += 1
except KeyError:
N.add_edge(rA, rB, occurrence = 1)
for nA,nB,data in _G.edges(data=True):
data['stanza'] = ','.join(data['stanza'])
# convert stuff to igraph
G = igraph.Graph()
for node,data in _G.nodes(data=True):
G.add_vertex(name=node, **data)
for nodeA, nodeB, data in _G.edges(data=True):
G.add_edge(nodeA, nodeB, **data)
# do the same for the N graph
_N = igraph.Graph()
for node,data in N.nodes(data=True):
_N.add_vertex(name=node, **data)
for nodeA, nodeB, data in N.edges(data=True):
if data['occurrence'] >= 10:
_N.add_edge(nodeA, nodeB, **data)
nx.write_yaml(N, 'R_rime_graph.yaml')
if 'communities' in argv:
C = _N.community_infomap(
edge_weights = 'occurrence',
vertex_weights = 'occurrence'
)
for community,name in zip(C.membership, _N.vs['name']):
N.node[name]['infomap'] = community
print('[i] Calculated communities for rhyme groups')
# check statistics, for example, get
C = G.community_infomap(
edge_weights = 'woccurrence',
vertex_weights = 'occurrence'
)
for community,name in zip(C.membership, G.vs['name']):
_G.node[name]['infomap'] = community
print('[i] Calculated communities for rhyme words.')
from html import parser
nx.write_gml(N, 'R_rime_transitions.gml')
with open('R_rime_transitions.gml') as f:
_t = f.read()
with open('R_rime_transitions.gml','w') as f:
f.write(parser.unescape(_t))
nx.write_gml(_G, 'R_infomap.gml')
with open('R_infomap.gml') as f:
_t = f.read()
with open('R_infomap.gml','w') as f:
f.write(parser.unescape(_t))
nx.write_yaml(_G, 'R_infomap.yaml')