flyerhzm/chinese_pinyin

Tone Marks

Opened this issue · 5 comments

chid commented

Is it possible to also add in the tone marks?

eg.

Pinyin.t('**', tonemarks: true) => "zhōng guó"

@chid We already supported tone, see README, but it output like "zhong1 guo2" rather than "zhōng guó",

chid commented

I realise that, I was wondering whether it is possible to add the symbols rather than the number.

I think we can approach this by adding an array like

tones = [
    ['a', 'ā', 'á', 'ǎ', 'à'],
    ['e', ...],
    ...
]

and if we want to get a special tone for a letter, we use:

r = 'zhong'   # 中
tone = 1
idx = 0
# If we have 'a', we put the sign above it
# Otherwise, we check 'o', 'e', 'i', 'u', 'yu'.
# If we have both 'i' and 'u', we put the sign above the one coming after the other
if r.index('a') then idx = r.index('a')
elsif r.index('o') then idx = r.index('o')
elsif r.index('e') then idx = r.index('e')
elsif r.index('i') and r.index('u') then
    if r.index('i') > r.index('u') then idx = r.index('i')
    else idx = r.index('u') end
elsif r.index('i') then idx = r.index('i')
elsif r.index('u') then idx = r.index('u')
elsif r.index('ǖ') then idx = r.index('ǖ')
end
for i in 0..tones.count-1
    if tones[i][0] == r[idx] then
        r[idx] = tones[i][tone]
        break
    end
end

Maybe this could work.

👍

I was writing some Lua code at that time and the code above looks like Lua... Thanks for improving it and making it Ruby-style (like %w(a o e i u v).each) 😜