1

I'm very new to coding and am working on my first rails app. I'm using the Wordnik api to return synonyms of words. So far so good. The code below works, so long as I only use one word.

Code below is in index.html.erb

<%= form_tag "/search", method: "GET" do %>
        Start Smithing:
<%= text_field_tag :search_word %>
<%= submit_tag "get those synonyms" %>

Code below is in the Wordnik controller

class WordnikController < ApplicationController
  def search
    query = params[:search_word] 
    @words = Wordnik.word.get_related(query, :type => 'synonym').first["words"]
  end
end

I'm trying to figure out how to input an entire phrase or sentence in the search and have the API return synonyms for each individual word in the sentence. This would require splitting the symbol and then passing each word individually into

Wordnik.word.get_related(query, :type => 'synonym').first["words"]

Problem is, I have zero idea how to do this. I was trying to do

query = params[:search_word].to_s.split

but that obviously didn't work.

I couldn't find anything in the Symbol documentation and I also found this question here on Stack Overflow.

Anyone have any thoughts that would point me in the right direction?

Screenshot of code with the above.

Community
  • 1
  • 1
colintherobot
  • 157
  • 2
  • 11
  • If you use `params[:search_word].split`, you should get an array of tokens (words). Are they separated by something other than spaces? What "wrong" result are you getting? – Vidya Jan 05 '14 at 20:57
  • when I had .split and use pry to see what's going on it still doesn't seem to be splitting it. – colintherobot Jan 05 '14 at 21:11
  • Have you verified that `Wordnik.word.get_related` can take multiple words at once? Or do you have to make one call for each word? – Vidya Jan 05 '14 at 21:16
  • I'm 90% sure it can't. I'm figuring I'm going to have to take the users sentence, which is saved as the symbol and then pass each word individually. I haven't even begun thinking about how I'm going to do that haha. Just trying to figure out how to split the symbol before it gets passed to wordnik. I included a screenshot in the original post of what the code looks like with the .split and the pry. Thanks for the help here! – colintherobot Jan 05 '14 at 21:19

1 Answers1

0

Perhaps Wordnik does not support array as an argument so you will need to query for every word:

def search
  query = params[:search_word].split
  @words = query.map { |word| Wordnik.word.get_related(word, :type => 'synonym').first["words"] }
end
Gosha A
  • 4,520
  • 1
  • 25
  • 33
  • Beat me to it. See the documentation: http://developer.wordnik.com/docs.html#!/word/getRelatedWords_get_4 Once you split the string you need to call the API for each element in the array as the code above illustrates. – Steve Rowley Jan 05 '14 at 21:26
  • Awesome! Sorry for the newbie question. I haven't quite figured it out yet, but I should have it in a bit and this has definitely put me on the right track! Thanks Vidya, Gosha and Steve – colintherobot Jan 05 '14 at 21:42