Skip to content

Instantly share code, notes, and snippets.

@Rosa-Fox
Created February 18, 2014 12:44
Show Gist options
  • Save Rosa-Fox/9070231 to your computer and use it in GitHub Desktop.
Save Rosa-Fox/9070231 to your computer and use it in GitHub Desktop.
Finds average
#! /usr/bin/env ruby
require 'csv'
tweet = "Chocolate makes me happy, it makes my life wonderful"
tweetsplit = tweet.downcase.split(/\W+/)
valences = []
tweetsplit.each do |i|
#Look through each row in the CSV file to see if any words in the tweet match the words in the 2nd element of the csv array(the word)
CSV.foreach("./ratings.csv") do |row|
#For each line if the word matches words in the tweet then create a new array and put the valence mean value (3rd array element) in it.
#puts [row[1], row[2]].inspect
if row[1]==(i)
valences << row[2]
end
end
puts valences.inspect
#Find the average of the valence array to be ordered later
total = 0.0
valences.each do |val|
total += val.to_f
end
average = total/valences.size
puts average
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment