Skip to content

Commit 02535ef

Browse files
committed
Refactor conjugations generation into a separate class
1 parent edfa321 commit 02535ef

File tree

2 files changed

+52
-59
lines changed

2 files changed

+52
-59
lines changed

src/conjugation_generator.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import re
2+
from config import Directories
3+
from file_utils import load_json
4+
5+
6+
class ConjugationGenerator:
7+
def __init__(self):
8+
self.irregular =\
9+
load_json(f"{Directories.processed_data}/irregular_verbs.json")
10+
self.vowel = "[aeiouy]"
11+
self.consonant = "[b-df-hj-np-tv-z]"
12+
13+
def imperative(self, infinitive):
14+
return infinitive
15+
16+
def third_person(self, infinitive):
17+
if re.search(f"{self.consonant}y$", infinitive):
18+
return infinitive[:-1] + "ies"
19+
if re.search("(s|z|ch|sh|x)$", infinitive) or infinitive.endswith("o"):
20+
return infinitive + "es"
21+
return infinitive + "s"
22+
23+
def gerund(self, infinitive):
24+
if infinitive.endswith("e"):
25+
return infinitive[:-1] + "ing"
26+
if re.search(f"{self.consonant}{self.vowel}{self.consonant}$", infinitive) \
27+
and not infinitive.endswith("fix"):
28+
return infinitive + infinitive[-1] + "ing"
29+
return infinitive + "ing"
30+
31+
def past_tense(self, infinitive):
32+
if infinitive in self.irregular:
33+
return self.irregular[infinitive]
34+
if infinitive.endswith("e"):
35+
return infinitive + "d"
36+
if re.search(f"{self.consonant}{self.vowel}{self.consonant}$", infinitive) \
37+
and not infinitive.endswith("fix"):
38+
return infinitive + infinitive[-1] + "ed"
39+
if re.search(f"{self.consonant}y$", infinitive):
40+
return infinitive[:-1] + "ied"
41+
return infinitive + "ed"

src/generate_conjugations.py

Lines changed: 11 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,17 @@
11
#!/usr/bin/env python3
22
from config import Directories
3-
from file_utils import load_txt_into_set, load_json, open_file_dir_safe
4-
import re
3+
from conjugation_generator import ConjugationGenerator
4+
from file_utils import load_txt_into_set, open_file_dir_safe
55

66
infinitives = load_txt_into_set(f"{Directories.processed_data}/infinitive.txt")
7-
irregular = load_json(f"{Directories.processed_data}/irregular_verbs.json")
87

9-
imperative_file = open_file_dir_safe(f"{Directories.processed_data}/imperative.txt", 'w')
10-
third_person_file = open_file_dir_safe(f"{Directories.processed_data}/third_person.txt", 'w')
11-
gerund_file = open_file_dir_safe(f"{Directories.processed_data}/gerund.txt", 'w')
12-
past_tense_file = open_file_dir_safe(f"{Directories.processed_data}/past_tense.txt", 'w')
8+
generator = ConjugationGenerator()
139

14-
vowel = "[aeiouy]"
15-
consonant = "[b-df-hj-np-tv-z]"
16-
17-
for infinitive in infinitives:
18-
# imperative
19-
# I know this is a duplicity but the two files have
20-
# a slightly different meaning and purpose
21-
imperative_file.write(infinitive + "\n")
22-
23-
# third person
24-
if re.search(f"{consonant}y$", infinitive):
25-
third_person = infinitive[:-1] + "ies"
26-
elif re.search("(s|z|ch|sh|x)$", infinitive) or infinitive.endswith("o"):
27-
third_person = infinitive + "es"
28-
else:
29-
third_person = infinitive + "s"
30-
third_person_file.write(third_person + "\n")
31-
32-
# gerund
33-
if infinitive.endswith("e"):
34-
gerund = infinitive[:-1] + "ing"
35-
elif re.search(f"{consonant}{vowel}{consonant}$", infinitive):
36-
gerund = infinitive + infinitive[-1] + "ing"
37-
else:
38-
gerund = infinitive + "ing"
39-
gerund_file.write(gerund + "\n")
40-
41-
# past tense
42-
if infinitive in irregular:
43-
past_tense = irregular[infinitive]
44-
elif infinitive.endswith("e"):
45-
past_tense = infinitive + "d"
46-
elif re.search(f"{consonant}{vowel}{consonant}$", infinitive):
47-
past_tense = infinitive + infinitive[-1] + "ed"
48-
elif re.search(f"{consonant}y$", infinitive):
49-
past_tense = infinitive[:-1] + "ied"
50-
else:
51-
past_tense = infinitive + "ed"
52-
past_tense_file.write(past_tense + "\n")
53-
54-
gerund_exceptions = [
55-
"fixing" # generated as fixxing
56-
]
57-
past_tense_exceptions = [
58-
"fixed" # generated as fixxed
59-
]
60-
61-
for word in gerund_exceptions:
62-
gerund_file.write(word + "\n")
63-
64-
for word in past_tense_exceptions:
65-
past_tense_file.write(word + "\n")
10+
forms = ['imperative', 'third_person', 'gerund', 'past_tense']
11+
for form in forms:
12+
file = open_file_dir_safe(f"{Directories.processed_data}/{form}.txt", "w")
13+
with file:
14+
for word in infinitives:
15+
method = getattr(generator, form)
16+
conjugation = method(word)
17+
file.write(conjugation + "\n")

0 commit comments

Comments
 (0)