Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from grapheme import graphemes
- ## Phonemic
- # Revert orthography
- def revert_orthography(word):
- replacements = [
- ("ch", "t͡ʃ"),
- ("qu", "k"),
- ("v", "w"),
- ("c", "k"),
- ("j", "x"),
- ("ʉ", "ɨ"),
- ("ʉ́", "ɨ́"),
- ("ʉ̃", "ɨ̃"),
- ("ʉ̃́", "ɨ̃́"),
- ("y", "j"),
- ("r", "ɾ")]
- new_word = word.lower()
- for i, j in replacements:
- new_word = new_word.replace(i, j)
- return new_word
- # Syllabification
- def syllabification(word):
- vowels = ["ṹ", "ɨ́", "ɨ̃́", "ã́", "ṍ", "ĩ́", "ɨ̃", "ẽ́", "õ", "i", "ɨ", "ã", "ĩ", "á", "u", "í", "ó", "o", "e", "ẽ", "ú", "ũ", "é", "a"]
- syllables = []
- new_word = list(graphemes(word))
- while new_word != "":
- found_vowel = False
- for i, letter in enumerate(new_word):
- if letter in vowels:
- syllables.append("".join(new_word[:i+1]))
- new_word = new_word[i+1:]
- found_vowel = True
- break
- else:
- continue
- if not found_vowel:
- break
- return syllables
- # Adding primary stress
- def give_accent(word):
- syllables = syllabification(word)
- accented_vowels = ["ã́", "ṍ", "ĩ́", "ẽ́", "ɨ̃́", "ṹ", "á", "é", "í", "ó", "ú", "ɨ́"]
- unaccented_vowels = ["ã", "õ", "ĩ", "ẽ", "ɨ̃", "ũ", "a", "e", "i", "o", "u", "ɨ"]
- vowel_map = dict(zip(accented_vowels, unaccented_vowels))
- if len(syllables) == 1:
- insert_position = 0
- else:
- for i, syllable in enumerate(syllables):
- if any([vowel in syllable for vowel in accented_vowels]):
- insert_position = i
- break
- else:
- insert_position = 1
- for i, syllable in enumerate(syllables):
- for accented_vowel, unaccented_vowel in vowel_map.items():
- if accented_vowel in syllable:
- syllables[i] = syllable.replace(accented_vowel, unaccented_vowel)
- break
- syllables.insert(insert_position, "ˈ")
- return "".join(syllables)
- # Nasalization
- def nasalize_vowels(word):
- syllables = syllabification(word)
- nasal_vowels = ["ã", "õ", "ĩ", "ẽ", "ɨ̃", "ũ"]
- vowels = ["a", "o", "i", "e", "ɨ", "u"]
- vowel_map = dict(zip(nasal_vowels, vowels))
- nasals = ["m", "n", "ñ"]
- not_nasals = ["b", "d", "j"]
- consonant_map = dict(zip(nasals, not_nasals))
- for i, syllable in enumerate(syllables):
- for nasal in nasals:
- if nasal in syllable:
- for nasal_vowel, vowel in vowel_map.items():
- if vowel in syllable:
- syllables[i] = syllable.replace(vowel, nasal_vowel)
- for i, syllable in enumerate(syllables):
- for nasal, not_nasal in consonant_map.items():
- if nasal in syllable:
- syllables[i] = syllable.replace(nasal, not_nasal)
- return "".join(syllables)
- # Creating phonemes
- def phonemify(word, pronunciation=None):
- word = revert_orthography(word)
- word = give_accent(word)
- word = nasalize_vowels(word)
- return "/" + word + "/"
- ## Phonetic
- # Never? :)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement