-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathN_gram_engin_arithmetic.py
2006 lines (1931 loc) · 124 KB
/
N_gram_engin_arithmetic.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
__author__ = 'wanghuafeng'
#coding:utf-8
import os
import re
import sys
import math
import glob
import codecs
import itertools
import time
# module_path = 'E:\SVN\chocolate_ime\script\gen_update_words\gen_hot_words'
# sys.path.append(module_path)
# from add_words_spell_m import WordsSearch
PATH = os.path.dirname(os.path.abspath(__file__))
class WordList(object):
def __init__(self):
pass
def add_word_pinyin(self):
'''为单字进行标音'''
ws = WordsSearch()
pinyin_set = set()
single_word_filename = os.path.join(PATH, 'data', 'single_word_5322_with_freq.txt')
com_str_list = []
with codecs.open(single_word_filename, encoding='utf8') as f:
for line in f.readlines():
splited_line = line.split('\t')
word = splited_line[0]
pinyin = ' '.join(ws.get_splited_pinyin(word)[0]).replace('*', '')
com_str = '\t'.join((word, pinyin)) + '\n'
com_str_list.append(com_str)
print len(pinyin_set)#402
new_filename = os.path.join(PATH, 'data', 'single_word_5322_with_pinyin.txt')
codecs.open(new_filename, mode='wb', encoding='utf-8').writelines(com_str_list)
class AddOmitPinyin:
'''27万词表中统计出的单字拼音与所有汉字拼音的差集'''
def gen_single_word_pinyin_set(self):
single_word_pinyin_filename = os.path.join(PATH, 'data', 'single_word_5322_with_pinyin.txt')
single_pinyin_set = set()
with codecs.open(single_word_pinyin_filename, encoding='utf-8') as f:
for line in f.readlines():
spilted_line = line.split('\t')
word = spilted_line[0]
pinyin = spilted_line[-1].strip()
single_pinyin_set.add(pinyin)
print len(single_pinyin_set)
return single_pinyin_set
def total_pinyin_set(self):
HZout_NoTone_filename = os.path.join(PATH, 'data', 'HZout_NoTone.txt')
notone_pinyin_set = set()
with codecs.open(HZout_NoTone_filename, encoding='utf16') as f:
for line in f.readlines():
splited_line = line.split('\t')
word = splited_line[0]
pinyin = splited_line[1]
notone_pinyin_set.add(pinyin)
print len(notone_pinyin_set)#413
return notone_pinyin_set
def get_omit_pinyin(self):
single_pinyin_set = self.gen_single_word_pinyin_set()
notone_pinyin_set = self.total_pinyin_set()
print notone_pinyin_set-single_pinyin_set
def add_freq_to_single_word(self):
whole_word_freq_dic = {}
word_freq_filename = os.path.join(PATH, 'data', 'word_freq_unsorted_from_orginal_data.txt')
with codecs.open(word_freq_filename, encoding='utf-8') as f:
for line in f.readlines():
splited_line = line.split('\t')
word = splited_line[0]
freq = splited_line[-1]
whole_word_freq_dic[word] = freq
single_word_filename = os.path.join(PATH, 'data', 'single_word_from_95K_5322.txt')
com_str_list = []
with codecs.open(single_word_filename, encoding='utf-8') as f:
for line in f.readlines():
word = line.strip()
freq = whole_word_freq_dic.get(word, '0\n')
com_str = u'\t'.join((word, freq))
# print com_str
com_str_list.append(com_str)
codecs.open(single_word_filename, mode='wb', encoding='utf-8').writelines(com_str_list)
def caculate_word_weight(self):
'''计算词表文件的频率'''
combine_filename = os.path.join(PATH, 'data', 'single_word_5329.txt')
new_file_to_write_filename = os.path.join(PATH, 'data', 'single_word_weight_5329.txt')
with codecs.open(combine_filename, encoding='utf-8') as f, \
codecs.open(new_file_to_write_filename, mode='wb', encoding='utf-8') as wf:
line_list = f.readlines()
sum_freq = sum([int(item.split('\t')[1]) for item in line_list])
print sum_freq
for line in line_list:
splited_line = line.split('\t')
freq_percentage = int(splited_line[1])/float(sum_freq)
word = splited_line[0]
weight = str(int(math.log10(freq_percentage)*(-20000)))
com_str = '\t'.join((word, weight))
wf.write(com_str+'\n')
# wl = WordList()
# wl.caculate_word_weight()
class CutLinguisticData(object):
'''用新词表进行第二次切割'''
def gen_cuted_linguistic_data(self):
'''在非词表出进行切割'''
src_data_path = r'E:\SVN\linguistic_model\data'
base_wordlist_filename = os.path.join(PATH, 'data', 'single_word_5329.txt')
cut_filename = os.path.join(src_data_path, 'linguistic_sample.txt')
format_str = ''.join([item.split('\t')[0] for item in codecs.open(base_wordlist_filename, encoding='utf-8').readlines()])
base_wordlist_pattern =re.compile(ur"([%s]+)"%format_str, re.U)
cuted_linguistic_data_filename = os.path.join(PATH, 'cuted_linguistic_data.txt')
with codecs.open(cut_filename, encoding='utf-8') as f, \
codecs.open(cuted_linguistic_data_filename, mode='wb', encoding='utf-8') as wf:
for line in f.readlines():
splited_line_list = base_wordlist_pattern.split(line)
for param in splited_line_list:
if len(param) == 0:
continue
if base_wordlist_pattern.match(param):
wf.write(param+'\n')
def cut_linguistic_sample_into_small_part(self):
'''将按词表切割后的文件分为150M大小的若干个文件'''
cuted_filename = os.path.join(PATH, 'cuted_linguistic_data.txt')
sample_filename_int = 1
with codecs.open(cuted_filename, encoding='utf-8') as f:
while 1:
line = f.read(51470527)
if not line:
break
stample_filename = os.path.join(PATH, 'splited_linguistic_data', '%s.txt'%sample_filename_int)
with codecs.open(stample_filename, mode='wb', encoding='utf-8') as wf:
wf.write(line)
sample_filename_int += 1
def cut_lines_into_words(self):
'''将行(句子)切割成词,其间以空格隔开'''
for file_count in range(1, 29):
total_line_list = []
print file_count
src_filename = os.path.join(PATH, 'splited_linguistic_data', '%s.txt'%file_count)
with codecs.open(src_filename, encoding='utf-8') as f:
# cuted_lines_list = [' '.join(cs.cut_with_weight(line))+'\n' for line in f.readlines()]
for line in f.readlines():
line_list = []
for single_word in line.strip():
line_list.append(single_word)
new_line = ' '.join(line_list) + '\n'
total_line_list.append(new_line)
codecs.open(src_filename, mode='wb', encoding='utf-8').writelines(total_line_list)
def cut_sentence_into_words(self):
'''将行(句子)切割成词,其间以空格隔开'''
from cut_sentence import Cut_Sentence
cs = Cut_Sentence()
src_path = r'F:\linguistic_data\original_data_cuted'
file_list = os.listdir(src_path)
for file_name in file_list:
print file_name
filename = os.path.join(src_path, file_name)
with codecs.open(filename, encoding='utf-8') as f:
cuted_lines_list = [' '.join(cs.cut_with_weight(line))+'\n' for line in f.readlines()]
codecs.open(filename, mode='wb', encoding='utf-8').writelines(cuted_lines_list)
# cut_sentence_into_words()
# cs = CutLinguisticData()
# cs.cut_lines_into_words()
class GenNgram(object):
'''生成n-gram模型'''
def __init__(self):
self.src_data_file_path = r'E:\SVN\linguistic_model\N_gram\splited_linguistic_data'
self.TOTAL_FILE_COUNT = 28
self.uninorder_file_pattern = '%s_four_gram.txt'#排序前的N元模型
self.inorder_file_pattern = '%s_four_gram_inorder.txt'#排序后的N元模型
self.uncombine_inorder_file_pattern = 'four_gram_inorder.txt'#合并前的N元模型
self.combine_filename = 'four_gram_combine_word_freq.txt'#合并后的N元模型
def gen_bigram_data(self, ngram='bigram'):
'''统计二、三元模型'''
src_data_path = r'E:\SVN\linguistic_model\N_gram'
des_path = r'E:\SVN\linguistic_model\N_gram\bigram'
src_data_filename = os.path.join(src_data_path, 'cuted_linguistic_data.txt')
bigram_freq_dic = {}
trigram_param_freq_dic = {}
with codecs.open(src_data_filename, encoding='utf-8') as f:
for line in (item.strip() for item in f.readlines()):
if not line:
continue
lenght_of_line = len(line)
if ngram == 'bigram':
# #二元组数据
line_bigram_list = []
line_bigram_list.append('BOS'+line[0])
for i in range(lenght_of_line):
if i < lenght_of_line - 1:
line_bigram_list.append(line[i]+line[i+1])
line_bigram_list.append(line[-1]+'EOS')
for bigram_item in line_bigram_list:
try:
bigram_freq_dic[bigram_item] += 1
except:
bigram_freq_dic[bigram_item] = 1
elif ngram == 'trigram':
##三元组数据
line_trigram_list = []
if lenght_of_line == 2:
line_trigram_list.append('BOS'+ line[0]+ line[1])
line_trigram_list.append(line[0]+line[1]+'EOS')
elif lenght_of_line > 2:
line_trigram_list.append('BOS'+line[0]+line[1])
for k in range(lenght_of_line):
if k < lenght_of_line - 2:
line_trigram_list.append(line[k]+line[k+1]+line[k+2])
elif k == lenght_of_line - 2:
line_trigram_list.append(line[k]+line[k+1]+'EOS')
for trigram_item in line_trigram_list:
try:
trigram_param_freq_dic[trigram_item] += 1
except:
trigram_param_freq_dic[trigram_item] = 1
if ngram == 'bigram':
bigram_tuple_freq_list = ['\t'.join((bigram_item, str(freq)))+'\n' for (bigram_item, freq) in bigram_freq_dic.items()]
bigram_filename = os.path.join(des_path, 'bigram_combine_freq.txt')
codecs.open(bigram_filename, mode='wb', encoding='utf-8').writelines(bigram_tuple_freq_list)
elif ngram == 'trigram':
trigram_tuple_freq_list = ['\t'.join((trigram_item, str(freq)))+'\n' for (trigram_item, freq) in trigram_param_freq_dic.items()]
trigram_filename = os.path.join(des_path, 'trigram_combine_freq.txt')
codecs.open(trigram_filename, mode='wb', encoding='utf-8').writelines(trigram_tuple_freq_list)
def gen_n_igram_model_data(self, n=4):
'''统计三元、四元、五元组数据模型'''
file_name_pattern = 'four' if n==4 else 'five' if n==5 else 'three'
for file_count in range(1, 29):
n_gram_param_freq_dic = {}
# five_gram_param_freq_dic = {}
print file_count
src_data_filename = os.path.join(PATH, 'splited_linguistic_data', '%s.txt'%file_count)
with codecs.open(src_data_filename, encoding='utf-8') as f:
for line in [item.strip().replace(' ', '') for item in f.readlines()]:
if not line:
continue
splited_list_length = len(line)
###四元组数据
if n == 4:
line_fourgram_list = []
if splited_list_length == 1:
# line_fourgram_list.append(('BOS', line[0]))
# line_fourgram_list.append((line[0], 'EOS'))
continue
elif splited_list_length == 2:
# line_fourgram_list.append(('BOS', line[0]))
# line_fourgram_list.append(('BOS', line[0], line[1]))
# line_fourgram_list.append((line[0], line[1], 'EOS'))
continue
elif splited_list_length == 3:
# line_fourgram_list.append(('BOS', line[0]))
# line_fourgram_list.append(('BOS', line[0], line[1]))
line_fourgram_list.append(('BOS', line[0], line[1], line[2]))
line_fourgram_list.append((line[0], line[1], line[2], 'EOS'))
# line_fourgram_list.append((line[1], line[2], 'EOS'))
# line_fourgram_list.append((line[2], 'EOS'))
else:
# line_fourgram_list.append(('BOS', line[0]))
# line_fourgram_list.append(('BOS',line[0], line[1]))
line_fourgram_list.append(('BOS', line[0], line[1], line[2]))
for k in range(splited_list_length):
if k < splited_list_length - 3:
line_fourgram_list.append((line[k], line[k+1], line[k+2], line[k+3]))
elif k == splited_list_length - 3:
line_fourgram_list.append((line[-3], line[-2], line[-1], 'EOS'))
# elif k == splited_list_length - 2:
# line_fourgram_list.append((line[-2], line[-1], 'EOS'))
# elif k == splited_list_length - 1:
# line_fourgram_list.append((line[-1], 'EOS'))
# for bigram_item in line_fourgram_list:
# print bigram_item[0], bigram_item[1], bigram_item[2], bigram_item[3]
# time.sleep(1)
for fourgram_tuple in line_fourgram_list:
try:
n_gram_param_freq_dic[fourgram_tuple] += 1
except:
n_gram_param_freq_dic[fourgram_tuple] = 1
# ##五元组数据
if n == 5:
line_fivegram_list = []
if splited_list_length == 1:
line_fivegram_list.append(('BOS', line[0]))
line_fivegram_list.append((line[0], 'EOS'))
elif splited_list_length == 2:
line_fivegram_list.append(('BOS', line[0]))
line_fivegram_list.append(('BOS', line[0], line[1]))
line_fivegram_list.append((line[0], line[1], 'EOS'))
line_fivegram_list.append((line[1], 'EOS'))
elif splited_list_length == 3:
line_fivegram_list.append(('BOS', line[0]))
line_fivegram_list.append(('BOS', line[0], line[1]))
line_fivegram_list.append(('BOS', line[0], line[1], line[2]))
line_fivegram_list.append((line[0], line[1], line[2], 'EOS'))
line_fivegram_list.append((line[1], line[2], 'EOS'))
line_fivegram_list.append((line[2], 'EOS'))
elif splited_list_length == 4:
line_fivegram_list.append(('BOS', line[0]))
line_fivegram_list.append(('BOS', line[0], line[1]))
line_fivegram_list.append(('BOS', line[0], line[1], line[2]))
line_fivegram_list.append(('BOS', line[0], line[1], line[2], line[3]))
line_fivegram_list.append((line[0], line[1], line[2], line[3], 'EOS'))
line_fivegram_list.append((line[1], line[2], line[3], 'EOS'))
line_fivegram_list.append((line[2], line[3], 'EOS'))
line_fivegram_list.append((line[3], 'EOS'))
else:
line_fivegram_list.append(('BOS', line[0]))
line_fivegram_list.append(('BOS', line[0], line[1]))
line_fivegram_list.append(('BOS', line[0], line[1], line[2]))
line_fivegram_list.append(('BOS', line[0], line[1], line[2], line[3]))
for k in range(splited_list_length):
if k < splited_list_length - 4:
line_fivegram_list.append((line[k], line[k+1], line[k+2], line[k+3], line[k+4]))
elif k == splited_list_length - 4:
line_fivegram_list.append((line[-4], line[-3], line[-2], line[-1], 'EOS'))
elif k == splited_list_length - 3:
line_fivegram_list.append((line[-3], line[-2], line[-1], 'EOS'))
elif k == splited_list_length - 2:
line_fivegram_list.append((line[-2], line[-1], 'EOS'))
elif k == splited_list_length - 1:
line_fivegram_list.append((line[-1], 'EOS'))
# for bigram_item in line_fivegram_list:
# print bigram_item[0], bigram_item[1], bigram_item[2], bigram_item[3], bigram_item[4]
# time.sleep(1)
for fivegram_tuple in line_fivegram_list:
try:
n_gram_param_freq_dic[fivegram_tuple] += 1
except:
n_gram_param_freq_dic[fivegram_tuple] = 1
# # 写入N元组
n_gram_com_str_list = ('\t'.join((','.join(n_gram_tuple), str(freq)))+'\n' for (n_gram_tuple, freq) in n_gram_param_freq_dic.items())
n_gram_filename = os.path.join(self.src_data_file_path, '%(file_count)s_%(file_name_pattern)s_gram.txt'%({'file_count':file_count, 'file_name_pattern':file_name_pattern}))
codecs.open(n_gram_filename, mode='wb', encoding='utf-8').writelines(n_gram_com_str_list)
# #写入五元组
# fivegram_com_str_list = ('\t'.join((','.join(fivegram_tuple), str(freq)))+'\n' for (fivegram_tuple, freq) in n_gram_param_freq_dic.items())
# n_gram_filename = os.path.join(PATH, '0709modify', 'four_five_gram_item', '%s_five_gram.txt'%file_count)
# codecs.open(n_gram_filename, mode='wb', encoding='utf-8').writelines(fivegram_com_str_list)
def mk_n_gram_inorder(self, uninorder_file_pattern, inorder_file_pattern):
'''N元组文件排序'''
for file_count in range(1, self.TOTAL_FILE_COUNT+1):
print file_count
ngram_filename = os.path.join(self.src_data_file_path, uninorder_file_pattern%file_count)
with codecs.open(ngram_filename, encoding='utf-8') as f:
sorted_bigram_list = sorted(f.readlines(), key=lambda x:x.split('\t')[0])
inorder_bigram_filename = os.path.join(self.src_data_file_path, inorder_file_pattern%file_count)
codecs.open(inorder_bigram_filename, mode='wb', encoding='utf-8').writelines(sorted_bigram_list)
def combine_bigram_freq(self, uncombine_file_pattern, combine_filename='combine_word_freq.txt'):
'''将n个排序后文件的N元模型进行词频叠加'''
combine_bigram_freq_filename = os.path.join(self.src_data_file_path, combine_filename)
com_fileObj = codecs.open(combine_bigram_freq_filename, mode='a', encoding='utf-8')
for file_count in range(1, self.TOTAL_FILE_COUNT+1):
#28个bigram_filename[1, 28]
exec "bigram_filename%(bigram_filename_count)s = os.path.join(self.src_data_file_path, '%(bigram_inorder)s_{}'.format(uncombine_file_pattern))"%{'bigram_filename_count':file_count, 'bigram_inorder':file_count} in globals(), locals()
#28个fileobj[1, 28]
exec "fileObj%(fileObj_count)s = codecs.open(bigram_filename%(bigram_filename_count)s, encoding='utf-8')"%({'fileObj_count':file_count,'bigram_filename_count':file_count}) in globals(), locals()
bigram_param_list = []
for fileObj_index in range(1, self.TOTAL_FILE_COUNT+1):
bigram_param_list.append((fileObj_index, eval('next(fileObj%s)'%fileObj_index)))
#以fileObj的index为key,以bigram_param freq 为value生成字典
bigram_dic = dict(bigram_param_list)
file_count = 0
while 1:
#按照bigram_param进行排序,返回key(index)值组成的List
sorted_bigram_dic_keys_list = sorted(bigram_dic.iterkeys(), key=lambda x:bigram_dic[x].split('\t')[0])
# print sum([int(item.encode('utf-8').split('\t')[1]) for item in bigram_dic.itervalues()])
#排序后字典内第一个元素,查找与该元素相等的元素
if len(sorted_bigram_dic_keys_list) == 0:
break
first_index = sorted_bigram_dic_keys_list[0]
first_item_in_bigram_dic_splited = bigram_dic[first_index].split('\t')
first_bigram_param = first_item_in_bigram_dic_splited[0]
freq_int = int(first_item_in_bigram_dic_splited[1])
bigram_dic.pop(first_index)
try:
bigram_dic[first_index] = eval('next(fileObj%s)'%first_index)
except:
file_count += 1
print file_count
if file_count == self.TOTAL_FILE_COUNT:
break
for sorted_index in sorted_bigram_dic_keys_list[1:]:
if first_bigram_param == bigram_dic[sorted_index].split('\t')[0]:
freq_int += int(bigram_dic[sorted_index].split('\t')[1])
bigram_dic.pop(sorted_index)
try:
bigram_dic[sorted_index] = eval('next(fileObj%s)'%sorted_index)
except:
file_count += 1
print file_count
if file_count == self.TOTAL_FILE_COUNT:
break
com_str = '\t'.join((first_bigram_param, str(freq_int)))
com_fileObj.write(com_str+'\n')
def cut_off_param(self,un_cut_filename='combine_word_freq.txt',cutOff=1):
'''按cutOff设定值进行裁剪'''
cut_off_set= set([item for item in range(1, 1+cutOff)])
un_cut_off_filename = os.path.join(self.src_data_file_path, un_cut_filename)
assert os.path.isfile(un_cut_off_filename)
cuted_filename = un_cut_filename.split('.')[0] + '_cutOff=%s.txt'%cutOff
cuted_off_filename = os.path.join(self.src_data_file_path, cuted_filename)
read_file_with_readlines = False
with codecs.open(un_cut_off_filename, encoding='utf-8') as f, \
codecs.open(cuted_off_filename, mode='wb', encoding='utf-8') as wf:
if not read_file_with_readlines:#如果数据比较大,则逐行读取数据
while 1:
line = f.readline()
if not line:
break
splited_line = line.split('\t')
n_gram_item = splited_line[0]
freq = int(splited_line[-1])
if freq in cut_off_set:
continue
else:
freq -= cutOff
com_str = '\t'.join((n_gram_item, str(freq))) + '\n'
wf.write(com_str)
else:#如果数据在内存允许范围之内,则一次性读取到内存中,速度是逐行读取的10倍
for line in f.readlines():
splited_line = line.split('\t')
n_gram_item = splited_line[0]
freq = int(splited_line[-1])
if freq in cut_off_set:
continue
else:
freq -= cutOff
com_str = '\t'.join((n_gram_item, str(freq))) + '\n'
wf.write(com_str)
def put_ngram_item_into_different_file(self, combine_filename):
'''把ngram语言模型,根据n的值分到不同的文件中'''
cut_off_filename = os.path.join(self.src_data_file_path, combine_filename)
bigram_fileanme = os.path.join(self.src_data_file_path, 'n_gram_filename', 'bigram_from_fivegram_item.txt')
trigram_filename = os.path.join(self.src_data_file_path, 'n_gram_filename', 'trigram_from_fivegram_item.txt')
fourgram_fileanme = os.path.join(self.src_data_file_path, 'n_gram_filename', 'fourgram_from_fivegram_item.txt')
fivegram_fileanme = os.path.join(self.src_data_file_path, 'n_gram_filename', 'fivegram_from_fivegram_item.txt')
lenght_2_ngram_item_list = []
lenght_3_ngram_item_list = []
lenght_4_ngram_item_list = []
lenght_5_ngram_item_list = []
with codecs.open(cut_off_filename, encoding='utf-8') as f,\
codecs.open(fivegram_fileanme, mode='wb', encoding='utf-8')as wf:
for line in f.readlines():
splited_line = line.split('\t')
ngram_item = splited_line[0]
ngram_item_words_lenght = len(ngram_item.split(','))
if ngram_item_words_lenght == 2:
lenght_2_ngram_item_list.append(line)
elif ngram_item_words_lenght == 3:
lenght_3_ngram_item_list.append(line)
elif ngram_item_words_lenght == 4:
lenght_4_ngram_item_list.append(line)
else:
wf.write(line)
# lenght_5_ngram_item_list.append(line)
codecs.open(bigram_fileanme, mode='wb', encoding='utf-8').writelines(lenght_2_ngram_item_list)
codecs.open(trigram_filename, mode='wb', encoding='utf-8').writelines(lenght_3_ngram_item_list)
codecs.open(fourgram_fileanme, mode='wb', encoding='utf-8').writelines(lenght_4_ngram_item_list)
# codecs.open(fivegram_fileanme, mode='wb', encoding='utf-8').writelines(lenght_5_ngram_item_list)
# gn = GenNgram()
# gn.gen_bigram_data(ngram='trigram')
# gn.gen_n_igram_model_data(n=4)
# gn.mk_n_gram_inorder(gn.uninorder_file_pattern, gn.inorder_file_pattern)
# gn.combine_bigram_freq(gn.uncombine_inorder_file_pattern, gn.combine_filename)
# gn.cut_off_param(gn.combine_filename, cutOff=1)
# gn.put_ngram_item_into_different_file('five_gram_combine_word_freq.txt')
class CutoffNgramGenWeight(object):
def __init__(self):
self.src_data_file_path = r'E:\SVN\linguistic_model\N_gram'
def mk_n_gram_inorder(self, uninorder_filename):
'''N元组文件排序'''
ngram_filename = os.path.join(self.src_data_file_path, uninorder_filename)
with codecs.open(ngram_filename, encoding='utf-8') as f:
sorted_bigram_list = sorted(f.readlines(), key=lambda x:x.split('\t')[0])
inorder_filename = uninorder_filename.split('.')[0] + '_inorder.txt'
inorder_bigram_filename = os.path.join(self.src_data_file_path, inorder_filename)
codecs.open(inorder_bigram_filename, mode='wb', encoding='utf-8').writelines(sorted_bigram_list)
def cut_off_ngram_file(self, ngram_filename, cutoff=1, line_count=0):
'''二、三、四、五元模型文件
ngram_filename:待裁剪的文件
cutoff:裁剪值
line_count:裁剪后要保留行数
若设置了裁剪值,则所得文件为比该行数小的最靠近line_count的文件'''
ngram_file_path = os.path.join(self.src_data_file_path, 'bigram')
filename = os.path.join(ngram_file_path, ngram_filename)
pure_cutoff_filename = ngram_filename.split('.')[0]+'_cutoff=%s.txt'%cutoff
cutoff_filename = os.path.join(ngram_file_path, pure_cutoff_filename)
cutoff_line_list = []
total_line_count = 0
with codecs.open(filename, encoding='utf-8') as f:
for line in f.readlines():
#除去BOS的项
if line.startswith('BOS'):
continue
splited_line = line.split('\t')
ngram_item = splited_line[0]
#除去EOS的项
if ngram_item.endswith('EOS'):
continue
freq_int = int(splited_line[-1])
if freq_int > cutoff:
total_line_count += 1
cutoff_line_list.append(line)
if line_count:#如果设置了裁剪后数据的行数
if total_line_count > line_count:#文件行数大于设定值,cutoff值自增1,递归主函数做二次裁剪
new_cutoff_value = cutoff + 1
print 'total_line_count:%s cutoff_value:%s'%(total_line_count,new_cutoff_value)
cutoff_line_list[:] = []
self.cut_off_ngram_file(ngram_filename, new_cutoff_value, line_count)
else:#文件行数小宇设定值,则写入本地
codecs.open(cutoff_filename, mode='wb', encoding='utf-8').writelines(cutoff_line_list)
else:#若没有设定裁剪数据行数,则按照cutoff值,将裁剪的数据写入本地
codecs.open(cutoff_filename, mode='wb', encoding='utf-8').writelines(cutoff_line_list)
def unigram_weight(self):
filename = os.path.join(self.src_data_file_path, 'bigram', 'unigram_combine_word_freq.txt')
total_line_list = []
with codecs.open(filename, encoding='utf-8') as f:
line_list = f.readlines()
total_freq = sum([int(item.split('\t')[-1]) for item in line_list])
print total_freq
time.sleep(4)
for line in line_list:
splited_line = line.split('\t')
ngram_item = splited_line[0]
freq_int = int(splited_line[-1])
percentage = freq_int/float(total_freq)
weight = str(int(math.log10(percentage)*(-20000)))
com_str = ngram_item + '\t' + weight + '\n'
total_line_list.append(com_str)
filename_to_write = os.path.join(self.src_data_file_path, 'bigram', 'unigram_item_weight.txt')
codecs.open(filename_to_write, mode='wb', encoding='utf-8').writelines(total_line_list)
def bigram_weight(self):
'''计算二元模型中各个元素出现的概率'''
filename = os.path.join(self.src_data_file_path, 'bigram', 'bigram_combine_freq_inorder_cutoff=1.txt')
bos_count = 0
eos_count = 0
bigram_item_freq_dic = {}
with codecs.open(filename, encoding='utf-8') as f:
for line in f.readlines():
splited_line = line.split('\t')
ngram_item = splited_line[0]
freq_int = int(splited_line[-1])
if ngram_item.startswith('BOS'):
bos_count += freq_int
elif ngram_item.endswith('EOS'):
eos_count += freq_int
else:
try:
bigram_item_freq_dic[ngram_item[0]] += freq_int
except:
bigram_item_freq_dic[ngram_item[0]] = freq_int
# bigram_item_freq_dic['BOS'] = bos_count
# bigram_item_freq_dic['EOS'] = eos_count
total_line_list = []
with codecs.open(filename, encoding='utf-8') as f:
for line in f.readlines():
splited_line = line.split('\t')
ngram_item = splited_line[0]
freq_int = int(splited_line[-1])
if ngram_item.startswith('BOS'):
# first_word = ngram_item.split(',')[0]
percentage = freq_int/float(bos_count)
weight = str(int(math.log10(percentage)*(-20000)))
com_str = ngram_item + '\t' + weight + '\n'
# com_str = ngram_item + '\t' + str(percentage) + '\n'
total_line_list.append(com_str)
elif ngram_item.endswith('EOS'):
percentage = freq_int/float(eos_count)
weight = str(int(math.log10(percentage)*(-20000)))
com_str = ngram_item + '\t' + weight + '\n'
# com_str = ngram_item + '\t' + str(percentage) + '\n'
total_line_list.append(com_str)
else:
percentage = float(freq_int)/bigram_item_freq_dic[ngram_item[0]]
weight = str(int(math.log10(percentage)*(-20000)))
com_str = ngram_item + '\t' + weight + '\n'
total_line_list.append(com_str)
filename = os.path.join(self.src_data_file_path, 'bigram', 'bigram_item_weight.txt')
codecs.open(filename, mode='wb', encoding='utf-8').writelines(total_line_list)
def trigram_weight(self):
'''计算三元模型个元素的概率'''
trigram_filename = os.path.join(self.src_data_file_path, 'bigram', 'trigram_combine_freq_inorder_cutoff=18.txt')
eos_count = 0
first_word_freq_dic = {}
with codecs.open(trigram_filename, encoding='utf-8') as f:
for line in f.readlines():
splited_line = line.split('\t')
ngram_item = splited_line[0]
freq_int = int(splited_line[-1])
if ngram_item.startswith('BOS'):
try:
first_word_freq_dic[ngram_item[:-1]] += freq_int
except:
first_word_freq_dic[ngram_item[:-1]] = freq_int
#若以EOS结尾,则求解EOS发生的情况下,item为first_word_of_bigram_item的概率
elif ngram_item.endswith('EOS'):
eos_count += freq_int
else:
try:
first_word_freq_dic[ngram_item[:-1]] += freq_int
except:
first_word_freq_dic[ngram_item[:-1]] = freq_int
total_line_list = []
with codecs.open(trigram_filename, encoding='utf-8') as f:
for line in f.readlines():
splited_line = line.split('\t')
ngram_item = splited_line[0]
freq_int = int(splited_line[-1])
if ngram_item.startswith(u'BOS'):
percentage = freq_int/float(first_word_freq_dic[ngram_item[:-1]])
# percentage = freq_int/float(bos_count)
weight = str(int(math.log10(percentage)*(-20000)))
com_str = ngram_item + '\t' + weight + '\n'
total_line_list.append(com_str)
elif ngram_item.endswith('EOS'):
percentage = freq_int/float(eos_count)
weight = str(int(math.log10(percentage)*(-20000)))
com_str = ngram_item + '\t' + weight + '\n'
# com_str = ngram_item + '\t' + str(percentage) + '\n'
total_line_list.append(com_str)
else:
percentage = float(freq_int)/first_word_freq_dic[ngram_item[:-1]]
weight = str(int(math.log10(percentage)*(-20000)))
com_str = ngram_item + '\t' + weight + '\n'
total_line_list.append(com_str)
filename = os.path.join(self.src_data_file_path, 'bigram', 'trigram_item_weight.txt')
codecs.open(filename, mode='wb', encoding='utf-8').writelines(total_line_list)
def four_gram_weight(self, first_word_count=3):
'''计算四元模型个元素的概率'''
trigram_filename = os.path.join(self.src_data_file_path, 'bigram', 'four_gram_combine_word_freq_cutoff=17.txt')
eos_count = 0
first_word_freq_dic = {}
with codecs.open(trigram_filename, encoding='utf-8') as f:
for line in [item.replace(',', '') for item in f.readlines()]:
splited_line = line.split('\t')
ngram_item = splited_line[0]
freq_int = int(splited_line[-1])
if ngram_item.startswith('BOS'):
try:
first_word_freq_dic[ngram_item[:-1]] += freq_int
except:
first_word_freq_dic[ngram_item[:-1]] = freq_int
elif ngram_item.endswith('EOS'):
eos_count += freq_int
else:
try:
first_word_freq_dic[ngram_item[:-1]] += freq_int
except:
first_word_freq_dic[ngram_item[:-1]] = freq_int
total_line_list = []
with codecs.open(trigram_filename, encoding='utf-8') as f:
for line in (item.replace(',', '') for item in f.readlines()):
splited_line = line.split('\t')
ngram_item = splited_line[0]
freq_int = int(splited_line[-1])
if ngram_item.startswith('BOS'):
percentage = freq_int/float(first_word_freq_dic[ngram_item[:-1]])
weight = str(int(math.log10(percentage)*(-20000)))
com_str = ngram_item + '\t' + weight + '\n'
total_line_list.append(com_str)
elif ngram_item.endswith('EOS'):
percentage = freq_int/float(eos_count)
weight = str(int(math.log10(percentage)*(-20000)))
com_str = ngram_item + '\t' + weight + '\n'
total_line_list.append(com_str)
else:
percentage = float(freq_int)/first_word_freq_dic[ngram_item[:-1]]
weight = str(int(math.log10(percentage)*(-20000)))
com_str = ngram_item + '\t' + weight + '\n'
total_line_list.append(com_str)
filename = os.path.join(self.src_data_file_path, 'bigram', 'fourgram_item_weight.txt')
codecs.open(filename, mode='wb', encoding='utf-8').writelines(total_line_list)
def five_gram_weight(self, first_word_count=4):
'''计算五元模型个元素的概率'''
trigram_filename = os.path.join(self.src_data_file_path, 'bigram', 'fivegram_combine_word_freq_cutoff=53.txt')
eos_count = 0
first_word_freq_dic = {}
with codecs.open(trigram_filename, encoding='utf-8') as f:
for line in [item.replace(',','') for item in f.readlines()]:
splited_line = line.split('\t')
ngram_item = splited_line[0]
freq_int = int(splited_line[-1])
if ngram_item.startswith('BOS'):
try:
first_word_freq_dic[ngram_item[:-1]] += freq_int
except:
first_word_freq_dic[ngram_item[:-1]] = freq_int
elif ngram_item.endswith('EOS'):
eos_count += freq_int
else:
try:
first_word_freq_dic[ngram_item[:-1]] += freq_int
except:
first_word_freq_dic[ngram_item[:-1]] = freq_int
total_line_list = []
with codecs.open(trigram_filename, encoding='utf-8') as f:
for line in (item.replace(',','') for item in f.readlines()):
splited_line = line.split('\t')
ngram_item = splited_line[0]
freq_int = int(splited_line[-1])
if ngram_item.startswith('BOS'):
percentage = freq_int/float(first_word_freq_dic[ngram_item[:-1]])
weight = str(int(math.log10(percentage)*(-20000)))
com_str = ngram_item + '\t' + weight + '\n'
total_line_list.append(com_str)
elif ngram_item.endswith('EOS'):
percentage = freq_int/float(eos_count)
weight = str(int(math.log10(percentage)*(-20000)))
com_str = ngram_item + '\t' + weight + '\n'
total_line_list.append(com_str)
else:
percentage = float(freq_int)/first_word_freq_dic[ngram_item[:-1]]
weight = str(int(math.log10(percentage)*(-20000)))
com_str = ngram_item + '\t' + weight + '\n'
total_line_list.append(com_str)
filename = os.path.join(self.src_data_file_path, 'bigram', 'fivegram_item_weight.txt')
codecs.open(filename, mode='wb', encoding='utf-8').writelines(total_line_list)
def re_combine_weigh_file(self):
filepattern = '*weight.txt'
glob_path = os.path.join(self.src_data_file_path, 'bigram', filepattern)
filename_list = glob.glob(glob_path)
print filename_list
combine_line_list = []
for filename in filename_list:
with codecs.open(filename, encoding='utf-8') as f:
for line in f.readlines():
combine_line_list.append(line)
combine_filename = os.path.join(self.src_data_file_path, 'bigram', 'combine_ngram_weight.txt')
codecs.open(combine_filename, mode='wb', encoding='utf-8').writelines(combine_line_list)
def max_weight(self, ngram_weight_filename):
filename = os.path.join(self.src_data_file_path, 'bigram', ngram_weight_filename)
weight_set = set()
with codecs.open(filename, encoding='utf-8') as f:
for line in f.readlines():
splited_line = line.split('\t')
weight = int(splited_line[-1])
weight_set.add(weight)
print max(weight_set)#167426
cutoff = CutoffNgramGenWeight()
# cutoff.mk_n_gram_inorder('trigram_combine_freq.txt')
# cutoff.cut_off_ngram_file('bigram_combine_freq_inorder.txt', cutoff=2, line_count=25000000)
# cutoff.cut_off_ngram_file('trigram_combine_freq_inorder.txt', cutoff=18, line_count=5000000)
# cutoff.cut_off_ngram_file('four_gram_combine_word_freq.txt', cutoff=17, line_count=5300000)
# cutoff.cut_off_ngram_file('fivegram_combine_word_freq.txt', cutoff=53, line_count=800000)
# cutoff.unigram_weight()
# cutoff.bigram_weight()
# cutoff.trigram_weight()
# cutoff.four_gram_weight()
# cutoff.five_gram_weight()
# cutoff.re_combine_weigh_file()
# cutoff.max_weight('bigram_item_weight.txt')
# module_path = 'E:\SVN\chocolate_ime\script\gen_update_words\gen_hot_words'
# sys.path.append(module_path)
# from add_words_spell_m import WordsSearch
class Key9_InputRules(object):
'''初始化时,传入待转换文件的路径,调用convert_pinyin_to_rules方法时,传入其文件名,文件可以是\t隔开的第一列是待转换词'''
def __init__(self, src_path):
self.src_file_path = src_path
# self.src_filename = 'single_word_5329.txt'
# self.src_file_path = r'E:\SVN\linguistic_model\9_keys\0709modify\four_five_gram_item\arithmetic_param'
def convert_pinyin_to_rules(self, src_filename):
'''把基础词库中的拼音转换为输入规则(数字序列)'''
coding_map = {'a': '2', 'c': '2', 'b': '2', 'e': '3', 'd': '3', 'g': '4', 'f': '3', 'i': '4', 'h': '4', 'k': '5', 'j': '5', 'm': '6', 'l': '5', 'o': '6', 'n': '6', 'q': '7', 'p': '7', 's': '7', 'r': '7', 'u': '8', 't': '8', 'w': '9', 'v': '8', 'y': '9', 'x': '9', 'z': '9'}
from add_pinyin_to_single_word import AddPinyin
addpinyin = AddPinyin()
base_filename = os.path.join(self.src_file_path, src_filename)
filename_without_suffix = src_filename.split('.')[0]
base_file_with_pinyin_role = os.path.join(self.src_file_path, '%s_pinyin_role'
'.txt'%filename_without_suffix)
with codecs.open(base_filename, encoding='utf-8') as f, \
codecs.open(base_file_with_pinyin_role, mode='wb', encoding='utf-8') as wf:
whole_word_list = (item.split('\t')[0] for item in f.readlines())
for word in whole_word_list:
pinyin_str = addpinyin.get_pinyin(word)
role_num = ''.join([coding_map[letter] for letter in pinyin_str if letter.isalpha()])
com_str = '\t'.join((word, pinyin_str, role_num))
wf.write(com_str+'\n')
self.mk_base_file_inorder(base_file_with_pinyin_role)
def mk_base_file_inorder(self, file_with_pinyin_roles):
'''基础词库按照输入规则进行排序'''
base_file_with_pinyin = os.path.join(self.src_file_path, file_with_pinyin_roles)
filename_without_suffix = base_file_with_pinyin.split('.')[0]
base_file_with_pinyin_inorder = os.path.join(self.src_file_path, '%s_inorder.txt'%filename_without_suffix)
with codecs.open(base_file_with_pinyin, encoding='utf-8') as f, \
codecs.open(base_file_with_pinyin_inorder, mode='wb', encoding='utf-8') as wf:
temp_list_for_write = sorted(f.readlines(), key=lambda x:x.split('\t')[2])
wf.writelines(temp_list_for_write)
self.gen_role_num_words_mapping(base_file_with_pinyin_inorder)
os.remove(os.path.join(self.src_file_path, file_with_pinyin_roles))
def gen_role_num_words_mapping(self, file_with_pinyin_inorder):
'''以输入规则(数字序列)为key,该序列对应的多个汉字(逗号隔开)作为value,生成文件'''
total_mapping_dic = {}
base_file_with_pinyin_inorder_filename = os.path.join(self.src_file_path, file_with_pinyin_inorder)
filename_without_suffix = file_with_pinyin_inorder.split('.')[0]
mapping_base_file_with_pinyin_inorder_filename = os.path.join(self.src_file_path, '%s_mapping_9key.txt'%filename_without_suffix)
with codecs.open(base_file_with_pinyin_inorder_filename, encoding='utf-8') as f, \
codecs.open(mapping_base_file_with_pinyin_inorder_filename, mode='wb', encoding='utf-8') as wf:
for line in f.readlines():
splited_line = line.split('\t')
word = splited_line[0]
role_num = splited_line[2].strip()
check_value = total_mapping_dic.get(role_num)
if not check_value:
total_mapping_dic[role_num] = [word]
else:
total_mapping_dic[role_num].append(word)
for role_num, word_str_list in total_mapping_dic.iteritems():
com_str = '\t'.join((role_num, ','.join(word_str_list)))
wf.write(com_str+'\n')
self.mapping_file_inorder(mapping_base_file_with_pinyin_inorder_filename)
os.remove(os.path.join(self.src_file_path, file_with_pinyin_inorder))
def mapping_file_inorder(self, mapping_file):
'''按照输入规则进行排序'''
mapping_base_file_with_pinyin_inorder_filename = os.path.join(self.src_file_path, mapping_file)
with codecs.open(mapping_base_file_with_pinyin_inorder_filename, encoding='utf-8') as f:
temp_list_for_write = sorted(f.readlines(), key=lambda x:x.split('\t')[0])
with codecs.open(mapping_base_file_with_pinyin_inorder_filename, mode='wb', encoding='utf-8') as wf:
wf.writelines(temp_list_for_write)
self.get_prefix_of_mapping_role(mapping_file)
def get_prefix_of_mapping_role(self, mapping_file):
'''输出所有输入规则的真前缀'''
mapping_filename = os.path.join(self.src_file_path, mapping_file)
total_profix_set = set()
with codecs.open(mapping_filename, encoding='utf-8') as f:
for line in f.readlines():
splited_line = line.split('\t')
role_num = splited_line[0]
for i in range(1, len(role_num)):
total_profix_set.add(role_num[0:i]+'\n')
prefix_filename = os.path.join(self.src_file_path, 'prefix_if_mapping_role_num.txt')
temp_list_for_write = sorted(total_profix_set, key=lambda x:x.strip())
codecs.open(prefix_filename, mode='wb', encoding='utf-8').writelines(temp_list_for_write)
# roles = Key9_InputRules(r'E:\SVN\linguistic_model\N_gram\data')
# roles.convert_pinyin_to_rules('single_word_5329.txt')
# roles.mk_base_file_inorder('single_word_5329_pinyin_role_9key.txt')
class Key26_InputRules(object):
def __init__(self):
pass
def convert_pinyin_to_rules(self):
'''把基础词库中的拼音转换为输入规则(数字序列)'''
from add_pinyin_to_single_word import AddPinyin
addpinyin = AddPinyin()
coding_map = {'a': '2', 'c': '2', 'b': '2', 'e': '3', 'd': '3', 'g': '4', 'f': '3', 'i': '4', 'h': '4', 'k': '5', 'j': '5', 'm': '6', 'l': '5', 'o': '6', 'n': '6', 'q': '7', 'p': '7', 's': '7', 'r': '7', 'u': '8', 't': '8', 'w': '9', 'v': '8', 'y': '9', 'x': '9', 'z': '9'}
base_filename = os.path.join(PATH, 'data', 'single_word_5329.txt')
base_file_with_pinyin = os.path.join(PATH, 'data', 'single_word_5329_pinyin_role_26key.txt')
with codecs.open(base_filename, encoding='utf-8') as f, \
codecs.open(base_file_with_pinyin, mode='wb', encoding='utf-8') as wf:
whole_word_list = (item.split('\t')[0] for item in f.readlines())
for word in whole_word_list:
pinyin_str = addpinyin.get_pinyin(word)
com_str = '\t'.join((word, pinyin_str))
wf.write(com_str+'\n')
def mk_base_file_inorder(self):
'''基础词库按照输入规则进行排序'''
base_file_with_pinyin = os.path.join(PATH, 'data', 'single_word_5329_pinyin_role_26key.txt')
base_file_with_pinyin_inorder = os.path.join(PATH, 'data', 'single_word_5329_pinyin_role_inorder_26key.txt')
with codecs.open(base_file_with_pinyin, encoding='utf-8') as f, \
codecs.open(base_file_with_pinyin_inorder, mode='wb', encoding='utf-8') as wf:
temp_list_for_write = sorted(f.readlines(), key=lambda x:x.split('\t')[-1])
wf.writelines(temp_list_for_write)
def gen_role_num_words_mapping(self):
'''以输入规则(数字序列)为key,该序列对应的多个汉字(逗号隔开)作为value,生成文件'''
total_mapping_dic = {}
base_file_with_pinyin_inorder_filename = os.path.join(PATH, 'data', 'single_word_5329_pinyin_role_inorder_26key.txt')
mapping_base_file_with_pinyin_inorder_filename = os.path.join(PATH, 'data', 'single_word_5329_pinyin_role_inorder_mapping_26key.txt')
with codecs.open(base_file_with_pinyin_inorder_filename, encoding='utf-8') as f, \
codecs.open(mapping_base_file_with_pinyin_inorder_filename, mode='wb', encoding='utf-8') as wf:
for line in f.readlines():
splited_line = line.split('\t')
word = splited_line[0]
role_num = splited_line[-1].strip()
check_value = total_mapping_dic.get(role_num)
if not check_value:
total_mapping_dic[role_num] = [word]
else:
total_mapping_dic[role_num].append(word)
# print total_mapping_dic
for role_num, word_str_list in total_mapping_dic.iteritems():
com_str = '\t'.join((role_num, ','.join(word_str_list)))
wf.write(com_str+'\n')
def mapping_file_inorder(self):
'''按照输入规则进行排序'''
mapping_base_file_with_pinyin_inorder_filename = os.path.join(PATH, 'data', 'single_word_5329_pinyin_role_inorder_mapping_26key.txt')
with codecs.open(mapping_base_file_with_pinyin_inorder_filename, encoding='utf-8') as f:
temp_list_for_write = sorted(f.readlines(), key=lambda x:x.split('\t')[0])
with codecs.open(mapping_base_file_with_pinyin_inorder_filename, mode='wb', encoding='utf-8') as wf:
wf.writelines(temp_list_for_write)
def get_prefix_of_mapping_role(self):
'''输出所有输入规则的真前缀'''
mapping_filename = os.path.join(PATH, 'data', 'single_word_5329_pinyin_role_inorder_mapping_26key.txt')
total_profix_set = set()
with codecs.open(mapping_filename, encoding='utf-8') as f:
for line in f.readlines():
splited_line = line.split('\t')
role_num = splited_line[0]
if len(role_num) == 1:
print line.strip()
for i in range(1, len(role_num)):
total_profix_set.add(role_num[0:i]+'\n')
prefix_filename = os.path.join(PATH, 'data', 'prefix_if_mapping_role_num_26key.txt')
temp_list_for_write = sorted(total_profix_set, key=lambda x:x.strip())
codecs.open(prefix_filename, mode='wb', encoding='utf-8').writelines(temp_list_for_write)
# get_prefix_of_mapping_role()
# key26 = Key26_InputRules()
# key26.convert_pinyin_to_rules()
# key26.mk_base_file_inorder()
# key26.gen_role_num_words_mapping()
# key26.get_prefix_of_mapping_role()
class EngineArithmetic(object):
def __init__(self):
self.src_data_path = r'E:\SVN\linguistic_model\N_gram\bigram'
self.ngram_weight_dic = {}
self._load_ngram_weight()
def _load_ngram_weight(self):
ngram_item_filename = os.path.join(self.src_data_path, 'combine_ngram_weight.txt')
with codecs.open(ngram_item_filename, encoding='utf-8') as f:
for line in f.readlines():
splited_line = line.split('\t')
ngram_item = splited_line[0]
weight = int(splited_line[-1])
self.ngram_weight_dic[ngram_item] = weight
def caculate_weight_of_word_list(self, words_list):
'''返回每一行测试数据的总概率'''
words_list_lenght = len(words_list)
sentence_weight = 0
for word_index in range(words_list_lenght):
if word_index < 4:
ngram_item = 'BOS' + ''.join(words_list[:word_index+1])
weight = self.ngram_weight_dic.get(ngram_item, 200000)
print ngram_item, weight
sentence_weight += weight
# if word_index == 0:
# ngram_item = 'BOS' + words_list[0]
# weight = self.ngram_weight_dic.get(ngram_item, 200000)
# elif word_index == 1:
# try:
# ngram_item = 'BOS' + ''.join(words_list[:word_index+1])#BOS,0,1三元模型
# weight = self.ngram_weight_dic[ngram_item]
# except:
# ngram_item = 'BOS' + ''.join(words_list[1:word_index+1])#退至BOS,
# weight = self.ngram_weight_dic.get(ngram_item, )
elif words_list_lenght >= 4:
try:
ngram_item = ''.join((words_list[word_index-4], words_list[word_index-3], words_list[word_index-2], words_list[word_index-1], words_list[word_index]))
weight = self.ngram_weight_dic[ngram_item]
except KeyError:
try:
ngram_item = ''.join((words_list[word_index-3], words_list[word_index-2], words_list[word_index-1], words_list[word_index]))
weight = self.ngram_weight_dic[ngram_item]+10000
except KeyError:
try:
ngram_item = ''.join((words_list[word_index-2], words_list[word_index-1], words_list[word_index]))
weight = self.ngram_weight_dic[ngram_item]+50000
except KeyError:
ngram_item = ''.join((words_list[word_index-1], words_list[word_index]))
weight = self.ngram_weight_dic.get(ngram_item, 100000)+100000
print ngram_item, weight
sentence_weight += weight
try:
ngram_item = ''.join(words_list[-4:]) + 'EOS'
weight = self.ngram_weight_dic[ngram_item]
except KeyError:#若五元模型不存在该元素,则退至四元模型
try:
ngram_item = ''.join(words_list[-3:])+ 'EOS'
weight = self.ngram_weight_dic[ngram_item]+10000
except KeyError:
try:
ngram_item = ''.join(words_list[-2:]) + 'EOS'
weight = self.ngram_weight_dic[ngram_item]+50000
except KeyError:
ngram_item = ''.join(words_list[-1:]) + 'EOS'
weight = self.ngram_weight_dic.get(ngram_item, 100000)+100000
print ngram_item, weight
sentence_weight += weight
print sentence_weight
return [(words_list,sentence_weight)]
def receive_sentence_list(self, prefix_list, candidate_list, max_lenght=1000):
'''接收sentence的列表,求出相应的权重值,并返回权重值较低的max_length个'''
sentence_list = [list(''.join(item)) for item in itertools.product(prefix_list, candidate_list)]
sentence_weight_tuple_list = []
for word_list in sentence_list:
sentence_weight_tuple = self.caculate_weight_of_word_list(word_list)
sentence_weight_tuple_list.append(sentence_weight_tuple)
return sorted(sentence_weight_tuple_list, key=lambda x:x[-1])[:max_lenght]
# ea = EngineArithmetic()
# while 1:#计算在没有路径裁剪情况下句子的实际权重
# sentence = raw_input('input your sentence:')
# word_list = [item for item in sentence.decode('utf-8')]
# ea.caculate_weight_of_word_list(word_list)