Class: PinYin::Backend::Simple
- Inherits:
-
Object
- Object
- PinYin::Backend::Simple
- Defined in:
- lib/ruby-pinyin/backend/simple.rb
Instance Method Summary collapse
-
#initialize(override_files = []) ⇒ Simple
constructor
A new instance of Simple.
- #romanize(str, tone = nil, include_punctuations = false) ⇒ Object
Constructor Details
#initialize(override_files = []) ⇒ Simple
Returns a new instance of Simple.
7 8 9 |
# File 'lib/ruby-pinyin/backend/simple.rb', line 7 def initialize(override_files=[]) @override_files = override_files end |
Instance Method Details
#romanize(str, tone = nil, include_punctuations = false) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/ruby-pinyin/backend/simple.rb', line 11 def romanize(str, tone=nil, include_punctuations=false) res = [] return res unless str && !str.empty? str.unpack('U*').each_with_index do |t,idx| code = sprintf('%x',t).upcase readings = codes[code] if readings res << Value.new(format(readings, tone), false) else val = [t].pack('U*') if val =~ /^[_0-9a-zA-Z\s]*$/ # 复原,去除特殊字符,如全角符号等。 (res.last && res.last.english? ? res.last : res) << Value.new(val, true) # 如果上一个字符也是非中文则与之合并 elsif include_punctuations val = [Punctuation[code]].pack('H*') if Punctuation.include?(code) (res.last ? res.last : res) << Value.new(val, false) end end end res.map {|phrase| phrase.split(/\s+/)}.flatten end |