Coder Social home page Coder Social logo

kylestetz / sentencer Goto Github PK

View Code? Open in Web Editor NEW
378.0 12.0 40.0 650 KB

:pencil2: madlibs-style sentence templating in Javascript

Home Page: http://kylestetz.github.io/Sentencer

License: MIT License

JavaScript 100.00%
lorem-ipsum lorem-ipsum-generator madlibs generative-text

sentencer's Introduction

Sentencer

Build Status

sentencer is a node.js module for madlibs-style sentence templating. It is a simple templating engine that accepts strings with actions embedded in them:

"This is {{ an_adjective }} sentence."

Where each action returns a random string selected from a list:

"This is a bankrupt sentence."

Think of it as madlibs for Javascript. Want to roll your own lorem ipsum generator? Sentencer allows you to write the structure of your sentences and plug in any kind of vocabulary you choose.

Sentencer was written for and powers Metaphorpsum. The noun and adjective lists come from a relatively small curated selection of Ashley Bovan's excellent Word Lists for Writers.

How

npm install sentencer --save

var Sentencer = require('sentencer');

Sentencer.make("This sentence has {{ a_noun }} and {{ an_adjective }} {{ noun }} in it.");
// returns something like "This sentence has a bat and a finless cinema in it."

Here are all of the options, described in detail below.

var Sentencer = require('sentencer');

Sentencer.configure({
  // the list of nouns to use. Sentencer provides its own if you don't have one!
  nounList: [],

  // the list of adjectives to use. Again, Sentencer comes with one!
  adjectiveList: [],

  // additional actions for the template engine to use.
  // you can also redefine the preset actions here if you need to.
  // See the "Add your own actions" section below.
  actions: {
    my_action: function(){
      return "something";
    }
  }
});

Actions

Sentencer works by recognizing "actions" within {{ double_brackets }}. It replaces these actions with strings. The default actions are {{ noun }}, {{ a_noun }}, {{ nouns }}, {{ adjective }}, and {{ an_adjective }}, but you can extend Sentencer to include any kind of actions you need!

The default actions will continue to work if you pass in new a nounList and/or adjectiveList using Sentencer.configure.

Sentencer's actions are written semantically so that your sentence template still reads as a sentence. While this was simply a design decision, it does make templates easier to read and you are encouraged to follow this format if you create custom actions.

"{{ noun }}"

Returns a random noun from the noun list.

var noun = Sentencer.make("{{ noun }}")
// "actor", "knight", "orchid", "pizza", etc.

"{{ a_noun }}"

Returns a random noun from the noun list with "a" or "an" in front of it.

var nounWithArticle = Sentencer.make("{{ a_noun }}")
// "an actor", "a knight", "an orchid", "a pizza", etc.

"{{ nouns }}"

Returns the pluralized form of a random noun from the noun list. It's not 100% perfect, but it's probably 97% perfect.

var pluralNoun = Sentencer.make("{{ nouns }}")
// "actors", "knights", "orchids", "pizzas", etc.

"{{ adjective }}"

Returns a random adjective from the adjective list.

var adjective = Sentencer.make("{{ adjective }}")
// "blending", "earthy", "rugged", "untamed", etc.

"{{ an_adjective }}"

Returns a random adjective from the adjective list with "a" or "an" in front of it.

var adjective = Sentencer.make("{{ an_adjective }}")
// "a blending", "an earthy", "a rugged", "an untamed", etc.

Add your own actions

When configuring Sentencer you can provide your own "actions", which are just functions that return something. The name of the function that you pass into actions is how you will reference it within a sentence template.

Here's an example of an action that returns a random number from 1 to 10.

var Sentencer = require('sentencer');

Sentencer.configure({
  actions: {
    number: function() {
      return Math.floor( Math.random() * 10 ) + 1;
    }
  }
});

