Skip to content

Instantly share code, notes, and snippets.

@jacobandresen
Created December 31, 2010 10:02
Show Gist options
  • Save jacobandresen/760902 to your computer and use it in GitHub Desktop.
Save jacobandresen/760902 to your computer and use it in GitHub Desktop.
learning ruby
def readData()
data=[]
f = File.open("twl06.txt", "r")
f.each_line do |line|
data << line
end
return data
end
def levenshtein(s,t)
n = s.size
m = t.size
a = Array.new
if n!=0 && m!= 0
r = Array.new
rz = Array.new
0.upto(m) { |x| r.push(0)}
0.upto(n) { |x| a.push(r.dup)}
a.each_index {|x| a[x][0] = x}
0.upto(m) {|x| a[0][x] = x}
cost = 0
1.upto(n) do |i|
1.upto(m) do |j|
cost = s[i] == t[j] ? 0 : 1
a[i][j] = [a[ i- 1][j] +1,a[i][j - 1] + 1,a[i - 1][j -1] + cost].min
end
end
a[n][m]
else
0
end
end
def scoreWord(word,data)
word = word.upcase
count =0
minscore = 1000
data = data.sort_by{ |a| a.length }
data.each {
|d|
count = count +1
if (minscore==0 || minscore < ((d.length - word.length).abs)) then
break
elsif
score = levenshtein(d,word)
if (score < minscore) then
minscore = score
puts "MIN:"+d
end
if (count%3000==0) then
puts word+" "+d+" "+(String(score))+" "+(String(minscore))+" "+(String(count))+"/"+(String
(data.length))
end
end
}
return minscore
end
data=readData()
def scoreSentence( sentence,data)
score = 0
sentence.split(/\s+/).each { |w|
score += scoreWord(w,data)
}
puts score
end
scoreSentence("tihs sentenctcnes iss nout varrry goud",data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment