Skip to content

Commit

Permalink
awesomeness search
Browse files Browse the repository at this point in the history
  • Loading branch information
plukevdh committed Jan 4, 2011
1 parent a66a577 commit d003bf6
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 77 deletions.
82 changes: 28 additions & 54 deletions pants.rb
Expand Up @@ -3,60 +3,42 @@
require 'twitter'
require 'redis'

PANTS = "pants OR jeans OR slacks"

ON = %w(on wearing wear wore worn in)
OFF = %w(off no down without out don't didn't won't)
FLAG = %w(should might had put get back)

REDIS_CONNECTION = redis = Redis.new(host: "localhost", port: 6379)

helpers do
def redis
REDIS_CONNECTION
end

def calculate(status)
parts = status.split(' ')
parts.delete_if {|x| x.downcase! ; (!ON.include? x and !OFF.include? x and !FLAG.include? x) }

questionable = 0
on = 0
off = 0

parts.each do |x|
if FLAG.include? x
questionable += 1
elsif ON.include? x
on -= 1
elsif OFF.include? x
off += 1
def calculate(statuses, term, positive, negative)
# get texts
tweets = statuses.map {|x| x.text}
# delete tweets containing both terms
pos = []
neg = []

tweets.each do |tweet|
if tweet.include?(positive) && !tweet.include?(negative)
pos << tweet
elsif !tweet.include?(positive) && tweet.include?(negative)
neg << tweet
else
tweets.delete tweet
end
end

id = key_log(status)
# save search terms to redis
redis.hset(:terms, id, parts.join(","))

percent = ((on + off).to_f.abs / parts.size.to_f) * 100.0
variation = (questionable.to_f / parts.size.to_f) * 100.0
tc = tweets.count
nc = neg.count
pc = pos.count

percent = 0 if percent.nan?
variation = 100 if variation.nan?
# store this result in redis to be updated on future searches
tweets.each { |x| redis.sadd("tweets:#{term}", tweets) }
redis.set "total:#{term}:count", tc
redis.set "positive:#{term}:count", pc
redis.set "negative:#{term}:count", nc

redis.hset(:stats, id, [percent, variation].join(":"))

"has a #{percent}% chance of pantslessness with a margin of #{variation}!"
end

def key_log(status)
# incr counter
id = redis.incr :uniq_id
# save tweet for reference
redis.hset(:tweets, id, status)
return id
return tc.to_f, pc.to_f, nc.to_f
end

end


Expand All @@ -65,27 +47,19 @@ def key_log(status)
end

post '/search' do
search = Twitter::Search.new
search.containing(PANTS).from(params[:username])
search = Twitter::Search.new.phrase(params[:term]).containing("#{params[:positive]} OR #{params[:negative]}").per_page(100)
statuses = search.fetch

if statuses.empty?
status = "currently has no data pertaining to pants."
key_log("No status found")
else
status = calculate(statuses.first.text)
end

@result = "User <a href='http://twitter.com/#{params[:username]}'>@#{params[:username]}</a> #{status}"
@total, @positive, @negative = calculate(statuses, params[:term].downcase, params[:positive], params[:negative])

@tweet = statuses.first.text unless statuses.empty?
@id = redis.get "uniq_id"
@pcent = (@positive/@total)*100.0
@ncent = (@negative/@total)*100.0

erb :status
end

post '/evaluate' do
id = params[:id]
redis.hset :evaluation, params[:id], params[:eval]
redis.hset :evaluation, params[:term], params[:eval]
end

24 changes: 20 additions & 4 deletions public/stylesheets/style.css
Expand Up @@ -52,10 +52,6 @@ table, caption, tbody, tfoot, thead, tr, th, td {


/* @end */
* {
margin: 40px;
}

body {
font-family: helvetica;
background:
Expand All @@ -66,13 +62,20 @@ body {
color: #ebeaeb;
}

h1 { font-size: 30pt; }
h2 { font-size: 20pt; }
h2 { font-size: 18pt; }

div { padding: 20px; }

.content {
height: auto;
padding: 20px;
padding-top: 45px;
width: 700px;
margin-right: auto;
margin-left: auto;
margin-top: 0;
background-color: rgba(47,47,47,0.8);
-moz-border-radius-bottomleft: 15px;
-moz-border-radius-bottomright: 15px;
Expand Down Expand Up @@ -108,6 +111,7 @@ body {
border: 1px solid #c3c3c3;
text-align: center;
padding: 5px;
margin: 2px;
}

.result .data {
Expand All @@ -129,3 +133,15 @@ t {
color: #5253f7;
}

.graph-positive, .graph-negative {
width:5px;
height:50px;
display:inline-block;
padding: 0;
margin: 0;
}
.graph-positive { background-color: #905C71; }
.graph-positive:hover { background-color: #cd86a5; }
.graph-negative { background-color: #6486A1; }
.graph-negative:hover { background-color: #92c2e9; }

2 changes: 1 addition & 1 deletion views/layout.erb
Expand Up @@ -31,7 +31,7 @@
<body>

<div class="content">
<div class="title">
<div class="title" style="padding:0;">
<h1> St<b>at</b>e <b>o</b>f t<b>he</b> P<b>an</b>t<b>s</b> </h1>
</div>

Expand Down
25 changes: 21 additions & 4 deletions views/pants.erb
@@ -1,12 +1,29 @@
<div class="search">
<h3 class="message">Enter a twitter username</h3>

<form action='/search' method="post">
<input type="text" name="username" />
<h3 class="message">Enter a word to search by:</h3>
<span class="sub">(ex. chocolate)</span>
<br/>
<input type="submit" value="State of the Pants" class="awesome big" />

<div>
<input type="text" name="term" />
</div>

<div>
<h3 class="message">Enter a positive and a negative to poll with</h3>
<span class="sub">(ex. love + hate)</span>
</div>

<div>
<input type="text" name="positive" size="8" />
<span style="font-size:28pt">+</span>
<input type="text" name="negative" size="8" />
</div>

<div>
<input type="submit" value="State of the Pants" class="awesome big" />
</div>
</form>

<br/>
<br/>
</div>
39 changes: 25 additions & 14 deletions views/status.erb
@@ -1,33 +1,44 @@
<div class="result">
<div class="data">
<h2><%= @result %> </h2>

<p>Based on <%= @total.to_i %> data-points, people give the following results regarding:</p>

<br/>
<h2><%= params[:term] %></h2>
<br/>

<%= '%.2f' % @pcent %>% <%= params[:positive] %>
<div id="positive">
<% @pcent.to_i.times do %>
<div class="graph-positive"></div>
<% end %>
</div>

<% if @tweet %>
<p style="font-size:13pt;" class="message">
This is loosely<span style="vertical-align:top; color:#a80000; font-size:10pt">*</span> based on the following tweet:
</p>
<p style="color:#5253f7">"<%= @tweet %>"</p>
<% end %>
<br/>

<%= '%.2f' % @ncent %>% <%= params[:negative] %>
<div id="negative">
<% @ncent.to_i.times do %>
<div class="graph-negative"></div>
<% end %>
</div>

<div id="evals" style="padding:30px">
<h2>Help us make us better</h2>
<h3>Help us make us better</h3>
Was this evaluation any good?
<div style="padding-top:15px">
<a href="/evaluate?id=<%=@id%>&eval=yes" class="eval awesome"><img src="../images/up.png" alt="Yes" /></a>
<a href="/evaluate?id=<%=@id%>&eval=no" class="eval awesome"><img src="../images/down.png" alt="No" /></a>
</div>
</div>

<div style="padding-top:40px">
<div>
<a href="/" class="awesome big">Again</a>
</div>

<p style="font-size:9pt; color:#999" class="disclaimer">
<span style="vertical-align:super; color:#a80000;">*</span>Please keep in mind that this is in no way an accurate representation of whether or not
this individual actually is wearing pants. We are meerly estimating the likelihood of
the current pants situation. Please take all results with a grain of salt, two cents, and any other
measure that cancels all credibility of these results.
<span style="vertical-align:super; color:#a80000;">*</span>
These stats are aggregated based on all searches for <b><%= params[:term] %></b>, therefore the results may be better or worse depending on how many searches
have been made on this term.
</p>

</div>

0 comments on commit d003bf6

Please sign in to comment.