49714 Programming for Online Prototypes
· 15 members
A hands on introduction to building online products and services through code
Freud is a SMS-based bot that interprets and visualizes your dreams. He keeps your dreams securely in your personal dream collection and spot patterns in your dreams that will help you on your journey of self-discovery.
#Define methods
get "/test-nlp" do
text = "A hero can be anyone. Even a man doing something as simple and reassuring as
putting a coat around a little boy's shoulder to let him know that the world hadn't ended."
response = get_npl_for(text)
puts response.to_json
keywords = get_keywords_from_response response
keywords.to_s
end
def get_keywords_from_response resp
return [] if resp.nil?
keywords = []
puts resp
resp["keywords"].each do |kw|
keywords << kw["text"]
end
return keywords
end
def get_npl_for text
features = {
sentiment: {}, keywords: {}, concepts: {}, emotion: {}, entities: {}
}
data = {
"features" => features,
"text" => text
}
params = { "version" => "2018-03-19" }
headers = {
"Content-Type"=>"application/json"
}
auth = { username: "apikey", password: ENV['WATSON_API_KEY'] }
data = data.to_json if data.instance_of?(Hash)
url = ENV["WATSON_URL"]
method = "/v1/analyze"
response = HTTParty.post(
url + method,
basic_auth: auth,
headers: headers,
query: params,
body: data
)
end
#SEARCH FOR KEYWORDS
if body.include? "i dreamt"
session[:dream] = body
response = get_npl_for( body )
keywords = get_keywords_from_response( response )
puts keywords
answer = search_answer_for (keywords[0])
image = search_unsplash_for( keywords.join( ", ") )
media = image
message = keywords[0].capitalize + " was a symbol in your dream. " + answer
end
Click to Expand
#Define Method to search for symbols in symbols.txt file
def search_answer_for body
message = " "
array_of_lines = IO.readlines("symbols.txt")
array_of_lines.each do |line|
items=[ ]
items=line.split ("=")
symbols=items[0]
answers=items[1]
if body.include?symbols.to_s
message = items[1]
end
end
return message.to_s
end
Click to Expand
#Define unsplash methods to search for images with keywords
def search_unsplash_for response
Unsplash.configure do |config|
config.application_access_key = ENV['UNSPLASH_ACCESS_KEY']
config.application_secret = ENV['UNSPLASH_SECRET']
config.utm_source = "ExampleAppForClass"
end
search_results = Unsplash::Photo.search( response, 1, 1)
puts search_results.to_json
images = ""
puts search_results.size
search_results.each do |result|
#puts result.to_json
puts "Result"
image_thumb = result["urls"]["thumb"]
puts result["urls"]["thumb"].to_json
image_description = result["description"].to_s
images += "<img src='#{ image_thumb.to_s }' /><br/>"
images += "<hr/>"
return image_thumb.to_s
end
images
end
#Search
image = search_unsplash_for( keywords.join( ", ") )
Click to Expand
A hands on introduction to building online products and services through code
Freud is a SMS-based bot that interprets and visualizes your dreams. He keeps your dreams securely in your personal dream collection and spot patterns in your dreams that will help you on your journey of self-discovery.
October 19th, 2018