#Make a new Madlib Example: search with sentiment
##Make a button to direct to your madlib in tracks/index.html.erb
<div class="item">
<div class="carousel-caption container">
<div class="row">
<div class="col-sm-7">
<%= render "tracks/madlibs/02_sentiment" %>
<div class="portfolio-wrapper" >
<div class="col-md-12">
<ul class="filter">
<li>Input forms:</li>
<li data-target="#home-carousel" data-slide-to="0"><a>Simple</a></li>
<li data-target="#home-carousel" data-slide-to="1"><a class="active">Sentiment</a></li>
<li data-target="#home-carousel" data-slide-to="2" ><a>Party</a></li>
</ul><!--/#portfolio-filter-->
</div>
</div>
<img
class="f-pending-message"
style="width:500px;height:180px;display:none;"
src="https://userscontent2.emaze.com/images/c99e58c5-cc05-4af4-ab93-4614a40e077b/386d9c23-88e2-487a-b84f-47bba61bb4a6.gif"
/>
</div>
</div>
</div>
</div>
##Make a view for your madlib in app/views/madlibs/
<%= form_tag("/search_with_sentiment", method: "get", class: "form-search", remote: true) do %>
<h1>Looking for a song about
<%= text_field_tag(:word, "", placeholder:"(topic)") %>
that makes me feel
<select name="feeling" class="dotted-btn">
<ul class="dropdown-menu">
<li><option value="happy">happy</option></li>
<li><option value="calm">calm</option></li>
<li><option value="angry">angry</option></li>
<li><option value="sad">sad</option></li>
</ul>
</select>
<%= submit_tag("Search", class:"btn btn-send") %>
</h1>
<% end %> <!-- form -->
##Make a route in config/routes
get "/search_with_sentiment", to: "tracks#search_with_sentiment"
##Make a method in tracks controller
#Search by keyword and sentiment
def search_with_sentiment
@form_feeling = params[:feeling]
p "in search_with_sentiment"
@tracks = TracksHelper::Track.lyrics_keywords(params[:word], 20).select{ |t| t.match_sentiment(@form_feeling)}
respond_to do |format|
if @tracks.length > 0
format.html {render :show, layout: false}
else
flash[:danger] = 'There was a problem'
format.html { render :index }
format.json { }
end
end
end
##Make a helper method in the Tracks Helper
#For 02_sentiment madlib
#Filter by matching given feeling
def match_sentiment(form_feeling)
if form_feeling == "sad"
p "You want to be sad"
audio_features.valence < 0.4
elsif form_feeling == "angry"
p "You want to be angry"
audio_features.valence >= 0.4 && audio_features.valence <= 0.6
elsif form_feeling == "calm"
p "You want to be calm"
audio_features.valence > 0.4 && audio_features.tempo < 100
elsif form_feeling == "happy"
p "You want to be happy"
audio_features.valence > 0.6
end
end
##Simpler example
class Track
attr_reader :name, :valence, :match_sentiment
def initialize(attributes)
@name = attributes[:name]
@valence = attributes[:valence]
end
end#for class Track
def Track.class.match_sentiment(form_feeling)
if form_feeling == "sad"
valence < 0.4
elsif form_feeling == "happy"
valence > 0.6
end
end
t = Track.new({name:"happy", valence:0.7})
b = Track.new({name:"sad", valence:0.2})
tracks = [t,b]
#Get only sad tracks
form_feeling = "sad"
tracks.select{|t| t.match_sentiment(form_feeling)}
#Get only happy tracks
form_feeling = "happy"
tracks.select{|t| t.match_sentiment(form_feeling)}