faker-ruby / faker

A library for generating fake data such as names, addresses, and phone numbers.
MIT License
11.24k stars 3.18k forks source link

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

Closed Maxeeezy closed 3 years ago

Maxeeezy commented 4 years ago

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

tjozwik commented 4 years ago

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 commented 4 years ago

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 commented 3 years ago

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

Maxeeezy commented 3 years ago

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