#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
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .