-
Notifications
You must be signed in to change notification settings - Fork 0
/
rimador.py
executable file
·145 lines (112 loc) · 5.42 KB
/
rimador.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
#!/usr/bin/env python3
import re
import phonetics
import silabeador
class Rimador():
def __init__(self, ph, verbose=0):
self.rima_consonante = False
self.rima_asonante = False
self.decima_structure = {0:'A',1:'B', 2:'B', 3:'A', 4:'A', 5:'C', 6:'C', 7:'D', 8:'D', 9:'C'}
self.decima_rhyme = {'A': False, 'B': False, 'C': False, 'D': False}
self.abba_structure = {0: 'A', 1: 'B', 2: 'B', 3: 'A'}
self.abba_rhyme = {'A': False, 'B': False}
self.silabeador = silabeador.Silabeador(verbose, **ph)
self.ph = ph
def is_rima_consonante(self, w1, w2):
# return True if after the last accented vowel, the repeated sounds are consonants and vowels
# otherwise False
w1_rhyme = self.get_rhyme_word(w1)
w2_rhyme = self.get_rhyme_word(w2)
if w1_rhyme == w2_rhyme:
return True
else:
return False
def get_rhyme_word(self, word):
self.silabeador.count_syllables_word(word)
# this lines should be part of Acentuador class
monosilabo = self.silabeador.is_monosilabo(self.silabeador.last_word['word_syllables'])
accent = self.silabeador.acentuacion(self.silabeador.last_word['phonemes'],\
self.silabeador.last_word['structure'],\
monosilabo)
self.silabeador.last_word['monosilabo'] = monosilabo
self.silabeador.last_word['accent'] = accent
word_rhyme = self.get_segment_accented(self.silabeador.last_word)
return word_rhyme
def get_segment_accented(self, last_word):
if last_word['accent'] == 'none': # not very clean. to fix also in silabeador class
return last_word['phonemes']
elif last_word['accent'] == 'aguda':
return self.rhyme_only(last_word['word_syllables'].split('-')[-1])
elif last_word['accent'] == 'grave':
return self.rhyme_only(last_word['word_syllables'].split('-')[-2:])
elif last_word['accent'] == 'esdrujula':
return self.rhyme_only(last_word['word_syllables'].split('-')[-3:])
else:
raise AttributeError()
def rhyme_only(self, segment_accented):
# segment from accented vowel, without previous consonants
pattern = re.compile(f'.*?({self.ph["vowels"]}.*)')
try:
rhyme_only = re.match(pattern, segment_accented)
except TypeError:
rhyme_only = re.match(pattern, '-'.join(segment_accented))
return rhyme_only.group(1)
def is_rima_asonante(self, w1, w2):
# return True if after the last accented vowel, the repeated sounds are vowels
# otherwise False
w1_rhyme = self.get_rhyme_word(w1)
w2_rhyme = self.get_rhyme_word(w2)
w1_rhyme = ''.join([char for char in w1_rhyme if char not in self.ph['consonants']])
w2_rhyme = ''.join([char for char in w2_rhyme if char not in self.ph['consonants']])
if w1_rhyme == w2_rhyme:
return True
else:
return False
def is_any_rhyme(self, w1, w2):
if self.is_rima_asonante(w1, w2):
return True
elif self.is_rima_consonante(w1, w2):
return True
else:
return False
def is_ABBA(self, verse_number, word):
if not self.abba_rhyme[self.abba_structure[verse_number]]:
print(f'not previous rhyme in {self.abba_rhyme[self.abba_structure[verse_number]]}')
self.abba_rhyme[self.abba_structure[verse_number]] = (word, self.get_rhyme_word(word))
return True
else:
if self.is_any_rhyme(self.abba_rhyme[self.abba_structure[verse_number]][0], word):
print(f'there is rhyme between: {self.abba_rhyme[self.abba_structure[verse_number]][0]} {word}')
return True
else:
print(f'there is no rhyme between: {self.abba_rhyme[self.abba_structure[verse_number]][0]} {word}')
return False
def is_abba_rhyme(self, rhyme_char, compared_word):
if not self.abba_rhyme[self.abba_structure[rhyme_char]]:
self.abba_rhyme[self.abba_structure[rhyme_char]] = compared_word
return True
else:
if self.is_any_rhyme(self.abba_rhyme[self.abba_structure[rhyme_char]], compared_word):
print(self.abba_rhyme[self.abba_structure[rhyme_char]], compared_word)
return True
else:
return False
def is_decima_rhyme(self, rhyme_char, compared_word):
if not self.decima_rhyme[self.decima_structure[rhyme_char]]:
self.decima_rhyme[self.decima_structure[rhyme_char]] = compared_word
return True
else:
if self.is_any_rhyme(self.decima_rhyme[self.decima_structure[rhyme_char]], compared_word):
print(self.decima_rhyme[self.decima_structure[rhyme_char]], compared_word)
return True
else:
return False
if __name__ == '__main__':
ph = phonetics.phonetics
rimador = Rimador(ph, verbose=0)
print(rimador.is_rima_asonante('camión', 'martillo'))
print(rimador.is_rima_asonante('camión', 'canción'))
print(rimador.is_rima_asonante('camión', 'cansión'))
print(rimador.is_rima_asonante('cuchillo', 'martillo'))
print(rimador.is_rima_asonante('abro', 'cuajo'))
print(rimador.is_rima_asonante('abro', 'zapato'))