Skip to content

Instantly share code, notes, and snippets.

@Najaf
Last active August 29, 2015 13:56
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 Najaf/8983235 to your computer and use it in GitHub Desktop.
Save Najaf/8983235 to your computer and use it in GitHub Desktop.
#! /usr/bin/env ruby
require "twitter"
require 'csv'
require 'regexp'
# Twitter setup was here...
#Gets the last 10 tweets from a given user from the twitter API
screen_name = String.new ARGV[0]
#for each tweet
tweet = client.user_timeline(screen_name, count: 10).each do |tweet|
#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 = CSV.foreach("csvratings.csv", {:headers=>true}).each 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.
if row[1].match(tweet)do
valence = array.new
valance.push(row[2])
#Find the average of the valence array to be ordered later
len = valence.length
total = valence.inject(:+)
average = total.to_f / len
#print out the tweet and their average valence number
puts tweet + average
#Alternative ending idea...
#average valence number needs to be ordered, not sure how to get these to correspond with the tweets...
all_averages = array.new
all_averages.push(average + tweet)
all_averages.sort
puts tweet + all_averages
#Notes:
#For the tweets each count search through the CSV loop,
#if any words match then assign them with the coresponding number in vmean[2] of the CSV array
#Find the valence avergage for the words in each line
#Assign them a value from 1 - 10 and print in that order
@Najaf
Copy link
Author

Najaf commented Feb 13, 2014

OK, good start, a couple of things that might help you crack this one:

Creating and adding to arrays

This is a perfectly valid way of creating an array:

my_array = Array.new

But unless you've got a good reason for doing so (you want to set an initial size and default value), we usually just do this:

my_array = []

Adding things to an array with my_array.push(thing_to_add) is perfectly idiomatic Ruby, my personal preferences is to use the double arrows instead:

my_array << 'cheese'
my_array << 'on'
my_array << 'toast'

# at this stage, my_array is ['cheese', 'on', 'toast']

my_array << 'is' << 'probably' << 'not' << 'good' << 'you'

# by now, my_array is ['cheese', 'on', 'toast', 'is', 'probably', 'not', 'good', 'for', 'you']

Indentation

You're almost definitely going to get confused about which do..end block you're in if you're indenting as you go!

Other notes

  • I don't thing you need the require 'regexp' directive, could be wrong.
  • if statements don't require an enclosing do for the code that they branch on, but they do require a finishing end.
  • Don't worry about the order of the tweets, they just need the numeric rating next to them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment