#!/usr/bin/ruby -Ku |
lala
inser into sqlite3 db
def save_to_db
|
反正就是一整個好玩!!!(  ̄ c ̄)y▂ξ
#!/usr/bin/ruby -Ku |
lala
inser into sqlite3 db
def save_to_db
|
#!/usr/bin/ruby -Ku stop_words_db = "stop_words.csv" #stop words list |
class String def mbsubstr1(*range) split(//)[*range].join("") end
def mbsubstr2(idx, len) slice(/\A.{#{idx}}(.{0,#{len}}/m, 1) end end p "一大串字一大串字".mbsubstr1(2,3) p "一大串字一大串字".mbsubstr1(2..3) p "一大串字一大串字".mbsubstr2(2,2) |
Maximum Matching Segmentation (MMSeg)
#str 一段文字, term 字典的中的詞 def mmseg(str,term)
|
使用lambda 計算 Euclidean distance
若p(1,1), q(3,3)
則 d(p,q)
->(xs,ys,xt,yt){Math.sqrt((xs-xt)**2+(ys-yt)**2)}.(1,1,3,3) |
lambda fuction
f = { 1 => ->(x) { puts "#{x} from 1" }, 2 => ->(x) { puts "#{x} bye from 2" } } f.call[1]("mary") |
在 HyperSphere 中,求任兩點的的距離(EuclideanDistance)
def EuclideanDistance(x,y) sum = 0 for i in (0..x.size-1) printf("x[%d]=%d,y[%d]=%d\n",i,x[i],i,y[i]) ->(px,py){ sum += (px-py)**2}.(x[i],y[i]) end puts Math.sqrt(sum) end x=[1,1] y=[3,3] p=[1,1,1] q=[3,3,3] a=[1,1,1,1] b=[3,3,3,3] EuclideanDistance(x,y) EuclideanDistance(p,q) EuclideanDistance(a,b) |
n類中,取任兩類別算相似度, 第一步,先把要算的組合列出來
n=4 |
require 'ai4r' #abuild(dataset,clusters) res=kmeans.build(ai4r_data,2) puts res.centroids.join(',') |
http://en.wikipedia.org/wiki/Euclidean_space
http://en.wikipedia.org/wiki/Euclidean_distance
dis = Ai4r::Clusterers::KMeans.new a = [1,1]
|
都是幻覺,嚇不倒我的 (  ̄ c ̄)y▂ξ
#!/usr/bin/ruby -Ku require 'rubygems' require 'csv' f=ARGV[0] File.open("#{f}") { |content| while line=content.gets puts CSV.parse(line).map{ |x| [ x[0], *x[1..-1].each.with_index.map{ |y, i| "#{i+1}:#{y}"} ].join(' ') }.join("\n") end } |