Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

various movings and changes (more lines to document everything)

- moved everything to the twitter-sentiment namespace lib
- moved lingpipe to ext folder because it's an external library
- made textmood.rb more abstract and a bit more efficient (hash map instead of Binary-Search based)
- something else i'm probably forgetting
  • Loading branch information...
commit d5e9684b7a0d7b69af448b44d68ad329277a2279 1 parent e58a261
Jake McGinty authored
View
1  .gitignore
@@ -0,0 +1 @@
+lib/sigmusic/prefs/secrets.rb
View
11 README.md
@@ -1,5 +1,5 @@
-Twitter Sentiment Toolbox
-=========================
+Twitter Sentiment
+=================
We've all seen those interesting visualizations about twitter "moods." Most of them are not the most intelligent systems for detecting the really cool parts about posts. On top of that, nobody has invented a proper toolkit for analyzing, breaking up, and getting *as much* information about shorts snippets (or potentially long) as possible.
Let's do this.
@@ -17,9 +17,14 @@ Preparing your environment
The toolbox requires JRuby, as it interacts with Java libraries, which only the JRuby VM supports. It is highly recommended you use [RVM](http://beginrescueend.com/) to manage the various Ruby VMs. OS X users can use [Jewelry Box](http://unfiniti.com/software/mac/jewelrybox).
With RVM installed:
+
rvm install jruby
rvm use jruby
- sudo gem install twitter
+ gem install rake
+ gem install yardoc
+ gem install twitter
+ gem install twitter-stream
+
Research Leads
--------------
View
0  lib/lingpipe-4.1.0.jar → ext/lingpipe-4.1.0.jar
File renamed without changes
View
9 lib/sigmusic/input/twitter.rb
@@ -1,9 +0,0 @@
-require 'rubygems'
-require 'tweetstream'
-#require 'sigmusic/prefs/defaults'
-#require 'sigmusic/prefs/secrets'
-
-poo = TweetStream::Client.new()
-poo.follow(480959867) do |status|
- puts "[#{status.user.screen_name}] #{status.text}"
-end
View
65 lib/sigmusic/parser/textmood.rb
@@ -1,65 +0,0 @@
-module TextMood
- #Module variables
- filestring = "AFINN-111-emo.txt"
- #end
-
- file = File.new(filestring)
- @fileArr = file.readlines()
-
- #Returns the mood score of the string
- def self.score(s = "")
- sArr = TextMood.textToArray(s)
- score = 0
- n = 0
- while n<sArr.length
- wordscore = TextMood.searchScoreW(sArr[n]) #string
- score += Integer(wordscore)
- n+=1
- end
- score
- end
-
- #Takes a string and turns it into a string array
- #TODO remove punctuation from words
- def self.textToArray (text)
- length = text.length()
- n = 0
- wordStart = 0
- textArr = []
- while (n < length)
- if(text[n] == ' ' || n == length - 1)
- if (n == length - 1) #fixes off by one at end
- n+=1
- end
- textArr += [text[wordStart..n-1]]
- wordStart = n+1
- end
- n+=1
- end
- textArr
- end
-
- #Returns the mood score of a given word (wrapper)
- def self.searchScoreW(word = nil)
- if(word != nil)
- word.downcase!
- TextMood.searchScore(@fileArr,word,0,@fileArr.length-1)
- end #word not nil
- end #searchScore
-
- #Returns the mood score of a given word (recursive binary search)
- def self.searchScore(arr, word, lo, hi)
- return 0 if (hi<lo)
- mid = ((lo+hi)/2)
- arrLine = arr[mid]
- stop = arrLine.index("\t")
- arrWord = arrLine[0..(stop-1)]
- if(arrWord.==(word))
- arrLine[stop+1..arrLine.length()-1]
- elsif(arrWord.<(word))
- TextMood.searchScore(arr, word, mid+1, hi)
- elsif(arrWord.>(word))
- TextMood.searchScore(arr, word, lo, mid-1)
- end
- end
-end
View
3  lib/sigmusic/prefs/secrets.rb
@@ -1,3 +0,0 @@
-module SIGmusic
- module Prefs
-
View
16 lib/twitter-sentiment/input/twitter.rb
@@ -0,0 +1,16 @@
+require 'rubygems'
+require 'tweetstream'
+require 'twitter_sentiment/prefs'
+require 'twitter_sentiment/prefs/secrets'
+module TwitterSentiment
+ module Input
+ class Twitter
+ def initialize
+ stream_client = TweetStream::Client.new(TwitterSentiment::Prefs::Secrets.twitter)
+ stream_client.follow() do |status|
+ puts "[#{status.user.screen_name}] #{status.text}"
+ end
+ end
+ end # Twitter
+ end # Input
+end # TwitterSentiment
View
0  lib/sigmusic/parser/AFINN-111-emo.txt → ...witter-sentiment/parser/AFINN-111-emo.txt
File renamed without changes
View
0  lib/sigmusic/parser/collector.rb → lib/twitter-sentiment/parser/collector.rb
File renamed without changes
View
0  lib/sigmusic/parser/example.rb → lib/twitter-sentiment/parser/example.rb
File renamed without changes
View
0  lib/sigmusic/parser/facerecon.rb → lib/twitter-sentiment/parser/facerecon.rb
File renamed without changes
View
59 lib/twitter-sentiment/parser/text_mood.rb
@@ -0,0 +1,59 @@
+module TwitterSentiment
+ module Parser
+ # Analyzes text from a bag of words with sentiments attached (separated by white space)
+ # See the dict/ folder for examples of bags.
+ class TextMood
+
+ @@bags_dir = 'dict' # What folder the dictionaries are stored in in project dir
+ @@bags = {
+ :afinn => "AFINN-111-emo.txt",
+ :afinn_emo => "AFINN-111.txt",
+ }
+ #end
+
+ # Load a file to be used as our bag of words.
+ #
+ # @param [String, Symbol] String path or symbol to a known dictionary we want to use
+ # => Note: If it's a string, it's the path relative to the root directory.
+ # @raise [ArgumentError] if the file isn't a word, a tab, and a score per line
+ def initialize file
+ case file
+ when Symbol
+ file = @@bags[file] # Convert from symbol to filepath if passed
+ when String
+ # Do nuttin', we already have a filepath
+ else
+ raise ArgumentError, "Expected String or Symbol input for file"
+ end
+ @dict = {}
+ generate_dictionary File.open(File.join(@@bags_dir, file), "r")
+ end
+
+ # Generate Dictionary from file of proper syntax
+ #
+ # @param [File] file the file whose tab-separated word and score
+ # @private
+ def generate_dictionary file
+ file.each_line do |line|
+ line = line.strip.split("\t") # Strip newline, and turn into tab-separated array
+ raise SyntaxError, "lines must be word{tab}weight" unless line.length == 2
+
+ # word -> symbol for hash key, weight -> int value for hash value
+ word, val = line[0].to_sym, line[1].to_i
+ @dict[word] = val
+ end
+ end
+ private :generate_dictionary
+
+ # Returns the mood score of the string
+ #
+ # @param [String] word to search for
+ # @return [Integer,nil] the
+ def score word
+ return @dict[word] if @dict.member? word
+ return nil
+ end
+
+ end # TextMood
+ end # Parser
+end # TwitterSentiment
View
7 lib/twitter-sentiment/prefs/defaults.rb
@@ -0,0 +1,7 @@
+module TwitterSentiment
+ module Prefs
+ @@twitter = {
+ :user_id => 480959867,
+ }
+ end
+end
View
10 lib/twitter-sentiment/prefs/secrets.rb
@@ -0,0 +1,10 @@
+module TwitterSentiment
+ module Prefs
+ @@twitter = {
+ :consumer_key => 'xW42O1MOwxqqFdq7BxqSg',
+ :consumer_secret => 'UcBrgpAYiikKIVJw9Rkog0QK2EzJdVcselCbC8HAVmQ',
+ :oauth_token => '92918151-Bb5J5q0l9o14vxcLLvUwZwPqra4FKxFUpqE08aNUo',
+ :oauth_token_secret => 'cUQtc5NnfwKdSHFJGDX2hog6EijdL1zgQuHioAElY',
+ }
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.