现在主要在知乎,地址:https://www.zhihu.com/people/qqrrm 老的文章在:http://blog.csdn.net/pyp

东风何处是人间(ruby版)

上一篇 / 下一篇  2011-12-15 17:48:43 / 个人分类:ruby

在今年3月份的,就看到这个帖子《东风何处是人间》了,对宋词进行分词计数,当时就保存了这个帖子,想以后有时间写个ruby版的。后来就忘记了
近期这个帖子大火啊,也终于抽出时间写ruby版的了。
个人水平有限,程序写的很糟糕,至少比原文的看着复杂多了,不知道是否能有ruby高手给大家写个示例。

数据:《全宋词》文本


#coding: utf-8
require "iconv"

s1 = Iconv.conv 'gbk','utf-8',","
s2 = Iconv.conv 'gbk','utf-8',"。"
s3 = Iconv.conv 'gbk','utf-8',"!"
s4 = Iconv.conv 'gbk','utf-8',"?"
s5 = Iconv.conv 'gbk','utf-8',"、"

NUM1 = 2  #分词长度
NUM2 =500  #显示大于多少的记录

def splitword(s,l)    #分词,x是字符串,l是字符分词长度
  lt  = s.length
  k = Array.new
  0.upto(lt-l) do |i|
    k<<s[i..i+l-1]
  end
  return k
end

x = Array.new     #记录分词结果的数组

File.open("ci.txt","r") do |file|
  file.each do |line|
    if line.length<500 and line.length>10
      line.gsub!(s2,s1)     #把标点都替换为",",再统一进行分割
      line.gsub!(s3,s1)
      line.gsub!(s4,s1)
      line.gsub!(s5,s1)
      line.chomp!
      column = line.split(s1)       #用逗号分割
      column.delete_if {|i| i.length >10 } #去除大于10个字的语句
      column.each do |col|
        splitword(col,NUM1).each{|i| x<<i}  if col.length>=NUM1 # 分词
      end 
    end
  end
end

h = Hash.new
h = x.inject(Hash.new(0)){|hash,x| hash[x] += 1; hash} #把数组内容进行计数为hash
h.delete_if {|key, value| value <NUM2}               #去除hash中小于指定数值的部分

y = Array.new
y  = h.sort {|a,b| b[1]<=>a[1]}                      # 从大到小排序
y.each_index {|i| puts "#{i+1} #{y[i][0]} = #{y[i][1]}" }

TAG:

 

评分:0

我来说两句

日历

« 2024-04-18  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 70114
  • 日志数: 47
  • 图片数: 2
  • 文件数: 2
  • 建立时间: 2006-11-24
  • 更新时间: 2023-01-29

RSS订阅

Open Toolbar