-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkafiye_üreteci.py
89 lines (63 loc) · 2.42 KB
/
kafiye_üreteci.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
# -*- coding: utf-8 -*-
"""kafiye_üreteci.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1jwglO5r6JVfWsN0sAcPBqTUmCIOaMajE
"""
from trnlp import *
import re
turkish_words = set(line.strip() for line in open('all_NEW_turkish_Words.txt'))
def vowel_extractor(a_word):
vowel_instance = tuple("aeıioöuüâî")
all_vowels = [char for char in a_word if char in vowel_instance]
return tuple(all_vowels)
def syllable_harmony(a_word):
vowel_instance = re.compile(r'[aeıioöuüâî]')
consonant_instance = re.compile(r'[bcçdfgğhjklmnprsştvyzxqw]')
word_temporary = syllabification(a_word)
harmony = []
for hece in word_temporary:
substituted_syllable = re.sub(vowel_instance, 'V', hece)
substituted_syllable = re.sub(consonant_instance, 'C', substituted_syllable)
harmony.append(substituted_syllable)
return tuple(harmony)
all_turkish_words_dict = {item: [
tuple(syllabification(item)),
vowel_extractor(item),
syllable_harmony(item),
len(syllabification(item))
] for item in turkish_words}
def rhyme_generator(input_word):
input_word = input_word.replace(" ", "").lower()
input_syllabification = tuple(syllabification(input_word))
input_vowels = vowel_extractor(input_word)
input_harmony = syllable_harmony(input_word)
input_length = len(input_syllabification)
usable_words = [word for word in turkish_words if len(syllabification(word)) == input_length]
only_words = [item for item in usable_words if levenshtein_distance(input_word, item) <= 5]
filtered_step_1 = []
for item in only_words:
tuple1_2 = input_vowels
if item in all_turkish_words_dict:
tuple2_2 = all_turkish_words_dict[item][1]
score = sum(1 for i in range(min(len(tuple1_2), len(tuple2_2))) if tuple1_2[i] == tuple2_2[i])
if score == len(input_vowels):
filtered_step_1.append(item)
else:
pass
else:
pass
filtered_step_2 = []
for item in filtered_step_1:
tuple1_1 = input_harmony
if item in all_turkish_words_dict:
tuple2_1 = all_turkish_words_dict[item][2]
score = sum(1 for i in range(min(len(tuple1_1), len(tuple2_1))) if tuple1_1[i] == tuple2_1[i])
if score <= 1:
pass
else:
filtered_step_2.append(item)
else:
pass
return filtered_step_2
print(rhyme_generator("saksafon"))