-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactory with extending multitone_pinyin
- Loading branch information
Showing
5 changed files
with
103 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
class MultiPinyin < Pinyin | ||
M_SPLIT_CHAR = "|" | ||
|
||
def self.full(value, split_char = nil) | ||
res = etymon_mapping_arr(value, split_char) | ||
cross_product_arr(res).map{|w| full_word(w, split_char) }.join(M_SPLIT_CHAR) | ||
end | ||
|
||
def self.abbr(value, split_char = nil) | ||
res = etymon_mapping_arr(value, split_char) | ||
cross_product_arr(res).map{|w| abbr_word(w, split_char) }.join(M_SPLIT_CHAR) | ||
end | ||
|
||
def self.abbr_else(value, split_char = nil) | ||
res = etymon_mapping_arr(value, split_char) | ||
cross_product_arr(res).map { |w| abbr_else_word(w, split_char) }.join(M_SPLIT_CHAR) | ||
end | ||
|
||
def self.find_etymon(word) | ||
@@dist.select{ |k, v| v.match(word) }.map{ |k, v| k } | ||
end | ||
|
||
def self.cross_product_arr(arr) | ||
return arr if arr.length <= 1 | ||
arg_str = (1..(arr.length - 1)).map{|num| "arr[#{num}]" }.join(', ') | ||
|
||
eval("arr[0].product(#{arg_str})") | ||
end | ||
|
||
def self.etymon_mapping_arr(value, split_char) | ||
return [] if value.nil? | ||
|
||
result = [] | ||
value.clone.split(//).each do |w| | ||
etymon = find_etymon(w) if zh_cn?(w) | ||
result << (etymon || [w]) | ||
end | ||
|
||
result | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require 'test_helper' | ||
|
||
class MultiPinyinTest < Test::Unit::TestCase | ||
def test_full | ||
assert_equal "yedongkai|xiedongkai", MultiPinyin.full("叶冬开") | ||
assert_equal "yedongkaiabcyedong|yedongkaiabcxiedong|xiedongkaiabcyedong|xiedongkaiabcxiedong", MultiPinyin.full("叶冬开abc叶冬", nil) | ||
end | ||
|
||
def test_abbr | ||
assert_equal "gjp|hjp", MultiPinyin.abbr("红靖鹏") | ||
end | ||
|
||
def test_abbr_else | ||
assert_equal "gongyjp|gongxjp|hongyjp|hongxjp", MultiPinyin.abbr_else("红叶靖鹏") | ||
end | ||
|
||
def test_find_etymon | ||
assert_equal ["ye", "xie"], MultiPinyin.find_etymon("叶") | ||
end | ||
|
||
def test_cross_product_arr | ||
assert_equal [["a", 1], ["a", 2], ["a", 3], ["b", 1], ["b", 2], ["b", 3]], MultiPinyin.cross_product_arr([['a', 'b'], [1, 2, 3]]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
require 'rubygems' | ||
require 'test/unit' | ||
require 'pinyin' | ||
require 'multi_pinyin' |