-
Notifications
You must be signed in to change notification settings - Fork 161
/
Copy pathbuild_dictionary.py
1165 lines (977 loc) · 38.9 KB
/
build_dictionary.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
""" Desc: A script to automate the building of multiple dictionaries based on
known areas of concern due to the original source of the data. The
script can be run from the source directly (-P and -p) once a
sutable text file is obtained. It can also be run on a previously
generated word frequency list to remove known problem areas.
Author: Tyler Barrus
Notes: The original inputs are from OpenSubtitles (http://opus.nlpl.eu/OpenSubtitles2018.php):
English Input: http://opus.nlpl.eu/download.php?f=OpenSubtitles/v2018/mono/OpenSubtitles.raw.en.gz
Spanish Input: http://opus.nlpl.eu/download.php?f=OpenSubtitles/v2018/mono/OpenSubtitles.raw.es.gz
German Input: http://opus.nlpl.eu/download.php?f=OpenSubtitles/v2018/mono/OpenSubtitles.raw.de.gz
French Input: http://opus.nlpl.eu/download.php?f=OpenSubtitles/v2018/mono/OpenSubtitles.raw.fr.gz
Portuguese Input: http://opus.nlpl.eu/download.php?f=OpenSubtitles/v2018/mono/OpenSubtitles.raw.pt.gz
Russian Input: http://opus.nlpl.eu/download.php?f=OpenSubtitles/v2018/mono/OpenSubtitles.raw.ru.gz
Arabic Input: http://opus.nlpl.eu/download.php?f=OpenSubtitles/v2018/mono/OpenSubtitles.raw.ar.gz
Basque Input: http://opus.nlpl.eu/download.php?f=tiOpenSubtles/v2018/mono/OpenSubtitles.raw.eu.gz
Latvian Input: https://huggingface.co/datasets/RaivisDejus/latvian-text
Dutch Input: http://opus.nlpl.eu/download.php?f=OpenSubtitles/v2018/mono/OpenSubtitles.raw.nl.gz
Italian Input: http://opus.nlpl.eu/download.php?f=OpenSubtitles/v2018/mono/OpenSubtitles.raw.it.gz
Requirements:
The script requires more than the standard library to run in its
entirety. You will also need to install the NLTK package to build a
dictionary from scratch. Otherwise, no additional packages are
required.
"""
import contextlib
import gzip
import json
import os
import string
from collections import Counter
STRING_PUNCTUATION = tuple(string.punctuation)
DIGETS = tuple(string.digits)
MINIMUM_FREQUENCY = 50
@contextlib.contextmanager
def load_file(filename, encoding="utf-8"):
"""Context manager to handle opening a gzip or text file correctly and
reading all the data
Args:
filename (str): The filename to open
encoding (str): The file encoding to use
Yields:
str: The string data from the file read
"""
if filename[-3:].lower() == ".gz":
with gzip.open(filename, mode="rt", encoding=encoding) as fobj:
yield fobj
else:
with open(filename, mode="r", encoding=encoding) as fobj:
yield fobj
def load_include_exclude(filename, encoding="utf-8"):
with load_file(filename=filename, encoding=encoding) as f:
for line in f:
if line[0] == "#":
continue
line = line.strip().split()
for l in line:
yield l.strip().lower()
def export_word_frequency(filepath, word_frequency):
"""Export a word frequency as a json object
Args:
filepath (str):
word_frequency (Counter):
"""
with open(filepath, "w") as f:
json.dump(word_frequency, f, indent="", sort_keys=True, ensure_ascii=False)
def build_word_frequency(filepath, language, output_path):
"""Parse the passed in text file (likely from Open Subtitles) into
a word frequency list and write it out to disk
Args:
filepath (str):
language (str):
output_path (str):
Returns:
Counter: The word frequency as parsed from the file
Note:
This only removes words that are proper nouns (attempts to...) and
anything that starts or stops with something that is not in the alphabet.
"""
# NLTK is only needed in this portion of the project
try:
import nltk # type: ignore
from nltk.tag import pos_tag # type: ignore
from nltk.tokenize import WhitespaceTokenizer # type: ignore
from nltk.tokenize.toktok import ToktokTokenizer # type: ignore
except ImportError as ex:
raise ImportError("To build a dictioary from scratch, NLTK is required!\n{}".format(ex.message))
nltk.download("averaged_perceptron_tagger")
word_frequency = Counter()
if language in ["es", "it"]:
tok = ToktokTokenizer()
else:
tok = WhitespaceTokenizer()
idx = 0
with load_file(filepath, "utf-8") as fobj:
for line in fobj:
# tokenize into parts
parts = tok.tokenize(line)
# Attempt to remove proper nouns
# Remove things that have leading or trailing non-alphabetic characters.
tagged_sent = pos_tag(parts)
words = [
word[0].lower()
for word in tagged_sent
if word[0] and not word[1] == "NNP" and word[0][0].isalpha() and word[0][-1].isalpha()
]
# print(words)
if words:
word_frequency.update(words)
idx += 1
if idx % 100000 == 0:
print("completed: {} rows".format(idx))
# end file loop
print("completed: {} rows".format(idx))
export_word_frequency(output_path, word_frequency)
return word_frequency
def export_misfit_words(misfit_filepath, word_freq_filepath, word_frequency):
with load_file(word_freq_filepath, "utf-8") as f:
source_word_frequency = json.load(f)
source_words = set(source_word_frequency.keys())
final_words = set(word_frequency.keys())
misfitted_words = source_words.difference(final_words)
misfitted_words = sorted(list(misfitted_words))
with open(misfit_filepath, "w+") as file:
for word in misfitted_words:
file.write(word)
file.write("\n")
def clean_english(word_frequency, filepath_exclude, filepath_include, filepath_dictionary):
"""Clean an English word frequency list
Args:
word_frequency (Counter):
filepath_exclude (str):
filepath_include (str):
"""
letters = set("abcdefghijklmnopqrstuvwxyz'")
# remove words with invalid characters
invalid_chars = list()
for key in word_frequency:
kl = set(key)
if kl.issubset(letters):
continue
invalid_chars.append(key)
for misfit in invalid_chars:
word_frequency.pop(misfit)
# remove words without a vowel
no_vowels = list()
vowels = set("aeiouy")
for key in word_frequency:
if vowels.isdisjoint(key):
no_vowels.append(key)
for misfit in no_vowels:
word_frequency.pop(misfit)
# Remove double punctuations (a-a-a-able) or (a'whoppinganda'whumping)
double_punc = list()
for key in word_frequency:
if key.count("'") > 1 or key.count("-") > 1 or key.count(".") > 2:
double_punc.append(key)
for misfit in double_punc:
word_frequency.pop(misfit)
# remove ellipses
ellipses = list()
for key in word_frequency:
if ".." in key:
ellipses.append(key)
for misfit in ellipses:
word_frequency.pop(misfit)
# leading or trailing doubles a, "a'", "zz", ending y's
doubles = list()
for key in word_frequency:
if key.startswith("aa") and key not in ("aardvark", "aardvarks"):
doubles.append(key)
elif key.startswith("a'"):
doubles.append(key)
elif key.startswith("zz"):
doubles.append(key)
elif key.endswith("yy"):
doubles.append(key)
elif key.endswith("hh"):
doubles.append(key)
for misfit in doubles:
word_frequency.pop(misfit)
# common missing spaces
missing_spaces = list()
for key in word_frequency:
if key.startswith("about") and key != "about":
missing_spaces.append(key)
elif key.startswith("above") and key != "above":
missing_spaces.append(key)
elif key.startswith("after") and key != "after":
missing_spaces.append(key)
elif key.startswith("against") and key != "against":
missing_spaces.append(key)
elif key.startswith("all") and word_frequency[key] < 15:
missing_spaces.append(key)
elif key.startswith("almost") and key != "almost":
missing_spaces.append(key)
# This one has LOTS of possibilities...
elif key.startswith("to") and word_frequency[key] < 25:
missing_spaces.append(key)
elif key.startswith("can't") and key != "can't":
missing_spaces.append(key)
elif key.startswith("i'm") and key != "i'm":
missing_spaces.append(key)
for misfit in missing_spaces:
word_frequency.pop(misfit)
# TODO: other possible fixes?
# remove small numbers
small_frequency = list()
for key in word_frequency:
if word_frequency[key] <= MINIMUM_FREQUENCY:
small_frequency.append(key)
for misfit in small_frequency:
word_frequency.pop(misfit)
# remove flagged misspellings
for line in load_include_exclude(filepath_exclude):
if line in word_frequency:
word_frequency.pop(line)
# Use a dictionary to clean up everything else...
final_words_to_remove = []
with load_file(filepath_dictionary) as fobj:
dictionary_words = []
for line in fobj:
if line[0].lower() in letters:
line = line.lower().strip()
dictionary_words.append(line)
for word in word_frequency:
if word not in dictionary_words:
final_words_to_remove.append(word)
for word in final_words_to_remove:
word_frequency.pop(word)
for word in dictionary_words:
if word not in word_frequency:
word_frequency[word] = MINIMUM_FREQUENCY
# Add known missing words back in (ugh)
for line in load_include_exclude(filepath_include):
if line in word_frequency:
print("{} is already found in the dictionary! Skipping!".format(line))
else:
word_frequency[line] = MINIMUM_FREQUENCY
return word_frequency
def clean_spanish(word_frequency, filepath_exclude, filepath_include, filepath_dictionary):
"""Clean a Spanish word frequency list
Args:
word_frequency (Counter):
filepath_exclude (str):
filepath_include (str):
"""
letters = set("abcdefghijklmnopqrstuvwxyzáéíóúüñ")
# fix issues with words containing other characters
invalid_chars = list()
for key in word_frequency:
kl = set(key)
if kl.issubset(letters):
continue
invalid_chars.append(key)
for misfit in invalid_chars:
word_frequency.pop(misfit)
# fix issues with more than one accent marks
# NOTE: Not sure there are any occurances but this is not possible as a valid word!
duplicate_accents = list()
for key in word_frequency:
if (key.count("á") + key.count("é") + key.count("í") + key.count("ó") + key.count("ú")) > 1:
duplicate_accents.append(key)
for misfit in duplicate_accents:
word_frequency.pop(misfit)
# fix misplaced "ü" marks
# NOTE: the ü must be just after a g and before an e or i only (with or without accent)!
misplaced_u = list()
for key in word_frequency:
if not "ü" in key:
continue
idx = key.index("ü")
if idx == 0 or idx == len(key) - 1: # first or last letter
misplaced_u.append(key)
continue
if key[idx - 1] != "g" and key[idx + 1] not in "eéií":
misplaced_u.append(key)
for misfit in misplaced_u:
word_frequency.pop(misfit)
# ción issues
cion_issues = list()
for key in word_frequency:
if not key.endswith("cion"):
continue
base = key[:-4]
n_key = "{}ción".format(base)
if n_key in word_frequency:
cion_issues.append(key)
for misfit in cion_issues:
word_frequency.pop(misfit)
# remove words that start with a double a ("aa")
double_a = list()
for key in word_frequency:
if key.startswith("aa"):
double_a.append(key)
for misfit in double_a:
word_frequency.pop(misfit)
# TODO: other possible fixes?
# remove small numbers
small_frequency = list()
for key in word_frequency:
if word_frequency[key] <= MINIMUM_FREQUENCY:
small_frequency.append(key)
for misfit in small_frequency:
word_frequency.pop(misfit)
# remove flagged misspellings
for line in load_include_exclude(filepath_exclude):
if line in word_frequency:
word_frequency.pop(line)
# Use a dictionary to clean up everything else...
final_words_to_remove = []
with load_file(filepath_dictionary) as fobj:
dictionary_words = []
for line in fobj:
if line[0].lower() in letters:
line = line.lower().strip()
dictionary_words.append(line)
for word in word_frequency:
if word not in dictionary_words:
final_words_to_remove.append(word)
for word in final_words_to_remove:
word_frequency.pop(word)
for word in dictionary_words:
if word not in word_frequency:
word_frequency[word] = MINIMUM_FREQUENCY
# Add known missing words back in (ugh)
for line in load_include_exclude(filepath_include):
if line in word_frequency:
print("{} is already found in the dictionary! Skipping!".format(line))
else:
word_frequency[line] = MINIMUM_FREQUENCY
return word_frequency
def clean_italian(word_frequency, filepath_exclude, filepath_include, filepath_dictionary):
letters = set("abcdefghijklmnopqrstuvwxyzáéíóúüàèìòù")
# fix issues with words containing other characters
invalid_chars = list()
for key in word_frequency:
kl = set(key)
if kl.issubset(letters):
continue
invalid_chars.append(key)
for misfit in invalid_chars:
word_frequency.pop(misfit)
# remove small numbers
small_frequency = list()
for key in word_frequency:
if word_frequency[key] <= MINIMUM_FREQUENCY:
small_frequency.append(key)
for misfit in small_frequency:
word_frequency.pop(misfit)
# TODO: other possible fixes?
# remove flagged misspellings
for line in load_include_exclude(filepath_exclude):
if line in word_frequency:
word_frequency.pop(line)
# Use a dictionary to clean up everything else...
final_words_to_remove = []
with load_file(filepath_dictionary) as fobj:
dictionary_words = []
for line in fobj:
if line[0].lower() in letters:
line = line.lower().strip()
dictionary_words.append(line)
for word in word_frequency:
if word not in dictionary_words:
final_words_to_remove.append(word)
for word in final_words_to_remove:
word_frequency.pop(word)
for word in dictionary_words:
if word not in word_frequency:
word_frequency[word] = MINIMUM_FREQUENCY
# Add known missing words back in (ugh)
for line in load_include_exclude(filepath_include):
if line in word_frequency:
print("{} is already found in the dictionary! Skipping!".format(line))
else:
word_frequency[line] = MINIMUM_FREQUENCY
return word_frequency
def clean_german(word_frequency, filepath_exclude, filepath_include, filepath_dictionary):
"""Clean a German word frequency list
Args:
word_frequency (Counter):
filepath_exclude (str):
filepath_include (str):
"""
letters = set("abcdefghijklmnopqrstuvwxyzäöüß")
# fix issues with words containing other characters
invalid_chars = list()
for key in word_frequency:
kl = set(key)
if kl.issubset(letters):
continue
invalid_chars.append(key)
for misfit in invalid_chars:
word_frequency.pop(misfit)
# remove words that start with a double a ("aa")
double_a = list()
for key in word_frequency:
if key.startswith("aa"):
double_a.append(key)
for misfit in double_a:
word_frequency.pop(misfit)
# TODO: other possible fixes?
# remove small numbers
small_frequency = list()
for key in word_frequency:
if word_frequency[key] <= MINIMUM_FREQUENCY:
small_frequency.append(key)
for misfit in small_frequency:
word_frequency.pop(misfit)
# remove flagged misspellings
for line in load_include_exclude(filepath_exclude):
if line in word_frequency:
word_frequency.pop(line)
# Use a dictionary to clean up everything else...
final_words_to_remove = []
with load_file(filepath_dictionary) as fobj:
dictionary_words = []
for line in fobj:
if line[0].lower() in letters:
line = line.lower().strip()
dictionary_words.append(line)
for word in word_frequency:
if word not in dictionary_words:
final_words_to_remove.append(word)
for word in final_words_to_remove:
word_frequency.pop(word)
for word in dictionary_words:
if word not in word_frequency:
word_frequency[word] = MINIMUM_FREQUENCY
# Add known missing words back in (ugh)
for line in load_include_exclude(filepath_include):
if line in word_frequency:
print("{} is already found in the dictionary! Skipping!".format(line))
else:
word_frequency[line] = MINIMUM_FREQUENCY
return word_frequency
def clean_french(word_frequency, filepath_exclude, filepath_include, filepath_dictionary):
"""Clean a French word frequency list
Args:
word_frequency (Counter):
filepath_exclude (str):
filepath_include (str):
"""
letters = set("abcdefghijklmnopqrstuvwxyzéàèùâêîôûëïüÿçœæ")
# fix issues with words containing other characters
invalid_chars = list()
for key in word_frequency:
kl = set(key)
if kl.issubset(letters):
continue
invalid_chars.append(key)
for misfit in invalid_chars:
word_frequency.pop(misfit)
# remove words that start with a double a ("aa")
double_a = list()
for key in word_frequency:
if key.startswith("aa"):
double_a.append(key)
for misfit in double_a:
word_frequency.pop(misfit)
# TODO: other possible fixes?
# remove small numbers
small_frequency = list()
for key in word_frequency:
if word_frequency[key] <= MINIMUM_FREQUENCY:
small_frequency.append(key)
for misfit in small_frequency:
word_frequency.pop(misfit)
# remove flagged misspellings
for line in load_include_exclude(filepath_exclude):
if line in word_frequency:
word_frequency.pop(line)
# Use a dictionary to clean up everything else...
final_words_to_remove = []
with load_file(filepath_dictionary) as fobj:
dictionary_words = []
for line in fobj:
if line[0].lower() in letters:
line = line.lower().strip()
dictionary_words.append(line)
for word in word_frequency:
if word not in dictionary_words:
final_words_to_remove.append(word)
for word in final_words_to_remove:
word_frequency.pop(word)
for word in dictionary_words:
if word not in word_frequency:
word_frequency[word] = MINIMUM_FREQUENCY
# Add known missing words back in (ugh)
for line in load_include_exclude(filepath_include):
if line in word_frequency:
print("{} is already found in the dictionary! Skipping!".format(line))
else:
word_frequency[line] = MINIMUM_FREQUENCY
return word_frequency
def clean_portuguese(word_frequency, filepath_exclude, filepath_include, filepath_dictionary):
"""Clean a Portuguese word frequency list
Args:
word_frequency (Counter):
filepath_exclude (str):
filepath_include (str):
"""
letters = set("abcdefghijklmnopqrstuvwxyzáâãàçéêíóôõú")
# fix issues with words containing other characters
invalid_chars = list()
for key in word_frequency:
kl = set(key)
if kl.issubset(letters):
continue
invalid_chars.append(key)
for misfit in invalid_chars:
word_frequency.pop(misfit)
# remove words that start with a double a ("aa")
double_a = list()
for key in word_frequency:
if key.startswith("aa"):
double_a.append(key)
for misfit in double_a:
word_frequency.pop(misfit)
# TODO: other possible fixes?
# remove small numbers
small_frequency = list()
for key in word_frequency:
if word_frequency[key] <= MINIMUM_FREQUENCY:
small_frequency.append(key)
for misfit in small_frequency:
word_frequency.pop(misfit)
# remove flagged misspellings
for line in load_include_exclude(filepath_exclude):
if line in word_frequency:
word_frequency.pop(line)
# Use a dictionary to clean up everything else...
final_words_to_remove = []
with load_file(filepath_dictionary, encoding="latin-1") as fobj:
dictionary_words = []
for line in fobj:
if line[0].lower() in letters:
line = line.lower().strip()
dictionary_words.append(line)
for word in word_frequency:
if word not in dictionary_words:
final_words_to_remove.append(word)
for word in final_words_to_remove:
word_frequency.pop(word)
for word in dictionary_words:
if word not in word_frequency:
word_frequency[word] = MINIMUM_FREQUENCY
# Add known missing words back in (ugh)
for line in load_include_exclude(filepath_include):
if line in word_frequency:
print("{} is already found in the dictionary! Skipping!".format(line))
else:
word_frequency[line] = MINIMUM_FREQUENCY
return word_frequency
def clean_russian(word_frequency, filepath_exclude, filepath_include):
"""Clean an Russian word frequency list
Args:
word_frequency (Counter):
filepath_exclude (str):
filepath_include (str):
"""
letters = set("абвгдеёжзийклмнопрстуфхцчшщъыьэюя")
# remove words with invalid characters
invalid_chars = list()
for key in word_frequency:
kl = set(key)
if kl.issubset(letters):
continue
invalid_chars.append(key)
for misfit in invalid_chars:
word_frequency.pop(misfit)
# remove words without a vowel
no_vowels = list()
vowels = set("аеёиоуыэюя")
for key in word_frequency:
if vowels.isdisjoint(key):
no_vowels.append(key)
for misfit in no_vowels:
word_frequency.pop(misfit)
# remove ellipses
ellipses = list()
for key in word_frequency:
if ".." in key:
ellipses.append(key)
for misfit in ellipses:
word_frequency.pop(misfit)
# leading or trailing doubles a, "a'", "zz", ending y's
doubles = list()
for key in word_frequency:
if key.startswith("аа") and key not in ("аарон", "аарона", "аарону"):
doubles.append(key)
elif key.startswith("ээ") and key not in ("ээг"):
doubles.append(key)
for misfit in doubles:
word_frequency.pop(misfit)
# TODO: other possible fixes?
# remove small numbers
small_frequency = list()
for key in word_frequency:
if word_frequency[key] <= MINIMUM_FREQUENCY:
small_frequency.append(key)
for misfit in small_frequency:
word_frequency.pop(misfit)
# remove flagged misspellings
for line in load_include_exclude(filepath_exclude):
if line in word_frequency:
word_frequency.pop(line)
# Add known missing words back in (ugh)
for line in load_include_exclude(filepath_include):
if line in word_frequency:
print("{} is already found in the dictionary! Skipping!".format(line))
else:
word_frequency[line] = MINIMUM_FREQUENCY
return word_frequency
def clean_arabic(word_frequency, filepath_exclude, filepath_include):
"""Clean an Arabic word frequency list
Args:
word_frequency (Counter):
filepath_exclude (str):
filepath_include (str):
"""
letters = set("دجحإﻹﻷأآﻵخهعغفقثصضذطكمنتالبيسشظزوةىﻻرؤءئ")
# remove words with invalid characters
invalid_chars = list()
for key in word_frequency:
kl = set(key)
if kl.issubset(letters):
continue
invalid_chars.append(key)
for misfit in invalid_chars:
word_frequency.pop(misfit)
# remove ellipses
ellipses = list()
for key in word_frequency:
if ".." in key:
ellipses.append(key)
for misfit in ellipses:
word_frequency.pop(misfit)
# TODO: other possible fixes?
# remove small numbers
small_frequency = list()
for key in word_frequency:
if word_frequency[key] <= MINIMUM_FREQUENCY:
small_frequency.append(key)
for misfit in small_frequency:
word_frequency.pop(misfit)
# remove flagged misspellings
for line in load_include_exclude(filepath_exclude):
if line in word_frequency:
word_frequency.pop(line)
# Add known missing words back in (ugh)
for line in load_include_exclude(filepath_include):
if line in word_frequency:
print("{} is already found in the dictionary! Skipping!".format(line))
else:
word_frequency[line] = MINIMUM_FREQUENCY
return word_frequency
def clean_basque(word_frequency, filepath_exclude, filepath_include):
"""Clean a Basque word frequency list
Args:
word_frequency (Counter):
filepath_exclude (str):
filepath_include (str):
"""
letters = set("abcdefghijklmnopqrstuvwxyzñ")
# fix issues with words containing other characters
invalid_chars = list()
for key in word_frequency:
kl = set(key)
if kl.issubset(letters):
continue
invalid_chars.append(key)
for misfit in invalid_chars:
word_frequency.pop(misfit)
# remove words that start with a double a ("aa")
double_a = list()
for key in word_frequency:
if key.startswith("aa"):
double_a.append(key)
for misfit in double_a:
word_frequency.pop(misfit)
# TODO: other possible fixes?
# remove small numbers
small_frequency = list()
for key in word_frequency:
if word_frequency[key] <= MINIMUM_FREQUENCY:
small_frequency.append(key)
for misfit in small_frequency:
word_frequency.pop(misfit)
# remove flagged misspellings
for line in load_include_exclude(filepath_exclude):
if line in word_frequency:
word_frequency.pop(line)
# Add known missing words back in (ugh)
for line in load_include_exclude(filepath_include):
if line in word_frequency:
print("{} is already found in the dictionary! Skipping!".format(line))
else:
word_frequency[line] = MINIMUM_FREQUENCY
return word_frequency
def clean_latvian(word_frequency, filepath_exclude, filepath_include):
"""Clean a Latvian word frequency list
Args:
word_frequency (Counter):
filepath_exclude (str):
filepath_include (str):
"""
letters = set("aābcčdeēfgģhiījkķlļmnņoprsštuūvzž")
# remove words with invalid characters
invalid_chars = list()
for key in word_frequency:
kl = set(key)
if kl.issubset(letters):
continue
invalid_chars.append(key)
for misfit in invalid_chars:
word_frequency.pop(misfit)
# remove words without a vowel
no_vowels = list()
vowels = set("aāiīeēouū")
for key in word_frequency:
if vowels.isdisjoint(key):
no_vowels.append(key)
for misfit in no_vowels:
word_frequency.pop(misfit)
# remove ellipses
ellipses = list()
for key in word_frequency:
if ".." in key:
ellipses.append(key)
for misfit in ellipses:
word_frequency.pop(misfit)
# leading or trailing doubles aa or ii
doubles = list()
for key in word_frequency:
if key.startswith("аа"):
doubles.append(key)
elif key.startswith("ii"):
doubles.append(key)
for misfit in doubles:
word_frequency.pop(misfit)
# remove single letters
single_letters = list()
for key in word_frequency:
if len(key) == 1:
single_letters.append(key)
for misfit in single_letters:
word_frequency.pop(misfit)
# TODO: other possible fixes?
# remove small numbers
small_frequency = list()
for key in word_frequency:
if word_frequency[key] <= MINIMUM_FREQUENCY:
small_frequency.append(key)
for misfit in small_frequency:
word_frequency.pop(misfit)
# remove flagged misspellings
for line in load_include_exclude(filepath_exclude):
if line in word_frequency:
word_frequency.pop(line)
# Add known missing words back in (ugh)
for line in load_include_exclude(filepath_include):
if line in word_frequency:
print("{} is already found in the dictionary! Skipping!".format(line))
else:
word_frequency[line] = MINIMUM_FREQUENCY
return word_frequency
def clean_dutch(word_frequency, filepath_exclude, filepath_include, filepath_dictionary):
"""Clean a Dutch word frequency list
Args:
word_frequency (Counter):
filepath_exclude (str):
filepath_include (str):
"""
letters = set("abcdefghijklmnopqrstuvwxyz'")
# remove words with invalid characters
invalid_chars = list()
for key in word_frequency:
kl = set(key)
if kl.issubset(letters):
continue
invalid_chars.append(key)
for misfit in invalid_chars:
word_frequency.pop(misfit)
# remove words without a vowel
no_vowels = list()
vowels = set("aeiouy")
for key in word_frequency:
if vowels.isdisjoint(key):
no_vowels.append(key)
for misfit in no_vowels:
word_frequency.pop(misfit)
# Remove double punctuations (a-a-a-able) or (a'whoppinganda'whumping)
double_punc = list()
for key in word_frequency:
if key.count("'") > 1 or key.count("-") > 1 or key.count(".") > 2:
double_punc.append(key)
for misfit in double_punc:
word_frequency.pop(misfit)
# remove ellipses
ellipses = list()
for key in word_frequency:
if ".." in key:
ellipses.append(key)
for misfit in ellipses:
word_frequency.pop(misfit)
# leading or trailing doubles a, "a'", "zz", ending y's
doubles = list()
for key in word_frequency:
if key.startswith("aa") and key not in ("aardvark", "aardvarks"):
doubles.append(key)
elif key.startswith("a'"):
doubles.append(key)
elif key.startswith("zz"):
doubles.append(key)
elif key.endswith("yy"):
doubles.append(key)
elif key.endswith("hh"):
doubles.append(key)
for misfit in doubles:
word_frequency.pop(misfit)
# common missing spaces
missing_spaces = list()
for key in word_frequency:
if key.startswith("about") and key != "about":
missing_spaces.append(key)
elif key.startswith("above") and key != "above":
missing_spaces.append(key)
elif key.startswith("after") and key != "after":
missing_spaces.append(key)
elif key.startswith("against") and key != "against":
missing_spaces.append(key)
elif key.startswith("all") and word_frequency[key] < 15:
missing_spaces.append(key)
elif key.startswith("almost") and key != "almost":
missing_spaces.append(key)
# This one has LOTS of possibilities...