apostrophecms / random-words

Generate one or more common English words. Intended for use as sample text, for example generating random blog posts for testing
MIT License
245 stars 70 forks source link

Exact size of random word #13

Closed iskrich closed 1 year ago

iskrich commented 5 years ago

Thank you for library!

Is there any way for generating word with a predefined size of word length? As far as I can see, it could be implemented with minLength parameter, for example:

randomWords({minLength: 4, maxLength: 4})

iskrich commented 5 years ago

It would be helpful for my project. But If you think that it will be useful for all, I can make PR

netgfx commented 5 years ago

I have naively implemented this by doing:

function word() {
        return constants.wordListSmall[randInt(constants.wordListSmall.length)];
    }
function randInt(lessThan) {
        return Math.floor(Math.random() * lessThan);
    }

function findWord(){
   let _word = word();
   while (String(_word).length <= constants.utils.wordLengthBase) {
                _word = word();
    }
   return _word;
}

where constants.wordListSmall is the words dictionary of random-words library. And constants.utils.wordLengthBase can be anything like 3,4 just don't put -1 because it will run forever. Alternatively I would use lodash library or simple JS, to filter the words based on the String length.

boutell commented 5 years ago

This looks useful, but a problem is that it's not really well defined what lengths are even available in the dataset. I haven't looked...

On Mon, Jan 14, 2019 at 5:47 AM Michael Dobekidis notifications@github.com wrote:

I have naively implemented this by doing:

function word() { return constants.wordListSmall[randInt(constants.wordListSmall.length)]; } function randInt(lessThan) { return Math.floor(Math.random() * lessThan); }

function findWord(){ let _word = word(); while (String(_word).length <= constants.utils.wordLengthBase) { _word = word(); } return _word; }

where constants.wordListSmall is the words dictionary of random-words library. And constants.utils.wordLengthBase can be anything like 3,4 just don't put -1 because it will run forever. Alternatively I would use lodash library or simple JS, to filter the words based on the String length.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/punkave/random-words/issues/13#issuecomment-453963954, or mute the thread https://github.com/notifications/unsubscribe-auth/AAB9fVT1gHsqeIlZawwvgkfG2il1vEU_ks5vDGBDgaJpZM4Z9-0F .

--

Thomas Boutell, Chief Software Architect P'unk Avenue | (215) 755-1330 | punkave.com

netgfx commented 5 years ago

running this on the console:

var max = 0;
for(var i=0;i<wordList.length;i++){
  if(wordList[i].length > max){max = wordList[i].length;}
}
console.log(max);

printed 14 so that is the longest word on this library dictionary.

boutell commented 5 years ago

There could be lengths with no words less than 14, though.

On Mon, Jan 14, 2019 at 9:19 AM Michael Dobekidis notifications@github.com wrote:

running this on the console:

var max = 0; for(var i=0;i<wordList.length;i++){ if(wordList[i].length > max){max = wordList[i].length;} } console.log(max);

printed 14 so that is the longest word on this libraries dictionary.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/punkave/random-words/issues/13#issuecomment-454020000, or mute the thread https://github.com/notifications/unsubscribe-auth/AAB9feDEif791vF6wRwQI2FtsCCNA614ks5vDJHhgaJpZM4Z9-0F .

--

Thomas Boutell, Chief Software Architect P'unk Avenue | (215) 755-1330 | punkave.com

Vinayaka2k commented 2 years ago

++ request for this feature. I think it would be great if this library provides a feature for getting random words with the given length

boutell commented 2 years ago

PRs (and dictionary improvements) are welcome!

BoDonkey commented 1 year ago

How to work on this issue

  1. Fork this repo into your own account
  2. Make changes to the code - since it is in your account, you could work directly on main, but wouldn't you rather get some Git practice by working on a branch, and then merging to your own main?
  3. Write one or more tests to make sure your feature works
  4. Make sure your code changes don't break any existing tests.
  5. Update the CHANGELOG.md file - since other people might contribute during the same sprint cycle, add your change log message under 'UNRELEASED', not a specific version. We will add the version number when everything is approved and published on npm.
  6. When you are done, return to this repository and create a PR to pull code from your fork. Read more about this here. Make sure to fill out the PR template as best as you can.
  7. Navigate to our Discord server here if you have already joined, or here if you need an invitation and post a message in the open-source contribution channel that you need a PR review.
  8. After (hopefully) a short amount of time, one of our team engineers will review your PR and potentially advise you about things they want to see changed.
  9. If you need to make changes, go back to your local fork, make changes, and contribute those back to the main repo. This will update the PR.
  10. When you are done with changes and want a re-review, ask in Discord once again.
  11. After your PR is accepted, celebrate!!! 🎉

Code suggestions for this issue

Check out the suggestions that have already been made on this issue in earlier comments.

One edge case you need to worry about that is pointed out is if there aren't any words of a specific length or if the user wants ten words that are 6 letters long` and there are only four. Right now, there aren't any guardrails in place because the other options can almost always be fulfilled. Suggesting how you would take care of this either here on the issue or in the Discord would be great to get feedback and make sure you are on a good track.

Finally, please make use of the Discord to ask questions. Try to answer the questions yourself using internet resources, but don't be afraid to ask questions on the Discord about anything. We are here to help!