Skip to content

Instantly share code, notes, and snippets.

@hitode909
Created October 8, 2008 02:41
Show Gist options
  • Save hitode909/15433 to your computer and use it in GitHub Desktop.
Save hitode909/15433 to your computer and use it in GitHub Desktop.
require 'pp'
$KCODE = 'UTF8'
module Ngram
def run(str)
analyze(cut(str,3))
end
def cut(str, n)
list = str.split(//)
hash = { }
hash.default = 0
0.upto(list.size - n) do |counter|
token = list[counter..counter+n-1].join('')
next if token =~ /\n|\r/
hash[token] += 1
end
hash
end
def analyze(hash)
hash.sort_by{ |key, value| value}.reverse
end
end
include Ngram
str = ARGF.read
pp Ngram.run(str) #.select{ |key, value| value > 1}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment