moos / wordpos

Part-of-speech utilities for node.js based on the WordNet database.
477 stars 41 forks source link

Random word with length N #18

Closed slidenerd closed 7 years ago

slidenerd commented 7 years ago

First of all, thank you for the fantastic effort in developing and maintaining this library 👍 I was wondering how I can query a random word of length 5. Is that possible? I would like to query 4 number of random words of length a,b,c,d each? Any suggestions

moos commented 7 years ago

It's certainly possible. However, I don't think its generic enough to be part of the core library. You'll have to get a bunch of random numbers and bucketize them by length. e.g.

> wordpos.rand({count: 100}).then(function(results){ console.log(_.groupBy(results, 'length'))  });
{ '2': [ '64', '70'],
 '3': [ 'wok', 'ifc', 'fsb', 'nsa', '4wd', 'bjs', 'zip', 'myg', 'etd' ],
'4':            
 [ 'fawn',      
   'gigo',      
   'iota',      
   'bmdo',      
   '41st',      
   'xmas',      
   'tear',      
   'd.a.',      
   '32nd' ],    
'5':            
 [ 'cease',     
   'zesty',     
   'bohme',     
   'sassy',     
   'favor',     
   'poach',     
   'aptly',     
   'rosin',     
   'agism' ],   
'6':            
...
 '34': [ 'tyrannus_domenicensis_domenicensis' ] }

Four to 8-length words are fairly common. You'll have to repeat the process until you get enough words, or increase the batch size. Just remember, each word is a disk read, so there is a perf penalty for larger sets. Hope that helps.