-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlanguage_parser.py
More file actions
918 lines (771 loc) · 30.6 KB
/
Copy pathlanguage_parser.py
File metadata and controls
918 lines (771 loc) · 30.6 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
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
# coding: utf-8
"""
LANGUAGE_PARSER:
Contains LanguageParser class for parsing data in
a particular language from Wiktionary.
"""
from wiktionary_parser import *
class LanguageParser(WiktionaryParser):
ALPHABETS = {}
LEXICA = {}
def __init__(self, language):
WiktionaryParser.__init__(self)
self.language = language
self.url = self.url % self.language
# --> lexica
self.lexicon = self.init_lexicon(self.language)
self.add_lexicon(self.language, self.lexicon)
# --> alphabets
self.alphabets = self.fetch_alphabets()
self.alphabet = self.find_alphabet(self.language)
self.wordnet_words = self.get_wordnet_words()
self.words = self.wordnet_words.intersection(self.lexicon)
def reset_language(self, language):
"""
Sets this LanguageParser's language to the given language.
:param language: str, language to change to
:return: None
"""
if self.language != language:
self.refresh_json()
self.__init__(language)
# LEXICA
# ------
def init_lexicon(self, language=None, lim=None):
"""
Returns a set of all words in this language, up to lim.
~
If lim is None, returns all words.
:param language: str, language of lexicon to retrieve
:param lim: int, number of words in lexicon to retrieve
:return: List[str], words in LanguageParser's language
"""
lang_code = self.get_lang_code(language)
words = list()
if lang_code is not None:
path = self.PATH + "/resources/frequency_words/content/2016/%s/%s_full.txt" % (lang_code, lang_code)
with open(path, 'r') as lexicon:
line_no = 0
for line in lexicon:
word = line.split(" ", 1)[0]
words.append(self.unicodize(word))
if lim:
if line_no > lim:
break
line_no += 1
return words
def add_lexicon(self, language, lexicon):
"""
Adds the given lexicon to LEXICA under the given language.
:param language: str, language of lexicon
:param lexicon: List[str], all words in given language
:return: None
"""
self.LEXICA[language] = lexicon
def find_lexicon(self, language, lim=100000):
"""
Returns the lexicon for the given language.
~
If no lexicon for this language exists, creates new
lexicon for language, adds to LEXICA, and returns the result.
:param language: str, language of lexicon
:return: List[str], all words in given language
"""
try:
return self.LEXICA[language]
except KeyError:
lexicon = self.init_lexicon(language, lim)
self.add_lexicon(language, lexicon)
return lexicon
def in_lexicon(self, word, language=None):
"""
Returns True if this word is in this language's lexicon
or if this language's lexicon is empty (undetermined),
False otherwise.
:param word: str, word to check if in lexicon
:param language: str, language of lexicon
:return: bool, True if word in lexicon or empty lexicon
"""
lexicon = self.find_lexicon(language)
return word in lexicon or len(lexicon) == 0
def parse_lexicon(self, language):
"""
Parses plaintext lexicon in given language.
Returns a dict with all words in lexicon
as keys and corresponding lemma forms as values.
~
Each new lexical entry should be separated by "\n",
while inflected forms should be separated by ",".
~
Assumes first word on every line is the lemma form
of all subsequent words on the same line.
~
N.B. The same lemma value will often belong to multiple keys.
e.g. "kota, kocie, kot" -> {"kota":"kota", "kocie":"kota", "kot":"kota"}
:param language: str, language of .txt file for lexicon
:return: dict, where...
key (str) - inflected form of a word
val (str) - lemma form of inflected word
"""
return self.all_inflections(language)
def parse_custom_lexicon(self, language):
"""
Parses a custom lexicon for this language, if one exists.
~
Currently only supports French and Polish.
:param language: str, language to parse lexicon for
:return: dict, where...
key (str) - word lemma
val (List[str]) - all lexical forms of word lemma
"""
if language == "Polish":
fn = self.parse_polish_lexicon
elif language == "French":
fn = self.parse_french_lexicon
else:
return
filename = "/resources/lexica/" + language + ".txt"
with open(self.PATH + filename, "rb") as lex:
lexicon = fn(lex)
return lexicon
def parse_french_lexicon(self, lex):
"""
Parses plaintext file for French lexicon with
given filename. Returns a dict with all lexemes
in French lexicon as keys and corresponding
lemma forms as values.
~
Each new lexical entry should be separated by "\n",
while inflected forms should be separated by ",".
~
Assumes second word on every line is the lemma form
of previous words on the same line.
~
N.B. The same lemma value will often belong to
multiple lexemes.
e.g. "grande, grand" -> {"grand": ["grand", "grande"]}
:param lex: List[str], entries in French lexicon
:return: dict, where...
key (str) - word lemma
val (List[str]) - all lexical forms of word lemma
"""
lexicon = dict()
for line in lex:
line = line.strip("\n")
line = self.unicodize(line)
if line[-1] == "=":
lemma = line[:-2]
lexicon.setdefault(lemma, list())
lexicon[lemma].append(lemma)
else:
entry = line.split("\t")
lemma = entry[1]
lexeme = entry[0]
lexicon.setdefault(lexeme, list())
lexicon[lexeme].append(lemma)
return lexicon
def parse_polish_lexicon(self, lex):
"""
Parses plaintext file for Polish lexicon with
given filename. Returns a dict with all lexemes
in Polish lexicon as keys and corresponding
lemma forms as values.
~
Inflected forms in each lexical entry should be
separated by ",".
~
Assumes first word on every line is the lemma form
of following words on the same line.
~
N.B. The same lemma value will often belong to
multiple lexemes.
e.g. "kot, kota, kocie" -> {"kot": ["kot", "kota", "kocie"]}
:param lex: List[str], entries in Polish lexicon
:return: dict, where...
key (str) - word lemma
val (List[str]) - all lexical forms of word lemma
"""
lexicon = dict()
for line in lex:
line = line.strip("\n")
line = line.strip("\r")
line = self.unicodize(line)
lexemes = line.split(",")
lexemes = [l.strip() for l in lexemes]
lemma = lexemes[0]
lexicon[lemma] = lexemes
return lexicon
# ALPHABET
# --------
def init_alphabet(self, language=None):
"""
Returns a set of all alphabet letters in this language.
:param language: str, language of alphabet to retrieve
:return: List[str], alphabet letters in this language
"""
page = self.url_page(self.lemma_url(language))
table = page.find("table", attrs={"id": "toc"})
alphabet = set()
rows = table.findAll("tr")
if len(rows) > 1:
rows = rows[1:]
for row in rows:
line = row.findAll("td")[-1]
cells = line.findAll("a")
for cell in cells:
text = cell.getText()
if text != "Top":
line = self.unicodize(text)
letters = line.split(" ")
for letter in letters:
if len(letter) != 0 and "(" not in letter:
alphabet.add(letter)
alphabet.add(letter.lower())
alphabet.add(letter.upper())
return sorted(alphabet)
def find_alphabet(self, language=None):
"""
Returns the alphabet for the given language.
~
If no alphabet for this language exists, creates new
alphabet for language, adds to ALPHABETS, and returns the result.
:param language: str, language of alphabet
:return: List[str], all letters in given language's alphabet
"""
language = self.verify_language(language)
alphabet = self.fetch_alphabet(language)
if len(alphabet) == 0:
alphabet = self.init_alphabet(language)
self.alphabets[language] = alphabet
return sorted(alphabet)
def verify_word(self, word, language=None):
"""
Returns True if given word contains only characters from
this LanguageParser's alphabet, False otherwise.
:param word: str, word to verify whether in language
:param language: str, language to verify word with
:return: bool, whether word contains only characters from language
"""
language = self.verify_language(language)
word_chars = set(word)
alphabet = self.find_alphabet(language)
return len(word_chars.difference(alphabet)) == 0 and self.in_lexicon(word, language)
# JSON
# ----
def fetch_alphabets(self):
"""
Returns a memoized dictionary of alphabets in every language.
:return: dict(str, list), where str is language and list is alphabet
"""
return self.fetch_json("alphabets")
def fetch_alphabet(self, language=None):
"""
Returns the memoized alphabet for this language.
:return: dict(str, list), where str is language and list is alphabet
"""
language = self.verify_language(language)
return self.alphabets.setdefault(language, list())
def refresh_json(self):
"""
Dumps this LanguageParser's data from...
alphabets to alphabets.json, and
wiktionary_entries to wiktionary_entries.json.
:return: None
"""
self.refresh_alphabets()
self.refresh_wiktionary_entries()
def refresh_dict(self, dict_name):
"""
Refreshes the LanguageParser's language dictionary with dict_name.
~
Dict_name should be one of:
alphabets, or
wiktionary_entries.
:param dict_name: str, name of language dictionary to refresh
:return: None
"""
dict_obj = getattr(self, dict_name, None)
if dict_obj is not None:
entry = getattr(self, dict_name[:-1], None)
if entry is not None:
dict_obj[self.language] = sorted(entry)
self.dump_json(dict_obj, dict_name)
def refresh_alphabets(self):
"""
Dumps this LanguageParser's alphabets data to alphabets.json.
:return: None
"""
self.refresh_dict("alphabets")
# LEMMAS
# ------
def lemmatize(self, word, language=None, pos=None):
"""
Returns the lemma for this word in this language.
~
If word has no lemma, this method returns the given word.
~
e.g. lemmatize("drove", "English", "Verb") -> "drive"
lemmatize("yeux", "French", "Noun") -> "œil"
:param word: str, word to find lemma for
:param language: str, language of given word
:param poses: Set[str], parts-of-speech for output lemma
:return: str, lemma for given word
"""
lemmas = self.word_lemmas(word, language, pos)
if len(lemmas) != 0:
return lemmas[0]
else:
return word
def word_lemmas(self, word, language=None, poses=None):
"""
Returns the lemmas for this word in this language.
~
e.g. word_lemmas("driven", "English") -> ["drive", "driven"]
:param word: str, word to find lemmas for
:param language: str, language of given word
:param poses: Set[str], parts-of-speech for output lemmas
:return: List[str], lemmas for given word
"""
lemmas = OrderedSet([])
if len(word) == 0:
return lemmas.items()
if poses is None:
poses = self.PARTS_OF_SPEECH
language = self.verify_language(language)
word = self.unicodize(self.entry_word(word, language))
declension = self.lookup_word_inflections(word, language)
if declension is not None:
lemmas.add(word)
else:
etym = self.find_word_inflections(word, language)
if etym is not None:
lemmas.add(word)
else:
stemwords = self.find_stemwords(word, language, poses)
lemmas.update(stemwords)
if len(lemmas) == 0:
headwords = self.find_headwords(word, language, poses)
lemmas.update(headwords)
return [lemma for lemma in lemmas.items() if self.verify_word(lemma, language)]
def uninflect(self, word, language=None):
"""
Returns the uninflected form of this word in this language.
:param word: str, word to uninflect
:param language: str, language of word
:return: str, word uninflected
"""
language = self.verify_language(language)
for word_entry in self.wiktionary_entries:
inflections = self.lookup_word_inflections(word_entry, language)
if inflections is not None:
for inflection in inflections:
if inflection == word:
return word_entry
else:
return self.lemmatize(word)
# IPAS
# ----
def word_ipa(self, word, language=None):
"""
Transcribes the given word to IPA. Returns this word's
IPA pronunciation and adds its transcription to the given
language's IPA dictionary.
:param word: str, word to transcribe to IPA
:param language: str, word's language
:return: unicode, IPA transcription of word
"""
ipas = self.word_ipas(word, language)
if len(ipas) != 0:
return ipas[0]
else:
return
def word_ipas(self, word, language=None):
"""
Transcribes the given word to IPAs.
:param word: str, word to transcribe to IPA
:param language: str, word's language
:return: List[unicode], IPA transcriptions of word
"""
language = self.verify_language(language)
entry = self.find_wiktionary_subentry(word, language, u"Pronunciation")
if len(entry) == 0:
ipa = self.etymology_ipa(word, language)
if ipa is not None:
entry.insert(0, ipa)
self.edit_wiktionary_entry(word, language, u"Pronunciation", entry)
return entry
def words_ipa(self, words):
"""
Transcribes the given words to IPA.
:param word: List[str], words to transcribe to IPA
:return: List[unicode], IPA transcriptions of words
"""
ipas = list()
for word in words:
ipa = self.word_ipa(word)
if ipa is not None:
ipas.append(ipa)
return ipas
def find_word_ipas(self, word, language=None):
"""
Transcribes the given word to IPAs.
:param word: str, word to transcribe to IPA
:param language: str, word's language
:return: List[unicode], IPA transcriptions of word
"""
language = self.verify_language(language)
try:
return self.wiktionary_entries[word][language][u"Pronunciation"]
except KeyError:
return list()
def etymology_ipa(self, word, language=None):
"""
Returns this word's IPA constructed from its etymology.
:param word: str, word to return etymology's IPA of
:param language: str, language of word
:return: (unicode) str, this word's etymology's IPA
"""
language = self.verify_language(language)
etyms = self.find_word_etymology(word, language)
ipas = []
if etyms is not None and len(etyms) != 0:
for etym in etyms:
etym_ipa = self.word_ipa(etym)
if etym_ipa is None:
return
else:
ipas += etym_ipa
return "".join(ipas)
# PARTS-OF-SPEECH
# ---------------
def word_pos(self, word, language=None):
"""
Returns the given word's part of speech.
:param word: str, word to get part of speech of
:param language: str, word's language
:return: str, word's part of speech
"""
poses = self.word_poses(word, language)
if len(poses) != 0:
return poses[0]
def word_poses(self, word, language=None):
"""
Returns the given word's parts of speech.
:param word: str, word to get parts of speech of
:param language: str, word's language
:return: List[str], word's parts of speech
"""
language = self.verify_language(language)
word_entry = self.find_wiktionary_entry(word, language)
if word_entry is not None:
poses = [sect for sect in word_entry
if sect in self.PARTS_OF_SPEECH]
pos_set = OrderedSet(poses)
return pos_set.items()
else:
return list()
def words_pos(self, words, language=None):
"""
Returns a list of the given words' parts of speech.
:param word: List[str], words to get parts of speech of
:param language: str, words' language
:return: List[str], words' parts of speech
"""
pos = list()
for word in words:
pos.append(self.word_pos(word, language))
return pos
def words_poses(self, words, language=None):
"""
Returns a list of the given words' parts of speech.
:param word: List[str], words to get parts of speech of
:param language: str, words' language
:return: List[Set(str)], words' parts of speech
"""
poses = list()
for word in words:
poses.append(self.word_poses(word, language))
return poses
# MORPHEMES
# ---------
def word_morphemes(self, word, language=None):
"""
Returns a set of morphemes in this word.
:param word: str, word to find morphemes of
:param language: str, language of morpehemes
:return: List[str], word's morphemes
"""
word = self.entry_word(word, language)
return self.lookup_word_etymology(word, language)
def recursive_all_word_morphemes(self, word, language=None):
"""
Returns a set of morphemes in this word, including the
morphemes for all its sub-morphemes.
~
e.g. all_word_morphemes("undeniable", "English") -> ["un-", "deny", "-able"]
:param word: str, word to find morphemes of
:param language: str, language of morpehemes
:return: List[str], word's morphemes
"""
language = self.verify_language(language)
word_morphemes = self.word_morphemes(word, language)
if word_morphemes is not None:
submorphemes = OrderedSet(word_morphemes)
for morpheme in word_morphemes:
print morpheme
submorphemes.update(self.recursive_all_word_morphemes(morpheme))
else:
submorphemes = OrderedSet([])
return submorphemes
def all_word_morphemes(self, word, language=None):
"""
Returns a set of morphemes in this word, including the
morphemes for all its sub-morphemes.
~
e.g. all_word_morphemes("undeniable", "English") -> ["un-", "deny", "-able"]
:param word: str, word to find morphemes of
:param language: str, language of morpehemes
:return: List[str], word's morphemes
"""
language = self.verify_language(language)
morphemes = OrderedSet([])
todo = {word}
while len(todo) != 0:
curr_word = todo.pop()
if len(curr_word) != 0 and curr_word not in morphemes.items_set and not self.contains_punct(curr_word):
morphemes.add(curr_word)
word_morphemes = self.word_morphemes(curr_word, language)
if word_morphemes is not None:
morphemes.update(word_morphemes)
todo.update(word_morphemes)
return morphemes.order_items()
def words_morphemes(self, words, language=None):
"""
Returns a list of morphemes in these words.
:param word: str, word to find morphemes of
:param language: str, language of morpehemes
:return: List[str], words' morphemes
"""
language = self.verify_language(language)
morphemes = list()
for word in words:
word_morphemes = self.all_word_morphemes(word, language)
morphemes += word_morphemes
return morphemes
# LOOKUPS & FINDS
# ---------------
# Use lookup to "look up" existing entries,
# use find to "find" new entries if none exist.
# ---------------
def lookup_word_inflections(self, word, language):
"""
Returns all inflected forms of this word in wiktionary_entries.
:param word: str, word for declension to get inflections of
:return: List[str], all inflected forms of word
"""
try:
return self.wiktionary_entries[word][language][u"Declension"]
except KeyError:
return
def find_word_inflections(self, word, language):
"""
Returns all inflected forms of this word from Wiktionary.
:param word: str, word for declension to get inflections of
:return: List[str], all inflected forms of word
"""
return self.find_wiktionary_subentry(word, language, u"Declension")
def lookup_word_etymology(self, word, language=None):
"""
Returns the etymology of the given word in this
language (according to Wiktionary).
:param word: str, word to find etymology of
:param language: str, language of etymology
:return: List[str], etymological roots of this word
"""
language = self.verify_language(language)
return self.lookup_wiktionary_subentry(word, language, u"Etymology")
def find_word_etymology(self, word, language):
"""
Returns the etymology of this word from Wiktionary.
:param word: str, word to lookup etymology of
:return: List[str], etymological roots of this word
"""
language = self.verify_language(language)
return self.find_wiktionary_subentry(word, language, u"Etymology")
# ALL ENTRIES
# -----------
def all_inflections(self, language=None):
"""
Returns a dictionary of all words in this language's declensions.
:param language: str, language of declension dicts
:return: dict(str, str), inflection-lemma pairs in this language
"""
language = self.verify_language(language)
declensions = dict()
for word in self.wiktionary_entries:
inflections = self.lookup_word_inflections(word, language)
if inflections is not None:
for inflection in inflections:
declensions.setdefault(inflection, list())
declensions[inflection] += word
return declensions
def all_morphemes(self, language=None):
"""
Returns all words in this language's declension.
:param language: str, language of inflection dict
:return: List[str], all morphemes in this language
"""
language = self.verify_language(language)
morphemes = list()
for word in self.wiktionary_entries:
etyms = self.word_morphemes(word, language)
if etyms is not None:
morphemes += etyms
return morphemes
def all_ipas(self, language=None):
"""
Returns all IPAs in this language's pronunciations.
:param language: str, language of pronunciations
:return: dict(str, str), inflection-lemma pairs in this language
"""
language = self.verify_language(language)
ipas = dict()
for word in self.wiktionary_entries:
word_ipas = self.find_word_ipas(word, language)
if word_ipas is not None:
ipas.setdefault(word, list())
ipas[word] = OrderedSet(ipas[word] + word_ipas).items()
return ipas
# WORDNET
# -------
def get_wordnet_words(self):
"""
Returns a set of all words in Wordnet.
:return: Set(str), Wordnet word
"""
if self.language == "English":
entries = set()
path = self.PATH + "/resources/wordnet/wn_s.txt"
with open(path, "r") as synsets:
for synset in synsets:
synset = synset[2:-3]
info = synset.split(",")
name = info[2]
name = name[1:-1]
if " " not in name:
entries.add(name)
return entries
else:
lexicon = self.parse_lexicon(self.language)
return set(lexicon.keys())
def get_wordnet_word_pairs(self):
"""
Returns a list of 2-tuples consisting of a Wordnet word and
its corresponding parts of speech.
:return: Set(tuple(str,str)), Wordnet word and pos
"""
path = self.PATH + "/resources/wordnet/wn_s.txt"
entries = set()
with open(path, "r") as synsets:
for synset in synsets:
synset = synset[2:-3]
info = synset.split(",")
name = info[2]
name = name[1:-1]
pos = info[3]
if " " not in name:
pair = (self.unicodize(name), self.unicodize(pos))
entries.add(pair)
return entries
# COMMON WORDS
# ------------
def common_words(self, language=None, lim=50000):
"""
Returns the most common words in this language up to lim.
:param lim: int, lim <= 50000, number of words to retrieve
:param language: Optional[str], language of common words
:return: List[str], 50k most common words in language
"""
lang_code = self.get_lang_code(language)
if lang_code is None:
return
words = list()
path = self.PATH + "/resources/frequency_words/content/2016/%s/%s_50k.txt" % (lang_code, lang_code)
with open(path, 'r') as fifty_k:
line_no = 0
for line in fifty_k:
word = line.split(" ", 1)[0]
word = self.unicodize(word)
words.append(word)
if line_no > lim:
break
line_no += 1
return words
def common_word_pairs(self, language=None, lim=50000):
"""
Returns the most common word-pos pairs in this language
up to lim.
:param lim: int, number of common words to return
:param language: Optional[str], language of common words
:return: List[tuple(str, str)], Wordnet word and pos
"""
language = self.verify_language(language)
common_words = self.common_words(language, lim)
if language == "English":
word_pairs = self.get_wordnet_word_pairs()
return [word_pair for word_pair in word_pairs if word_pair[0] in common_words]
else:
poses = self.words_poses(common_words)
word_pairs = list()
for i in range(len(common_words)):
word = common_words[i]
pos = poses[i]
if len(pos) == 0:
pos.append(None)
for p in pos:
pair = (self.unicodize(word), self.unicodize(p))
word_pairs.append(pair)
return word_pairs
def common_morphemes(self, language=None, lim=50000):
"""
Returns the most common words in this language up to lim,
including their constituent words.
:param lim: int, lim <= 50000, number of words to retrieve
:param language: Optional[str], language of common morpemes
:return: List[str], up to 50k most common words in LanguageParser's language
"""
language = self.verify_language(language)
lexicon = self.find_lexicon(language, lim)
common_words = lexicon[:lim]
morphemes = self.words_morphemes(common_words, language)
return morphemes
def add_common_wiktionary_entries(self, language=None, lim=50000):
"""
Adds Wiktionary entries for the most common words in
this language to this WiktionaryParser's wiktionary_entries.
:param words: List[str], words of Wiktionary entries to add
:param language: Optional[str], language of entries to add
:return: dict(str, dict), where str is language and dict is...
key (str) - title of subentry heading (e.g. Etymology)
val (list) - value(s) associated with subentry
"""
language = self.verify_language(language)
return self.add_wiktionary_entries(self.common_words(language, lim), language)
# URLS
# ----
def lemma_url(self, language=None):
"""
Returns the Wiktionary URL for lemmas in this language.
:param language: str, language of URL
:return: str, Wiktionary URL for this parser's lemmas
"""
language = self.verify_language(language)
return (self.WIKI_URL + self.END_URL + self.LEMMA_PATH) % language
def ipa_url(self, language=None):
"""
Returns the Wiktionary URL for IPAs in this language.
:param language: str, language of IPA URL
:return: str, Wiktionary URL for this parser's IPAs
"""
language = self.verify_language(language)
return (self.WIKI_URL + self.END_URL + self.IPA_PATH) % (language, language)