-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpoly_dist.py
More file actions
282 lines (256 loc) · 8.8 KB
/
poly_dist.py
File metadata and controls
282 lines (256 loc) · 8.8 KB
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
#-*- coding:utf-8 -*-
#!/usr/bin/env py3
import os
import sys
import re
import codecs
import copy
import csv
import xlwt
class Poly(object):
'''
calculate the distribution of polyphones
'''
def __init__(self, options, logger):
self.options = options
self.logger =logger
self.total = 0
self.poly_list = self.load_poly_list()
self.all_poly = [self.load_poly_list() for _ in range(6)]
self.all_total = []
self.outList = []
def isChinese(self, ch):
hexCH = ord(ch)
if (hexCH >= 0x3100 and hexCH <= 0x312F) \
or (hexCH >= 0x3400 and hexCH <= 0x4BDF) \
or (hexCH >= 0x4e00 and hexCH <= 0x9fff) \
or (hexCH >= 0xF900 and hexCH <= 0xFA2D) \
or (hexCH >= 0x20000 and hexCH <= 0x2a6d6) \
or (hexCH >= 0x2A700 and hexCH <= 0x2CEAF) \
or (hexCH >= 0x2F800 and hexCH <= 0x2FA1D) \
or (hexCH >= 0xe000 and hexCH <=0xf8ff):
return True
else:
return False
def load_poly_list(self):
with codecs.open(self.options['poly_list'], 'r', 'utf-8') as poly_list:
polys = []
for line in poly_list:
line = line.strip().split()[1]
polys.append([line,0])
return dict(polys)
def writeFile(self, fn, total, poly):
with codecs.open(fn,"w","utf-8") as fo:
fo.write("Total Char: %d\n" % total)
poly = sorted(poly.items(), key=lambda x: x[1], reverse=True)
for item in poly:
fo.write("%s %d %f\n" % (item[0], item[1], (item[1]*100)/total))
self.logger.info("Finishe %s" % fn)
def writeExcel(self):
wb = xlwt.Workbook()
for poly, total, n in zip(self.all_poly, self.all_total, self.outList):
ws = wb.add_sheet(str(n))
ws.write("Character", "Number","Percentage(%)")
ws.write("Total", total, 100)
poly = sorted(poly.items(), key=lambda x: x[1], reverse=True)
for item in poly:
ws.write(item[0], item[1], (item[1]*100)/total)
ws = wb.add_sheet("Total")
ws.write("Character", "Number","Percentage(%)")
ws.write("Total", self.total, 100)
poly = sorted(self.poly_list.items(), key=lambda x: x[1], reverse=True)
for item in poly:
ws.write(item[0], item[1], (item[1]*100)/self.total)
wb.save("poly.xls")
def processPOI(self):
fbase = self.options['POI']
total = 0
poly = self.load_poly_list()
district = os.listdir(fbase)
for d in district:
fs = os.listdir(os.path.join(fbase,d))
for f in fs:
fn = os.path.join(fbase,d,f)
self.logger.info("Process: %s"%fn)
fpoi = csv.reader(codecs.open(fn, encoding="GBK"))
while True:
try:
for line in fpoi:
line = line[0]
for c in line:
if self.isChinese(c):
total += 1
self.total += 1
if c in poly.keys():
poly[c] += 1
self.poly_list[c] += 1
break
except:
continue
self.writeFile("POI.out", total, poly)
def processSMS(self):
fbase = self.options['SMS']
total = 0
poly = self.load_poly_list()
fn = codecs.open(fbase,'r','utf-8')
self.logger.info("Process: %s"%fbase)
for line in fn:
line = line.strip()
# count = 0
# countA = 0
for c in line:
if self.isChinese(c):
# countA += 1
total += 1
self.total += 1
if c in poly.keys():
#count += 1
poly[c] += 1
self.poly_list[c] += 1
#print("total:%d, poly:%d"%(countA, count))
self.writeFile("SMS.out", total, poly)
def processRecord(self):
fbase = self.options['Record']
total = 0
poly = self.load_poly_list()
fs = os.listdir(fbase)
fs = list(filter(lambda x: "txt" in x, fs))
for f in fs:
self.logger.info("Process: %s"%f)
fn = os.path.join(fbase, f)
with codecs.open(fn, 'r', 'utf-8') as rd:
while True:
try:
for line in rd:
line = line.strip()
for c in line:
if self.isChinese(c):
total += 1
self.total += 1
if c in poly.keys():
poly[c] += 1
self.poly_list[c] += 1
break
except:
continue
self.writeFile("Record.out", total, poly)
def processCTB(self):
fbase = self.options['CTB']
total = 0
poly = self.load_poly_list()
fs = os.listdir(fbase)
for f in fs:
self.logger.info("Process: %s"%f)
fn = os.path.join(fbase, f)
with codecs.open(fn, 'r', 'utf-8') as ctb:
while True:
try:
for line in ctb:
line = line.strip()
if line[0] == r"<" or line == r"(完)":
continue
for c in line:
if self.isChinese(c):
total += 1
self.total += 1
if c in poly.keys():
poly[c] += 1
self.poly_list[c] += 1
break
except:
continue
self.writeFile("CTB.out",total, poly)
def processPKU(self):
fbase = self.options['PKU']
total = 0
poly = self.load_poly_list()
fs = os.listdir(fbase)
for f in fs:
fn = os.path.join(fbase, f)
with codecs.open(fn, 'r', 'gbk') as pku:
self.logger.info("Process: %s"%fn)
while True:
try:
for line in pku:
#line = re.sub("[0-9a-zA-Z\s\t]+","",line.strip())
for c in line:
if self.isChinese(c):
total += 1
self.total += 1
if c in poly.keys():
poly[c] += 1
self.poly_list[c] += 1
break
except:
continue
self.writeFile("PKU.out",total, poly)
def processWiki(self):
fbase = self.options['Wiki']
total = 0
poly = self.load_poly_list()
fn = fbase
with codecs.open(fn, 'r', 'utf-8') as wiki:
self.logger.info("Process: %s"%fn)
while True:
try:
for line in wiki:
line = re.sub("[0-9a-zA-Z\s\t]+","",line.strip())
for c in line:
if self.isChinese(c):
total += 1
self.total += 1
if c in poly.keys():
poly[c] += 1
self.poly_list[c] += 1
break
except:
continue
self.writeFile("Wiki.out",total, poly)
def main(self):
outList = ["PKU","SMS","Record","CTB","Wiki","POI"]
total = 0
poly = self.load_poly_list()
for i in outList:
fn = i+".out"
with codecs.open(fn,'r','utf-8') as fout:
first_line = fout.readline()
total += int(first_line.strip().split()[-1])
for line in fout:
line = line.strip().split()
poly[line[0]] += int(line[1])
self.writeFile("Total.out", total, poly)
if __name__ == '__main__':
import time
import logging
from argparse import ArgumentParser
parser = ArgumentParser(description='selectWord')
parser.add_argument("--version", action="version", version="selectWord 1.0")
parser.add_argument("--pku", action="store", dest="PKU", default=r"D:\Documents\poly\PKU", help='pku')
parser.add_argument("--poi", action="store", dest="POI", default=r"D:\Documents\poly\POI\Data", help='poi')
parser.add_argument("--ctb", action="store", dest="CTB", default=r"D:\Documents\poly\ctb8.0\data\raw", help='ctb')
parser.add_argument("--sms", action="store", dest="SMS", default=r"D:\Documents\poly\Chinese SMS Corpus-Word.txt", help='sms')
parser.add_argument("--wiki", action="store", dest="Wiki", default=r"D:\Documents\poly\wiki_s.txt", help='wiki')
parser.add_argument("--record", action="store", dest="Record", default=r"D:\Documents\poly\li-li record", help='record')
parser.add_argument("--poly_list", action="store", dest="poly_list", default=r"D:\Documents\poly\poly.list", help='record')
args = parser.parse_args()
options = vars(args)
logger = logging.getLogger()
formatter = logging.Formatter('[%(asctime)s][*%(levelname)s*][%(filename)s:%(lineno)d|%(funcName)s] - %(message)s', '%Y%m%d-%H:%M:%S')
file_handler = logging.FileHandler('LOG-polylist.txt', 'w',encoding='utf-8')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)
logger.setLevel(logging.INFO)
allStartTP = time.time()
appInst = Poly(options, logger)
# appInst.processPKU()
# appInst.processWiki()
# appInst.processRecord()
# appInst.processPOI()
# appInst.processCTB()
# appInst.processSMS()
appInst.main()
#appInst.writeExcel()
allEndTP = time.time()