From c0fe5e68f558c7c3205063cea28818e3a68eed55 Mon Sep 17 00:00:00 2001 From: tahtaciburak Date: Sat, 5 Jan 2019 02:55:41 +0300 Subject: [PATCH] Add Syllable function --- kefir/__init__.py | 1 + kefir/syllable.py | 70 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 kefir/syllable.py diff --git a/kefir/__init__.py b/kefir/__init__.py index bbca636..006f041 100644 --- a/kefir/__init__.py +++ b/kefir/__init__.py @@ -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)) diff --git a/kefir/syllable.py b/kefir/syllable.py new file mode 100644 index 0000000..e5cfb20 --- /dev/null +++ b/kefir/syllable.py @@ -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 +