Class: Pinyin

Inherits:
Object
  • Object
show all
Defined in:
lib/pinyin.rb

Instance Method Summary collapse

Constructor Details

#initializePinyin

Initialize using Pinyin.new py = Pinyin.new



7
8
9
10
11
12
13
14
# File 'lib/pinyin.rb', line 7

def initialize
  ma = File.join(File.dirname(File.expand_path(__FILE__)),'../dict/Mandarin.dat')
  @codes = {}
  File.readlines(ma).each do |line|
    nv = line.split(/\s/)
    @codes[nv[0]] = nv[1]
  end
end

Instance Method Details

Generating permlink using ‘-’ split Show the permlink: Interesting-Ruby-Tidbits-That-Dont-Need-Separate-Posts-17 You can also pass a split symbol to_permlink(str, ‘~’)



19
20
21
# File 'lib/pinyin.rb', line 19

def to_permlink(str)
  str_to_pinyin(str,'-')
end

#to_pinyin(str, separator = '', tone = false) ⇒ Object

通用情况 tone为取第几声的标识。eg. ni3hao3zhong1guo2



35
36
37
# File 'lib/pinyin.rb', line 35

def to_pinyin(str,separator='',tone=false)
  str_to_pinyin(str,separator,false,false,tone)
end

#to_pinyin_abbr(str) ⇒ Object

Generating shorter permlink with first letter of each charater Like: zgyh stands for “中国银行”



25
26
27
# File 'lib/pinyin.rb', line 25

def to_pinyin_abbr(str)
  str_to_pinyin(str,'',false,true)
end

#to_pinyin_abbr_else(str) ⇒ Object

第一个字取全部,后面首字母.名称缩写。eg. liudh 刘德华



30
31
32
# File 'lib/pinyin.rb', line 30

def to_pinyin_abbr_else(str)
  str_to_pinyin(str,'',true,nil) 
end