-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform_single_module.py
48 lines (39 loc) · 1.55 KB
/
transform_single_module.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
import pymorphy3
morph = pymorphy3.MorphAnalyzer()
numbers = ('sing', 'plur')
genders = ('masc', 'femn', 'neut')
cases = ('nomn', 'gent', 'datv', 'accs', 'ablt', 'loct')
persons = ('1per', '2per', '3per')
def process_word(word, remove_duplicates=False):
processed_word = morph.parse(word)[0]
results_set = set() if remove_duplicates else []
# Helper function to add results
def add_result(result):
if remove_duplicates:
results_set.add(result)
else:
if result not in results_set: # Mimic non-duplicate behavior without set
results_set.append(result)
if processed_word.tag.POS == 'NOUN':
for num in numbers:
for case in cases:
noun = processed_word.inflect({num, case})
if noun:
add_result(noun.word)
elif processed_word.tag.POS == 'INFN':
for person in persons:
for num in numbers:
verb = processed_word.inflect({num, person})
if verb:
add_result(verb.word)
elif processed_word.tag.POS == 'ADJF':
for num in numbers:
for gen in genders:
for case in cases:
adjective = processed_word.inflect({num, gen, case})
if adjective:
add_result(adjective.word)
if not results_set:
add_result(f'Failed to process the word: {word}')
return list(results_set) if remove_duplicates else results_set
# Keep the remainder of the file unchanged