Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

word_count option for Faker::Hipster.sentence is not working #2102

Closed
Maxeeezy opened this issue Aug 6, 2020 · 4 comments
Closed

word_count option for Faker::Hipster.sentence is not working #2102

Maxeeezy opened this issue Aug 6, 2020 · 4 comments

Comments

@Maxeeezy
Copy link

Maxeeezy commented Aug 6, 2020

The word_count oprtion for the Faker::Hipster.sentence is not working. It keeps choosing a random length of the sentence.

@Maxeeezy Maxeeezy changed the title word_count param for Faker::Hipster.sentence is not working word_count option for Faker::Hipster.sentence is not working Aug 6, 2020
@tjozwik
Copy link
Contributor

tjozwik commented Aug 14, 2020

Temporary solution

Currently, you can use it with an additional explicit param random_words_to_add, set to 0. For example:

2.6.6 :039 > Faker::Hipster.sentence(word_count: 4, random_words_to_add: 0)
 => "Chillwave park gentrify sustainable."

Context

The same issue appears to exist in Faker::Books::Lovecraft.sentence. It is caused by the random_words_to_add param that is set to the value greater than zero by default.
Line from hipster.rb:

def sentence(legacy_word_count = NOT_GIVEN, legacy_supplemental = NOT_GIVEN, legacy_random_words_to_add = NOT_GIVEN, word_count: 4, supplemental: false, random_words_to_add: 6)

Meanwhile, in lorem.rb, 0 is the default value of random_words_to_add:

def sentence(legacy_word_count = NOT_GIVEN, legacy_supplemental = NOT_GIVEN, legacy_random_words_to_add = NOT_GIVEN, word_count: 4, supplemental: false, random_words_to_add: 0)

The similar issue will probably exist when using Faker::Hipster.paragraph and Faker::Books::Lovecraft.paragraph, as there is a param random_sentences_to_add having a default value greater than 0.

Fix

It can be fixed easily as it would require changing that default values only. On the other hand, I'm not sure if it is a really unwanted behaviour, as otherwise all sentences generated using e.g. Faker::Hipster.sentence without any params, would have the same number of words (Faker::Lorem.sentence works in such a way).
But it would be a great idea to decide which version is desired in order not to have such inconsistency.

@sudeeptarlekar
Copy link
Contributor

As @tjozwik mentioned, passing random_words_to_add as 0 is fix for this. Sometimes, even after passing 0 for random words count, method generates the sentence with extra words than desired. This is because underlying method in sentence returns the array of words with space included, above I created small patch PR to fix that issue.

@sudeeptarlekar
Copy link
Contributor

@Maxeeezy if the issue is resolved can we close this?

@Maxeeezy
Copy link
Author

Thank you, @sudeeptarlekar, the issue can be closed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants