npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

four-word-phrase

v0.0.1

Published

Pseudo-random deterministic generation of phrases from a dictionary.

Downloads

2

Readme

Four Word Phrase

Four Word Phrase is a toolkit for the creation of short phrases by pseudo-randomly picking words from a provided dictionary. When using a given seed and dictionary the sequence of phrases generated is identical.

Getting Started

Install via NPM:

npm install four-word-phrase

Generate a dictionary from the text of Moby Dick and use it to create pseudo-random phrases:

var async = require('async');
var fs = require('fs');
var path = require('path');
var fwp = require('four-word-phrase');

var generator = new fwp.generator.MemoryGenerator({
  baseSeed: 'a random seed'
});

var filePath ='path/to/four-word-phrase/example/mobyDick.txt';
var readStream = fs.createReadStream(filePath);
var importer = new fwp.importer.ReadStreamImporter(generator);

importer.import({
  readStream: readStream
}, function (error) {
  if (error) {
    return console.error(error);
  }

  var phraseLength = 4;
  async.times(10, function (index, asyncCallback) {
    generator.nextPhrase(phraseLength, asyncCallback);
  }, function (error, phrases) {
    if (error) {
      return console.error(error);
    }
    phrases.forEach(function (phrase, index) {
      console.log('Phrase ' + (index + 1) + ': ' + phrase.join(' '));
    });
  });
});

Notes on Sufficient Uniqueness

UUIDs have a space of ~10e38 possible options, so there is a vanishing chance of collision even if you generate a lot of them. Word sequences that are usefully short (meaning short enough to memorize reliably) can't approach that. Four words is probably the limit there, and this has a far smaller space:

| | 3 words | 4 words | 10 words | | --------------------- | ------- | ------- | -------- | | Dictionary: 1,000 | ~10e9 | ~10e12 | ~10e30 | | Dictionary: 10,000 | ~10e12 | ~10e16 | ~10e40 | | Dictionary: 100,000 | ~10e15 | ~10e20 | ~10e50 |

For reference, a dictionary generated from Moby Dick and restricted to words of six to fourteen letters has ~11,000 words. The English language as a whole may have ~1,000,000 words, but far from all of those are useful for this purpose. Sequences of short words are no easier to remember, but are less secure as they can be more readily brute-forced.

Creating a New Generator Implementation

To create a Generator with a different storage backend, subclass the base Generator class and implement its undefined methods.

var util = require('util');
var Generator = require('four-word-phrase').generator.Generator

var MyGenerator = function (options) {
  MyGenerator.super_.call(this, options);

  // Setup here.

}
util.inherits(MyGenerator, Generator);

// Implement the necessary methods. e.g:
MyGenerator.prototype.appendWordsToDictionary = function (words, callback) {
  // ...
};