Advertisement
Guest User

Untitled

a guest
Mar 16th, 2024
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. from grapheme import graphemes
  2.  
  3. ## Phonemic
  4. # Revert orthography
  5. def revert_orthography(word):
  6. replacements = [
  7. ("ch", "t͡ʃ"),
  8. ("qu", "k"),
  9. ("v", "w"),
  10. ("c", "k"),
  11. ("j", "x"),
  12. ("ʉ", "ɨ"),
  13. ("ʉ́", "ɨ́"),
  14. ("ʉ̃", "ɨ̃"),
  15. ("ʉ̃́", "ɨ̃́"),
  16. ("y", "j"),
  17. ("r", "ɾ")]
  18.  
  19. new_word = word.lower()
  20. for i, j in replacements:
  21. new_word = new_word.replace(i, j)
  22.  
  23. return new_word
  24.  
  25. # Syllabification
  26. def syllabification(word):
  27. vowels = ["ṹ", "ɨ́", "ɨ̃́", "ã́", "ṍ", "ĩ́", "ɨ̃", "ẽ́", "õ", "i", "ɨ", "ã", "ĩ", "á", "u", "í", "ó", "o", "e", "ẽ", "ú", "ũ", "é", "a"]
  28. syllables = []
  29. new_word = list(graphemes(word))
  30. while new_word != "":
  31. found_vowel = False
  32. for i, letter in enumerate(new_word):
  33. if letter in vowels:
  34. syllables.append("".join(new_word[:i+1]))
  35. new_word = new_word[i+1:]
  36. found_vowel = True
  37. break
  38. else:
  39. continue
  40. if not found_vowel:
  41. break
  42.  
  43. return syllables
  44.  
  45. # Adding primary stress
  46. def give_accent(word):
  47. syllables = syllabification(word)
  48. accented_vowels = ["ã́", "ṍ", "ĩ́", "ẽ́", "ɨ̃́", "ṹ", "á", "é", "í", "ó", "ú", "ɨ́"]
  49. unaccented_vowels = ["ã", "õ", "ĩ", "ẽ", "ɨ̃", "ũ", "a", "e", "i", "o", "u", "ɨ"]
  50. vowel_map = dict(zip(accented_vowels, unaccented_vowels))
  51.  
  52. if len(syllables) == 1:
  53. insert_position = 0
  54. else:
  55. for i, syllable in enumerate(syllables):
  56. if any([vowel in syllable for vowel in accented_vowels]):
  57. insert_position = i
  58. break
  59. else:
  60. insert_position = 1
  61.  
  62. for i, syllable in enumerate(syllables):
  63. for accented_vowel, unaccented_vowel in vowel_map.items():
  64. if accented_vowel in syllable:
  65. syllables[i] = syllable.replace(accented_vowel, unaccented_vowel)
  66. break
  67.  
  68. syllables.insert(insert_position, "ˈ")
  69. return "".join(syllables)
  70.  
  71. # Nasalization
  72. def nasalize_vowels(word):
  73. syllables = syllabification(word)
  74.  
  75. nasal_vowels = ["ã", "õ", "ĩ", "ẽ", "ɨ̃", "ũ"]
  76. vowels = ["a", "o", "i", "e", "ɨ", "u"]
  77. vowel_map = dict(zip(nasal_vowels, vowels))
  78.  
  79. nasals = ["m", "n", "ñ"]
  80. not_nasals = ["b", "d", "j"]
  81. consonant_map = dict(zip(nasals, not_nasals))
  82.  
  83. for i, syllable in enumerate(syllables):
  84. for nasal in nasals:
  85. if nasal in syllable:
  86. for nasal_vowel, vowel in vowel_map.items():
  87. if vowel in syllable:
  88. syllables[i] = syllable.replace(vowel, nasal_vowel)
  89.  
  90. for i, syllable in enumerate(syllables):
  91. for nasal, not_nasal in consonant_map.items():
  92. if nasal in syllable:
  93. syllables[i] = syllable.replace(nasal, not_nasal)
  94. return "".join(syllables)
  95.  
  96. # Creating phonemes
  97. def phonemify(word, pronunciation=None):
  98. word = revert_orthography(word)
  99. word = give_accent(word)
  100. word = nasalize_vowels(word)
  101. return "/" + word + "/"
  102.  
  103. ## Phonetic
  104. # Never? :)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement