Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Syllable function #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions kefir/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
from .subject import subject, locative, genitive
from .predication import predicate, Copula
from .functional import enum_values
from .syllable import syllable

def sentence(subject, predicate, delimiter=' '):
return delimiter.join((subject, predicate))
Expand Down
70 changes: 70 additions & 0 deletions kefir/syllable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@

def syllable(word):
boundaries = get_syllable_boundaries(word)
result = []
for i in range(0,len(boundaries)-1):
result.append(word[boundaries[i]:boundaries[i+1]])
if len(boundaries) > 0:
result.append(word[boundaries[len(boundaries)-1]:])

return result

def get_syllable_boundaries(word):
size = len(word)
boundary_indexes = []
last_index = size
index = 0
while last_index > 0:
letter_count = letter_count_for_last_syllable(word,last_index)
if letter_count == -1:
return [0]

boundary_indexes.append(last_index - letter_count)
index += 1
last_index -= letter_count
result = []
for i in range(0,index):
result.append(boundary_indexes[index - i - 1])
return result

def is_vowel(character):
if character in "aeiıoöuü":
return True
return False

def letter_count_for_last_syllable(chrs,end_index):
if end_index == 0:
return -1

if is_vowel(chrs[end_index - 1]):
if end_index == 1:
return 1
if is_vowel(chrs[end_index - 2]):
return 1
if end_index == 2:
return 2
if not is_vowel(chrs[end_index - 3]) and end_index == 3:
return 3
return 2
else :
if end_index == 1:
return -1
if is_vowel(chrs[end_index - 2]):
if end_index == 2 or is_vowel(chrs[end_index - 3]):
return 2
if end_index == 3 or is_vowel(chrs[end_index - 4]):
return 3
if end_index == 4:
return -1
if not is_vowel(chrs[end_index - 5]):
return 3
return 3
else:
if not is_vowel(chrs[end_index - 2]):
return -1
if end_index == 2 or not is_vowel(chrs[end_index - 2]):
return -1
if end_index > 3 and not is_vowel(chrs[end_index - 4]):
return 4
return 3