一个简单的姓名拼音匹配
by
at 2011-01-19 22:32:15
original http://www.javaeye.com/topic/889109
# 一个简单的姓名拼音匹配 # # 姓名一般是由两三个汉字组成,选其顺序且连续的拼音缩略的组合就算匹配成功。 # require 'chinese_pinyin' @name = "成吉思汗" pinyins = Pinyin.t(@name).split # 把姓名生成对应的拼音数组 array = [] pinyins.each_with_index do |han, index| array[index] = [] han.size.times {|num| array[index] << han[0..(num - 1)] } end as = [] array.size.times do |x| sub_array = [] # 第一趟生成姓名拼音缩略的所有满足顺序且连续的组合 array.each_with_index do |ele, i| ni = i + x sub_array << array[i..ni] if array[ni] end # 第二趟把每个组合各自归并 sub_array.each do |arr| while arr.size > 1 sub_as = [] arr[0].each {|a1| arr[1].each {|a2| sub_as << ( a1 + a2 ) } } arr[0..1] = [sub_as] end as << arr puts arr.join(' ') end end as.flatten.join(',') # Ruby标准库里的abbr.rb是排除两个英文单词的共同缩略,见http://ruby-doc.org/stdlib/libdoc/abbrev/rdoc/classes/Abbrev.html
<br><br>
作者: <a href="http://mvj3.javaeye.com">mvj3</a>
<br>
声明: 本文系JavaEye网站发布的原创文章,未经作者书面许可,严禁任何网站转载本文,否则必将追究法律责任!
<br><br>
<span style="color:red">
<a href="http://www.javaeye.com/topic/889109" style="color:red">已有 <strong>0</strong> 人发表回复,猛击->><strong>这里</strong><<-参与讨论</a>
</span>
<br><br><br>
JavaEye推荐