console.log( Sentencer.make("I can count to {{ number }}.")
// "I can count to 5."

Actions can take arguments

You can pass arguments into your actions. We can use this to make a smarter version of the random number generator above...

var Sentencer = require('sentencer');

Sentencer.configure({
  actions: {
    number: function(min, max) {
      return Math.floor( Math.random() * (max - min) ) + min;
    }
  }
});

console.log( Sentencer.make("I can count to {{ number(8, 10) }}.")
// "I can count to 8."

Where are the verbs?

Verb pluralization, singularization, and tense modification are difficult computer science problems. Sentencer doesn't aim to solve those problems, however present tense verb pluralization/singularization is an experimental feature of natural and could be integrated if necessary.


Sentencer was created and is maintained by Kyle Stetz. The original prototype came out of Metaphorpsum but has been rewritten from the ground up.

sentencer's People

Contributors

alexkuz avatar cefn avatar dependabot[bot] avatar kylestetz avatar michaelmior avatar nemzes avatar optikfluffel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sentencer's Issues

Case sensitivity

Hi, this is a nice library. I'm using this now to replace my manual replacements with a word list I had.

What I'm missing though is something I had that allowed 'Noun' or 'noun', or 'NOUN'. Basically I just checked how the keyword was written, if it's capitalized or fully caps, then I would replace accordingly.

For instance {{ noun }} and {{ Noun }} could return 'radio' and 'Radio'.

Just an idea.

Add custom lists to configure that can be articlized and pluralized

Type: FEATURE REQUEST

Challenge:
Imagine a UI that builds templates and lists and then executes them dynamically. Adding those lists as actions is not currently possible.

Solution:
Allow configure to accept custom lists. Besides a key and values, you can specify whether to support articlize and pluralize:

var Sentencer = require('sentencer');

Sentencer.configure({
    // the list of nouns to use. Sentencer provides its own if you don't have one!
    nounList: [],

    // the list of adjectives to use. Again, Sentencer comes with one!
    adjectiveList: [],

    customLists: [
        {
            key: "animal",
            values: ["dog", "cat", "horse", "pig", "elephant"],
            articlize: "an_animal", // if named, add action that calls articlize
            pluralize: "animals"    // if named, add action that calls pluralize
        },
        {
            key: "band",
            values: ["The Beatles", "The Who", "Styx"],
            articlize: "",  // no key or empty value, don't articlize
            pluralize: ""   // no key or empty value, don't pluralize
        }
    ],

    // additional actions for the template engine to use.
    // you can also redefine the preset actions here if you need to.
    // See the "Add your own actions" section below.
    actions: {
        my_action: function () {
            return "something";
        }
    }
});

The templates available for custom lists:

"{{ animal }}"
// "dog", "cat", "horse", "pig", "elephant"

"{{ an_animal }}"
// "a dog", "a cat", "a horse", "a pig", "an elephant"

"{{ animals }}"
// "dogs", "cats", "horses", "pigs", "elephants"


"{{ band }}"
// "The Beatles", "The Who", "Styx"

"{{ a_band }}"
// ignore

"{{ bands }}"
// ignore

Missing Dependency?

Hi - I have been enjoying using this module (thanks!)

Ran into a bit of a hiccup - if I remember correctly the first time I saw anything was in a simple 'create-react-app' sandbox. I managed to move past by simply including a node module that was mentioned in the warnings.

This time (in my custom environment for my project) I can't seem to work out the cause.

WARNING in ./node_modules/natural/lib/natural/wordnet/wordnet.js
Module not found: Error: Can't resolve 'WNdb' in '/Users/~/node_modules/natural/lib/natural/wordnet'
 @ ./node_modules/natural/lib/natural/wordnet/wordnet.js
 @ ./node_modules/natural/lib/natural/index.js
 @ ./node_modules/sentencer/index.js
 @ ./src/js/App.jsx
 @ ./src/index.js

WARNING in ./node_modules/sylvester/lib/node-sylvester/matrix.js
Module not found: Error: Can't resolve 'lapack' in '/Users/~/node_modules/sylvester/lib/node-sylvester'
 @ ./node_modules/sylvester/lib/node-sylvester/matrix.js
 @ ./node_modules/sylvester/lib/node-sylvester/index.js
 @ ./node_modules/apparatus/lib/apparatus/clusterer/kmeans.js
 @ ./node_modules/apparatus/lib/apparatus/index.js
 @ ./node_modules/natural/lib/natural/classifiers/logistic_regression_classifier.js
 @ ./node_modules/natural/lib/natural/index.js
 @ ./node_modules/sentencer/index.js
 @ ./src/js/App.jsx
 @ ./src/index.js

ERROR in ./node_modules/natural/lib/natural/classifiers/classifier.js
Module not found: Error: Can't resolve 'fs' in '/Users/~/node_modules/natural/lib/natural/classifiers'
 @ ./node_modules/natural/lib/natural/classifiers/classifier.js 132:13-26 142:13-26
 @ ./node_modules/natural/lib/natural/classifiers/logistic_regression_classifier.js
 @ ./node_modules/natural/lib/natural/index.js
 @ ./node_modules/sentencer/index.js
 @ ./src/js/App.jsx
 @ ./src/index.js

ERROR in ./node_modules/natural/lib/natural/tfidf/tfidf.js
Module not found: Error: Can't resolve 'fs' in '/Users/~/node_modules/natural/lib/natural/tfidf'
 @ ./node_modules/natural/lib/natural/tfidf/tfidf.js 27:9-22
 @ ./node_modules/natural/lib/natural/index.js
 @ ./node_modules/sentencer/index.js
 @ ./src/js/App.jsx
 @ ./src/index.js

ERROR in ./node_modules/natural/lib/natural/wordnet/data_file.js
Module not found: Error: Can't resolve 'fs' in '/Users/~/node_modules/natural/lib/natural/wordnet'
 @ ./node_modules/natural/lib/natural/wordnet/data_file.js 24:7-20
 @ ./node_modules/natural/lib/natural/wordnet/wordnet.js
 @ ./node_modules/natural/lib/natural/index.js
 @ ./node_modules/sentencer/index.js
 @ ./src/js/App.jsx
 @ ./src/index.js

ERROR in ./node_modules/natural/lib/natural/wordnet/index_file.js
Module not found: Error: Can't resolve 'fs' in '/Users/~/node_modules/natural/lib/natural/wordnet'
 @ ./node_modules/natural/lib/natural/wordnet/index_file.js 24:7-20
 @ ./node_modules/natural/lib/natural/wordnet/wordnet.js
 @ ./node_modules/natural/lib/natural/index.js
 @ ./node_modules/sentencer/index.js
 @ ./src/js/App.jsx
 @ ./src/index.js

ERROR in ./node_modules/natural/lib/natural/wordnet/wordnet_file.js
Module not found: Error: Can't resolve 'fs' in '/Users/~/node_modules/natural/lib/natural/wordnet'
 @ ./node_modules/natural/lib/natural/wordnet/wordnet_file.js 23:10-23
 @ ./node_modules/natural/lib/natural/wordnet/data_file.js
 @ ./node_modules/natural/lib/natural/wordnet/wordnet.js
 @ ./node_modules/natural/lib/natural/index.js
 @ ./node_modules/sentencer/index.js
 @ ./src/js/App.jsx
 @ ./src/index.js

ERROR in ./node_modules/sylvester/lib/node-sylvester/matrix.js
Module not found: Error: Can't resolve 'fs' in '/Users/~/node_modules/sylvester/lib/node-sylvester'
 @ ./node_modules/sylvester/lib/node-sylvester/matrix.js 4:9-22
 @ ./node_modules/sylvester/lib/node-sylvester/index.js
 @ ./node_modules/apparatus/lib/apparatus/clusterer/kmeans.js
 @ ./node_modules/apparatus/lib/apparatus/index.js
 @ ./node_modules/natural/lib/natural/classifiers/logistic_regression_classifier.js
 @ ./node_modules/natural/lib/natural/index.js
 @ ./node_modules/sentencer/index.js
 @ ./src/js/App.jsx
 @ ./src/index.js

Dry listed as a noun

Thanks so much for sentencer. It has made me happy.

To improve the word list one at a time, I can't see any natural context in which turkish dry married prepared separated unshielded or fired would be nouns that compose well in sentences, as they read more like adjectives...

"dry",

Happy to raise a PR if you agree.

Where are the spooky adjectives and nouns?

On the GitHub Page there is a "Spooky Sentence" option which looks to pull from a restricted set of adjectives and nouns but the list is (as far as I can tell) nowhere to be found either in this package or in the metaphorpsum package.

Is it possible to provide this list so we can also create our own spooky sentences?

Module not found WNdb and lapack

Getting the following errors when using sentencer.js:

./node_modules/natural/lib/natural/wordnet/wordnet.js
Module not found: Can't resolve 'WNdb' in 'C:\Users\steigen\Projects\namerator\node_modules\natural\lib\natural\wordnet'

./node_modules/sylvester/lib/node-sylvester/matrix.js
Module not found: Can't resolve 'lapack' in 'C:\Users\steigen\Projects\namerator\node_modules\sylvester\lib\node-sylvester'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.