Permalink
Browse files

Added punctuation reading functionality

  • Loading branch information...
1 parent 1bbf849 commit d6954b856fc16c4ed4e0c9aac938d1935cfc4669 @scowalt scowalt committed Mar 9, 2012
Showing with 54 additions and 20 deletions.
  1. +20 −18 app.rb
  2. +4 −2 lib/twitter-sentiment/parser/face_recon.rb
  3. +30 −0 lib/twitter-sentiment/parser/randomness.rb
View
38 app.rb
@@ -11,9 +11,9 @@ class TwitterBeats
def initialize
pp :info, "TwitterBeats initializing..."
textmood = TwitterSentiment::Parser::TextMood.new :afinn
- facerecon = TwitterSentiment::Parser::FaceRecon.new
userinfo = TwitterSentiment::Parser::UserInfo.new
output_send = TwitterSentiment::Output::Send.new
+ random = TwitterSentiment::Parser::Randomness.new
TwitterSentiment::Input::Twitter.new({
:status_callback => lambda { |status|
weight, mood = {}, {}
@@ -24,56 +24,58 @@ def initialize
mood[:text] = :happy if weight[:text] > 0
mood[:text] = :sad if weight[:text] < 0
pp mood[:text], "text score: #{weight[:text].to_s.ljust(7)}tweet: #{status.text}", :med
- # image weight
- imgurl = status.user.profile_image_url.gsub(/_normal/, '')
- begin
- weight[:img] = facerecon.profile_image_happiness(imgurl)
- pp :info, "face image weight received", :high
- mood[:img] = :bhargav
- mood[:img] = :happy if weight[:img] > 0
- mood[:img] = :sad if weight[:img] < 0
- pp mood[:img], "img score: #{weight[:img].to_s.ljust(8)}url: #{imgurl}", :med
- rescue
- pp :warn, "failed to download profile image, so cannot calculate face weight"
- mood[:img] = 0.0
- end
+
#user stalking
info = userinfo.gather(status.user)
pp :info, "Boring images: #{info[0]}"
pp :info, "Followers per tweet: #{info[1]}"
weight[:description] = info[3]
+ weight[:img] = info[2]
mood[:description] = :bhargav
mood[:description] = :happy if weight[:description] > 0
mood[:description] = :sad if weight[:description] < 0
+ mood[:img] = :bhargav
+ mood[:img] = :happy if weight[:img] > 0
+ mood[:img] = :sad if weight[:img] < 0
pp mood[:description], "Desc. score: #{weight[:description].to_s.ljust(8)}User description: #{status.user.description}"
+
+ #symbol checking
+ syms = random.symbol_count(status.text)
+ pp :info, "syms: #{syms}"
+
#compile data (not JSON)
#put this is a separate method?
- total_happiness = Integer(weight[:text]) + Integer(weight[:img])/10 + Integer(weight[:description])
+ total_happiness = Integer(weight[:text]) + Integer(weight[:img])/10 + Integer(weight[:description]) #textmood + imgscore/10 + userdescription
capped_total_happiness = total_happiness
capped_total_happiness = 10 if total_happiness > 10
capped_total_happiness = -10 if total_happiness < -10
- total_excitement = Integer(info[1]*10) - Integer(info[0]*5)
+ total_excitement = Integer(info[1]*10) - Integer(info[0]*5) #followerspertweet * 10 - boringimages * 5
capped_total_excitement = total_excitement
capped_total_excitement = 10 if total_excitement > 10
capped_total_excitement = -10 if total_excitement < -10
+
+ total_randomness = -10 + Integer(syms[1]) + rand(5) #-10 + '?' + rand(5)
+ capped_total_randomness = total_randomness
+ capped_total_randomness = 10 if total_excitement > 10
+ capped_total_randomness = -10 if total_excitement < -10
data = {
"input" => {
"source" => "twitter",
"username" => status.user.screen_name,
"displayname" => status.user.name,
"userid" => status.user.id_str,
"url" => "https://twitter.com/#!/" + status.user.screen_name + "/status/" + status.id_str + "/",
- "userimgurl" => imgurl,
+ "userimgurl" => status.user.profile_image_url.gsub(/_normal/, ''),
"raw_input" => status.text,
"text" => nil, #fix this
"metadata" => nil #fix this
}, #input
"weights" => {
"happiness" => capped_total_happiness, #filler algorithm
"excitement" => capped_total_excitement, #filler algorithm
- "randomness" => rand(20) - 10 #literally random
+ "randomness" => capped_total_randomness #literally random
}, #weights
"sentiment" => {
"text" => {
@@ -36,13 +36,15 @@ def profile_image_happiness img = nil
if img != nil
#formatting url
img = img + "?x=.png"
- pp :info, "Face detection on: #{img}"
-
+ #pp :info, "Searching for face in #{img}"
+
#executing search
arr = @client.detect_faces(img) #call whatever calls the FaceAPI
arr = smile_info(arr) #format the search results
return 0 if arr.length == 0
# expecting arr = [[boolean,int],[boolean,int].....]
+
+ #pp :info, "faces array: #{arr}"
#tabulating results
score = 0
@@ -0,0 +1,30 @@
+require 'purdy-print'
+include PurdyPrint
+
+module TwitterSentiment
+ module Parser
+ include PurdyPrint
+ class Randomness
+ def initialize
+ #no code
+ end
+
+ #@param [String] Sentence (tweet)
+ #@return [Array] ["!", "?", "/"]
+ def symbol_count text
+ # all lowercase, baby
+ sentence = text.downcase
+ chars = sentence.split(//) # break up into char array
+ chars.reject! { |c| (c.>=('a') && c.<=('z')) || (c.>=('0') && c.<=('9')) || c.==(' ')} # get rid of any blank entries
+ pp :info, "Punctuation: #{chars}"
+ arr = [0,0,0]
+ chars.each do |c|
+ arr[0] += 1 if c.==('!')
+ arr[1] += 1 if c.==('?')
+ arr[2] += 1 if c.==('/')
+ end
+ arr
+ end #symbol_count
+ end #class
+ end #Parser
+end #TwitterSentiment

0 comments on commit d6954b8

Please sign in to comment.