Skip to content

Instantly share code, notes, and snippets.

@nebuta
Created November 8, 2011 08:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nebuta/1347320 to your computer and use it in GitHub Desktop.
Save nebuta/1347320 to your computer and use it in GitHub Desktop.
Make 1D dictionary
#2dto1d.rb
def normalize(norm)
$vector.each_key{|key|
sqsum = $vector[key].inject(0){|sum,e| sum += e*e}
p sqsum
factor = Math.sqrt(sqsum)
$vector[key].map!{|e| e.to_f * norm / factor}
}
end
def parse(lines)
arr = Array.new(65536)
start = 0
lines.each{|line|
arr[start,256]=line.chomp.split("\t").map{|e| e.to_f}
start += 256
}
return arr
end
def print_vector
$vector.each_key{|key|
puts "Normalized sum:#{key}: " + $vector[key].inject(0){|sum,e| sum += e}.to_s
open("vector_"+key.to_s+'_1d.txt','w'){|out|
out.puts $vector[key].join("\t")
}
}
end
def main
$vector = Hash.new
$vector[:ascii] = parse(IO.readlines("vector_ascii.txt"))
$vector[:utf8] = parse(IO.readlines("vector_utf8.txt"))
$vector[:eucjp] = parse(IO.readlines("vector_eucjp.txt"))
$vector[:iso] = parse(IO.readlines("vector_iso.txt"))
$vector[:shiftjis] = parse(IO.readlines("vector_shiftjis.txt"))
$vector.each_key{|key|
$vector[key] = (0..255).to_a.map{|i| ($vector[key][i*256,256]).inject(0){|sum,a| sum+=a} }
}
normalize(256)
print_vector
end
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment