-
Notifications
You must be signed in to change notification settings - Fork 0
/
word_classifier.py
166 lines (139 loc) · 6.22 KB
/
word_classifier.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
import logging
from dataclasses import dataclass
from typing import List
from typing import Optional
from django.db.models import QuerySet
from py_cmu_dict.apps.core.models import Dictionary
from py_cmu_dict.support.iter_utils import flatten
from py_cmu_dict.support.iter_utils import index_of_first
logger = logging.getLogger(__name__)
@dataclass(frozen=True)
class Homophone:
word_or_symbol: str
phonemic: str
phonetic: str
_english_vowels_diphthong = [
"eɪ",
"oʊ",
"aʊ",
"ɪə",
"eə",
"ɔɪ",
"aɪ",
"ʊə",
]
_english_vowels_short = [
"ɪ",
"e",
"æ",
"ʌ",
"ʊ",
"ɒ",
"ə",
]
def discover_rhymes(word_or_symbol: str, language_tag: str) -> Optional[List[str]]:
base_query_set = Dictionary.objects.filter(language__language_tag=language_tag)
word_from_database: Optional[Dictionary] = base_query_set.filter(word_or_symbol=word_or_symbol).first()
if not word_from_database:
logger.debug(f"No entry for {language_tag} / {word_or_symbol}")
return None
else:
updated_base_query_set = base_query_set.exclude(pk=word_from_database.pk)
if not word_from_database.ipa_phonemic_syllables:
logger.debug(f"No phonemic syllable available for {language_tag} / {word_or_symbol}")
return None
else:
if language_tag == "en-us":
return _discover_rhymes_for_cmu(word_from_database, updated_base_query_set)
return _discover_rhymes_standard_way(word_from_database, updated_base_query_set)
def _discover_rhymes_standard_way(word_or_symbol: Dictionary, base_query_set: QuerySet) -> Optional[List[str]]:
logger.debug("Discovering rhymes through STANDARD strategy by syllable...")
syllables = word_or_symbol.transform_ipa_syllable_entry_to_object()
number_of_syllables = len(syllables)
logger.debug(f"The word '{word_or_symbol}' has {number_of_syllables} syllables")
phonemic = _extract_phonemic_that_will_be_used_to_find_rhymes(syllables)
result = base_query_set.filter(ipa_phonemic__endswith=phonemic)
cleaned_result = [tuple_entry[0] for tuple_entry in result.values_list("word_or_symbol")]
logger.debug(f"Total rhymes words found: {len(cleaned_result)}")
return cleaned_result
def _extract_phonemic_that_will_be_used_to_find_rhymes(syllables: List[List[str]]) -> str:
# Naive implementation!
# https://en.m.wikipedia.org/wiki/Rhyme
# For certain there is something more technical in terms of linguistics...
number_of_syllables = len(syllables)
if number_of_syllables == 1:
syllables_to_be_used = syllables
else:
# All syllables, less the first one
syllables_to_be_used = syllables[1::]
inverted_syllables_to_be_used = syllables_to_be_used[::-1]
index_number_last_element = len(syllables_to_be_used) - 1
for index, entry in enumerate(inverted_syllables_to_be_used):
index_diphthong = index_of_first(entry, lambda item: item in _english_vowels_diphthong)
if index_diphthong:
return _valid_phonemic_from_list_of_syllables(
index,
entry,
index_diphthong,
syllables_to_be_used,
index_number_last_element,
)
index_short_vowel = index_of_first(entry, lambda item: item in _english_vowels_short)
if index_short_vowel:
return _valid_phonemic_from_list_of_syllables(
index,
entry,
index_short_vowel,
syllables_to_be_used,
index_number_last_element,
)
# Let's use the last syllable then!
# Remember: I'm wrapping in another list to comply with create_syllable_entry_ipa method
last_syllable = [syllables_to_be_used[-1]]
return Dictionary.create_syllable_entry_ipa(last_syllable)
def _valid_phonemic_from_list_of_syllables(
syllable_entry_index: int,
syllable_entry: List[str],
where_to_cut: int,
syllables: List[List[str]],
index_number_last_element: int,
) -> str:
phonemic_to_be_used = syllable_entry[where_to_cut::]
where_to_start = index_number_last_element - syllable_entry_index
syllables[where_to_start] = phonemic_to_be_used
syllables_to_be_used = syllables[where_to_start::]
syllables_to_be_used = [flatten(syllables_to_be_used)]
return Dictionary.create_syllable_entry_ipa(syllables_to_be_used)
def _discover_rhymes_for_cmu(word_or_symbol: Dictionary, base_query_set: QuerySet) -> Optional[List[str]]:
logger.debug("Discovering rhymes through ARPABET strategy")
# Some important variables
arpabet_stress_mark = "1"
separator_mark = Dictionary.arpabet_phoneme_separator_mark
phonemes = word_or_symbol.arpabet_phoneme.split(separator_mark)
# First try
phoneme_to_be_used = None
for index, phoneme in enumerate(phonemes):
if arpabet_stress_mark in phoneme:
phoneme_to_be_used = separator_mark.join(phonemes[index:])
# Then we should fallback to the common strategy
if not phoneme_to_be_used:
return _discover_rhymes_standard_way(word_or_symbol, base_query_set)
# Otherwise, we can try!
result = base_query_set.filter(arpabet_phoneme__endswith=phoneme_to_be_used)
cleaned_result = [tuple_entry[0] for tuple_entry in result.values_list("word_or_symbol")]
return cleaned_result
def discover_homophones(word_or_symbol: str, language_tag: str) -> Optional[List[Homophone]]:
base_query_set = Dictionary.objects.filter(language__language_tag=language_tag)
word_from_database: Optional[Dictionary] = base_query_set.filter(word_or_symbol=word_or_symbol).first()
if not word_from_database:
logger.debug(f"No entry for {language_tag} / {word_or_symbol}")
return None
else:
updated_base_query_set = base_query_set.exclude(pk=word_from_database.pk)
result = updated_base_query_set.filter(ipa_phonemic__exact=word_from_database.ipa_phonemic)
cleaned_result = [
Homophone(tuple_entry[0], tuple_entry[1], tuple_entry[2])
for tuple_entry in result.values_list("word_or_symbol", "ipa_phonemic", "ipa_phonetic")
]
logger.debug(f"Total homophones words found: {len(cleaned_result)}")
return cleaned_result