Created
February 19, 2011 05:49
-
-
Save masonicboom/834861 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| # Count co-occurences of all characters in words given 1-per-line from STDIN. | |
| # count has the form { char_i => { char_j => # of co-occurences }, ... }. | |
| # The block initialization here just sets up a default of 0 co-occurences. | |
| count = Hash.new do | |
| |hash, key| hash[key] = Hash.new { |h, k| h[k] = 0 } | |
| end | |
| STDIN.each do |word| | |
| w = word.chomp | |
| w.chars.to_a.combination(2).each do |char1, char2| | |
| count[char1][char2] += 1 | |
| count[char2][char1] += 1 | |
| end | |
| end | |
| # Format count into a sparse matrix CSV for consumption by other programs. | |
| index = Hash.new { |hash, key| hash[key] = hash.length+1 } | |
| done = {} | |
| count.each do |char1, other| | |
| other.each do |char2, co_occurences| | |
| ic1 = index[char1] | |
| ic2 = index[char2] | |
| # count includes duplicate data. Here, we strip them. | |
| next if done[ [ic2, ic1] ] | |
| done[ [ic1, ic2] ] = true | |
| #puts "#{ic1},#{ic2},#{co_occurences}\n" # matlab likes this form, with numeric indices rather than unicode characters | |
| puts "#{char1},#{char2},#{co_occurences}\n" | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment