abitdodgy/words_counted

Exclusion doesn't seem to work.....

muaad opened this issue · 9 comments

Hi,
I wanted to be able to exclude some words from the count but, I have noticed that those words were been included. It says in the documentation (https://github.com/abitdodgy/words_counted#excluding-words-from-the-analyser):

WordsCounted::Counter.new(
  "Magnificent! That was magnificent, Trevor.", exclude: "was magnificent"
)
counter.words
#=> ["That", "Trevor"]

But, this is what I found:

counter = WordsCounted::Counter.new(
   "Magnificent! That was magnificent, Trevor.", exclude: "was magnificent"  
)  
counter.words
=> ["Magnificent", "That", "was", "magnificent", "Trevor"]

Could you please look into that? Or, am I missing something?

I'm not the maintainer, but I have an idea what might be wrong.

Try replacing exclude: "was magnificant" with exclude: ["was", "magnificant"], or the equivalent exclude: %w[was magnificant] and see if that helps.

The exclude option takes a list of works to exclude, Since "was" and "magnificant" are counted as separate words, they have to be listed separately.

Is it your intent to exclude those words only when they occur together as a phrase, but to include them otherwise?

@wconrad thanks for chipping in. It's very strange because all the specs are passing and there is a test for this:

it "it accepts a string filter" do
  counter = Counter.new("That was magnificent, Trevor.", exclude: "magnificent")
  expect(counter.words).to eq(%w[That was Trevor])
end

Yet, in the console, the exclude argument is not working indeed.

counter = WordsCounted::Counter.new("That was magnificent, Trevor.", exclude: "magnificent")
counter.word_count
=> 4 
counter.word_occurrences
=> {"that"=>1, "was"=>1, "magnificent"=>1, "trevor"=>1} 
counter.words
=> ["That", "was", "magnificent", "Trevor"] 

I'm not sure how to explain that. Could be a different version of Ruby? Perhaps my local gem version running the tests is different from the published version?

I've gone ahead and failed the test deliberately, and it fails correctly:

it "it accepts a string filter" do
  counter = Counter.new("That was magnificent, Trevor.", exclude: "magnificent")
  # Added 'magnificent' to the array to see if the test will fail
  expect(counter.words).to eq(%w[That was Trevor magnificent])
end
Failures:

  1) WordsCounted::Counter.words it accepts a string filter
     Failure/Error: expect(counter.words).to eq(%w[That was Trevor magnificent])

       expected: ["That", "was", "Trevor", "magnificent"]
            got: ["That", "was", "Trevor"]

       (compared using ==)
     # ./spec/words_counted/counter_spec.rb:61:in `block (3 levels) in <module:WordsCounted>'

Finished in 0.00662 seconds (files took 0.65759 seconds to load)
31 examples, 1 failure

@abitdodgy I would bet on the "different versions of the gem" theory.

Did you see that @muaad's exclusion option is a single string with two words in it? I thought that might be the problem. What do you think?

@wconrad it's a different gem versions. The RubyGems version is out of date, despite showing the correct version number. Passing in a string of multiple words, as in @muaad's case, works fine. I will add a test for this use case to have better coverage.

I installed the gem from Git and tested it in the console, and like testing the local copy using rake console, it worked fine. Earlier I installed it from Ruby gems and used IRB to test it, and that did not work.

@muaad until I publish the git version to RubyGems, you can use gem 'words_counted', github: 'abitdodgy/words_counted'

Ah. Got it. Thanks for the explanation.

Thanks @wconrad and @abitdodgy for the help. Appreciated it. Doing gem 'words_counted', github: 'abitdodgy/words_counted' fixed it for me.

@wconrad @muaad thanks for chipping in. This is now fixed in 823824f and RubyGems should have the correct version.

Great. Thanks.