Skip to content

Instantly share code, notes, and snippets.

@Kimtaro
Forked from mattt/phones.rb
Created February 5, 2009 01:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kimtaro/58494 to your computer and use it in GitHub Desktop.
Save Kimtaro/58494 to your computer and use it in GitHub Desktop.
# 'r' after open-mid central unrounded?
module Keats
module Phones
IPA_CONSONANTS = {
"B" => "b", # b - Lower-case B <be> voiced bilabial plosive
"CH" => "t\312\203", # tʃ - T-Esh ligature <cheese> voiceless postalveolar affricate
"D" => "d", # d - Lower-case D <dee> voiced dental or alveolar plosive
"DH" => "\303\260", # ð - Eth <thee> voiced dental fricative
"F" => "f", # f - Lower-case F <fee> voiceless labiodental fricative
"G" => "g", # g - Lower-case G <green> voiced velar plosive
"HH" => "h", # h - Lower-case H <he> voiceless glottal fricative
"JH" => "d\312\222", # dʒ - D-Yogh ligature <gee> voiced postalveolar affricate
"K" => "k", # k - Lower-case k <key> voiceless velar plosive
"L" => "l", # l - Lower-case L <lee> voice alveolar lateral approx.
"M" => "m", # m - Lower-case M <me> voiced bilabial nasal
"N" => "n", # n - Lower-case N <knee> voiced alveolar nasal
"NG" => "\305\213", # ŋ - Engma <ping> voiced velar nasal
"P" => "p", # p - Lower-case P <pee> voiceless bilabial plosive
"R" => "\311\271", # ɹ - Turned R <read> voiced alveolar approximate
"S" => "s", # s - Lower-case S <sea> voiceless alveolar fricative
"SH" => "\312\203", # ʃ - Esh <she> voiceless postvelar fricative
"T" => "t", # t - Lower-case T <tea> voiceless alveolar plosive
"TH" => "\316\270", # θ - Theta <theta> voiceless dental fricative
"V" => "v", # v - Lower-case V <vee> voiced labiodental fricative
"W" => "w", # w - Lower-case W <we> voiced labio-velar approximant
"Y" => "j", # j - Lower-case J <yield> voiced palatal approximant
"Z" => "z", # z - Lower-case Z <zee> voiced alveolar fricative
"ZH" => "\312\222" # ʒ - Yogh <seizure> voiced postalveolar fricative
}
IPA_VOWELS = {
"AA" => "\311\221", # a - Script A <odd> open back unrounded vowel
"AE" => "\303\246", # æ - Ash <at> near-open front unrounded vowel
"AH" => "\312\214", # ᴧ - Turned V <hut> open-mid back unrounded vowel
"AO" => "\311\224", # ɔ - Open o <ought> open-mid back rounded vowel
"EH" => "\311\233", # ɛ - Epsilon <Ed> open-mid front unrounded vowel
"ER" => "\311\234", # ɜ - Reversed-Epsilon <hurt> open-mid central unrounded vowel
"IH" => "\311\252", # ɪ - Small Capital I <it> near-close, front unrounded vowel
"IY" => "i", # i - Lower-case I <eat> close front unrounded vowel
"UH" => "\312\212", # ʊ - Horseshoe u <hood> near-close near-back vowel
"UW" => "u", # u - Lower-case U <two> close back rounded vowel
"OY" => "\311\224i", # ɔi <toy> (diphthong)
"OW" => "o\312\212", # oʊ <oat> (diphthong)
"EY" => "e\311\252", # eɪ <ate> (diphthong)
"AY" => "a\311\266", # aɪ <hide> (diphthong)
"AW" => "a\312\212", # aʊ <cow> (diphthong)
}
IPA_CHART = IPA_VOWELS.update(IPA_CONSONANTS)
STRESS_CHART = {
"1" => "\313\210", # ˈ - Primary Stress
"2" => "\313\214", # ˌ - Secondary Stress
"0" => "" # - No Stress
}
end
end
class String
include Keats::Phones
def ipa_tokenize
self.split.collect do |token|
phoneme, stress = token.scan(/(\w{1,2})(\d)?/).first
pronounciation = "%s%s" % [STRESS_CHART[stress], IPA_CHART[phoneme]]
pronounciation.gsub(/( # Vowel elongation on stressed syllables
(?: \313\210 | \313\214 ) # Stress marks
(?: [iu] | \311\224 ) # Vowels to elongate
(!? i | $ )? # Dont elongate diphthong segments or at the end of words
)/x, '\1:')
end
end
def to_ipa
"/%s/" % self.ipa_tokenize.join("")
end
end
# Example
puts "AH0 B Z AO1 R B AH0 N S IY0".to_ipa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment