一个简单的姓名拼音匹配

姓名一般是由两三个汉字组成,选其顺序且连续的拼音缩略的组合就算匹配成功。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# encoding: UTF-8

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