Skip to content

Instantly share code, notes, and snippets.

@lekevicius
Created March 14, 2013 14:08
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 lekevicius/5161604 to your computer and use it in GitHub Desktop.
Save lekevicius/5161604 to your computer and use it in GitHub Desktop.
Converts the text feed to CSV.
require 'json'
require 'csv'
data_txt = File.read('data_itf.txt')
words = ['sir', 'thank', 'love', 'god', 'life', 'night', 'shit', 'boy', 'girl', 'fuck', 'car', 'money', 'father', 'mother', 'hell', 'son', 'kill', 'dead', 'call', 'friend', 'stay', 'leave', 'baby', 'home', 'world']
results = {}
words.each do |word|
years = {}
(1962..2012).each do |year|
years[year] = 0
end
results[word] = years
end
data_txt.each_line do |data_point|
data_points = data_point.split ' - '
year = data_points[0].to_i
word = data_points[1]
value = data_points[2].to_f
results[word][year] = value * 1000
end
# puts JSON.generate(results)
# puts results['love'][1982]
CSV.open("data_itf.csv", "wb") do |csv|
new_arr = words.clone
new_arr.unshift 'YEAR'
csv << new_arr
(1962..2012).each do |year|
word_data_arr = [year.to_s]
words.each do |word|
word_data_arr << results[word][year].to_s
end
csv << word_data_arr
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment