4 Replies - 1105 Views - Last Post: 14 February 2011 - 04:13 PM Rate Topic: -----

#1 muskeris   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-February 11

Ruby script to edit out unneeded words

Posted 14 February 2011 - 03:11 PM

Just as a precursor to this, Im very new when it comes to programming in Ruby. Anyways, this script is supposed to analyze an input and return an array of the "useful words" i.e. anything not in the stop_words list. When I implement this, it returns an array consisting of all the words, so clearly, Im close, but there is a problem with the .reject method. Is that even the method I should be using? If so, how do implement it correctly?

lines = readlines
stop_words = %w{a by on for of are the with but me etc is
                and to my I has some in if am now he she it}

puts "#{ lines.join.split.reject{|str|str == stop_words}}"



Is This A Good Question/Topic? 0
  • +

Replies To: Ruby script to edit out unneeded words

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5552
  • View blog
  • Posts: 14,558
  • Joined: 18-April 07

Re: Ruby script to edit out unneeded words

Posted 14 February 2011 - 03:50 PM

Try using this line...

puts "#{ goodwords = lines.split.reject{|str|stop_words.include?(str)}}"



Now the array "goodwords" will contain all the important words. What this is saying is take the user input text, split it and then for each item test if it is included in the stop_words array. If it is not, store the word in the array that eventually is put into goodwords.

You can check this by then looping through goodwords and printing each value. :)


P.S. This is assuming lines is a string. If lines is an array of strings, you can then loop through each, split them and check their words.

This post has been edited by Martyr2: 14 February 2011 - 03:53 PM

Was This Post Helpful? 0
  • +
  • -

#3 muskeris   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-February 11

Re: Ruby script to edit out unneeded words

Posted 14 February 2011 - 04:04 PM

Oh sweet. That was almost it (or maybe it was right and i just have something else wrong that made both wrongs right >.>). I just added .join between the lines.split on yours. Thank you very much. Btw, do you know what was wrong with my first example that was giving me the incorrect answer? Just trying to learn a bit ;)
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5552
  • View blog
  • Posts: 14,558
  • Joined: 18-April 07

Re: Ruby script to edit out unneeded words

Posted 14 February 2011 - 04:11 PM

stop_words is going to be an array of items. You were trying to compare each string to the array as a whole, not checking each item in stop_words to see if it equals str. It would be like taking an apple from a barrel of apples and then comparing it directly to a barrel of fruit. Not comparing the apple against each fruit item in the barrel itself and throwing it away when you find another apple.

If that analogy makes any sense. :D
Was This Post Helpful? 0
  • +
  • -

#5 muskeris   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-February 11

Re: Ruby script to edit out unneeded words

Posted 14 February 2011 - 04:13 PM

Ah I see. Cool. Thanks again for the help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1