-
Notifications
You must be signed in to change notification settings - Fork 0
/
LR counting.py
291 lines (237 loc) · 9.59 KB
/
LR counting.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env python
# coding: utf-8
# In[5]:
import os
import codecs
import docx
import spacy
from collections import Counter
from numpy import dot
from numpy.linalg import norm
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
# In[65]:
class PACount:
def __init__(self,path):
self.path = path
self.types = ["[ac]","[bc]","[ae]","[be]","[ag]","[bg]","[ai]","[bi]","[ak]","[bk]"]
self.get_count()
self._get_group_count()
self.get_group_similarity()
self._get_group_ab_count()
self._get_group_nab_count_list()
self.get_group_ab_similarity()
self._get_group_ab_count_list()
def get_group_name(self):
return self.count_dict.keys()
def get_group_nab_count(self):
return self.no_ab_count
# for i in group_name:
# if i not in group_count:
# group_count[i] = {}
# for j in a.count_dict[i]:
# for k in a.count_dict[i][j]:
# if k not in group_count[i]:
# group_count[i][k] = 0
# group_count[i][k] += a.count_dict[i][j][k]
def _get_group_count(self):
self.group_count = {}
self.group_name = list(self.get_group_name())
for i in self.group_name:
if i not in self.group_count:
self.group_count[i] = {}
for j in self.count_dict[i]:
for k in self.count_dict[i][j]:
if k not in self.group_count[i]:
self.group_count[i][k] = 0
self.group_count[i][k] += self.count_dict[i][j][k]
self._get_group_count_list()
def print_nab_group_plot(self):
types = []
for i in self.types:
if i[2] not in types:
types.append(i[2])
for idx,i in enumerate(self.no_ab_count):
print(i)
# x = self.group_count_list[idx]
x = []
for j in self.no_ab_count[i]:
x.append(self.no_ab_count[i][j])
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.bar(types,x)
plt.savefig("noab_" + i +".pdf",dpi='figure',aspect='auto', bbox_inches = 'tight')
plt.show()
def print_ab_group_plot(self):
types = []
for i in self.types:
if i[1] not in types:
types.append(i[1])
for idx,i in enumerate(self.ab_count):
print(i)
# x = self.group_count_list[idx]
x = []
for j in self.ab_count[i]:
x.append(self.ab_count[i][j])
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.bar(types,x)
# plt.legend()
plt.savefig("ab_" + i +".pdf",dpi='figure',aspect='auto', bbox_inches = 'tight')
plt.show()
def print_group_plot(self):
for idx,i in enumerate(self.get_group_count()):
print(i)
x = self.group_count_list[idx]
# x = []
# for j in a.get_group_count()[i]:
# x.append(a.get_group_count()[i][j])
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.bar(self.types,x)
plt.savefig("all_" + i +".pdf",dpi='figure',aspect='auto', bbox_inches = 'tight')
plt.show()
# def print_group_ab(self):
def get_group_ab_count(self):
return self.ab_count
def _get_group_ab_count(self):
self.no_ab_count = {}
self.ab_count = {}
for i in self.group_count:
self.no_ab_count[i] = {}
self.ab_count[i] = {}
for j in self.group_count[i]:
t = j[2]
t1 = j[1]
if t1 not in self.ab_count[i]:
self.ab_count[i][t1] = 0
self.ab_count[i][t1] += self.group_count[i][j]
if t not in self.no_ab_count[i]:
self.no_ab_count[i][t] = 0
self.no_ab_count[i][t] += self.group_count[i][j]
def cos_sim(self,a,b):
return dot(a, b)/(norm(a)*norm(b))
def print_cos_sim(self):
for (i,j) in self.group_sim:
print(i + " and " + j + " cosine similarity is :")
print(self.group_sim[(i,j)])
def print_nab_cos_sim(self):
for (i,j) in self.group_no_ab_sim:
print(i + " and " + j + " cosine similarity is :")
print(self.group_no_ab_sim[(i,j)])
def get_group_similarity(self):
self.group_sim = {}
for idx,i in enumerate(self.group_count):
for jdx,j in enumerate(self.group_count):
if i != j:
if (i,j) not in self.group_sim:
self.group_sim[(i,j)] = self.cos_sim(self.group_count_list[idx],self.group_count_list[jdx])
# print(i + " and " + j + " cosine similarity is :")
# print(self.cos_sim(self.group_count_list[idx],self.group_count_list[jdx]))
def get_group_ab_similarity(self):
self.group_no_ab_sim = {}
for idx,i in enumerate(self.no_ab_count):
for jdx,j in enumerate(self.no_ab_count):
if i != j:
if (i,j) not in self.group_no_ab_sim:
self.group_no_ab_sim[(i,j)] = self.cos_sim(self.group_nab_count_list[idx],self.group_nab_count_list[jdx])
def get_group_count(self):
return self.group_count
def _get_group_nab_count_list(self):
self.group_nab_count_list = []
for i in self.no_ab_count:
single = []
for k in self.no_ab_count[i]:
single.append(self.no_ab_count[i][k])
self.group_nab_count_list.append(single)
def _get_group_ab_count_list(self):
self.group_ab_count_list = []
for i in self.ab_count:
single = []
for k in self.ab_count[i]:
single.append(self.ab_count[i][k])
self.group_ab_count_list.append(single)
def _get_group_count_list(self):
self.group_count_list = []
for i in self.group_count:
single = []
for k in self.group_count[i]:
single.append(self.group_count[i][k])
self.group_count_list.append(single)
def get_group_count_list(self):
return self.group_count_list
def get_count(self):
# path = "./final"
# files_path = os.listdir("./data")
# path = "./data/longterm"
files_path = os.listdir(self.path)
self.count_dict = {}
self.times = {}
self.w_count = {}
self.token_num = {}
for pa in files_path:
if ".DS_Store" not in pa:
folder_path = os.path.join(self.path,pa)
self.count_dict[pa] = {}
self.times[pa] = {}
folders = os.listdir(folder_path)
self.token_num[pa] = 0
self.w_count[pa] = {}
for t in self.types:
self.w_count[pa][t[1:3]] = Counter()
for file in folders:
if file.endswith('docx'):
self.count_dict[pa][file] = Counter()
file_path = os.path.join(folder_path,file)
doc = docx.Document(file_path)
sents = []
for i in doc.paragraphs:
sents.append(i.text)
if "TIMES:" in i.text:
line = i.text.replace("TIMES:","").strip().split(":")
# times[pa][file] =i.text.replace("TIMES:","").strip()
# line = times[i][j].split(":")
# times[i][j] = (int(line[0]) + int(line[1])/60)
self.times[pa][file] = (int(line[0]) + int(line[1])/60)
for i in self.types:
self.count_dict[pa][file][i] = 0
for i in sents:
line = i.replace("\n","").split("**")
for idx,j in enumerate(line):
for t in self.types:
if t in j:
self.w_count[pa][t[1:3]][line[idx-1].replace("说话人","").replace("[bc]","").replace("[ac]","").lower().strip()] +=1
# token_num[pa] = token(sents)
if "Chinese" in pa:
s = sents
for i in sents:
for j in self.types:
self.count_dict[pa][file][j] += i.count(j)
# In[66]:
path = "./final"
# In[67]:
# PACount(文件路径)
a = PACount(path)
# In[68]:
#输出group的similarity(重要
print("print group similarity")
a.print_cos_sim()
print("======================")
#输出group的similarity(不分ab的)
print("print similarity(no ab)")
a.print_nab_cos_sim()
print("======================")
print("print group count")
print(a.get_group_count())
print("======================")
print("print group count(no ab)")
print(a.no_ab_count)
print("======================")
# 输出group的数量(只分ab)
print("print group count(only ab)")
print(a.ab_count)
print("======================")
a.print_group_plot()
a.print_nab_group_plot()
a.print_ab_group_plot()