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

epures

v2.9.21

Published

Story grammar generation in JavaScript

Downloads

5

Readme

A story grammar generation library written in ES6. This is a repackaging of the client-side library tracery.

My goal is to provide more tests and structure to the project to make it scalable, and to document interesting usecases properly. I would also like it to be able to support more languages such as French, Spanish and German.

History

This is a 2018 update of Kate and George work on the tracery grammar generation library published two years ago. Since then it is unmaintained, but new opportunities in the chatbot area make it interesting for server side usage.

This library must be used with Node.js 6 Boron, or above:

Setup

$ npm install epures --save

Usage

See the samples folder for more usecases.

Running with Node.js

Basic example

$ node samples/quickstart.js
var epures = require('epures')

var grammar = epures.createGrammar({
  'animal': ['panda','fox','capybara','iguana'],
  'emotion': ['sad','happy','angry','jealous'],
  'origin':['I am #emotion.a# #animal#.'],
})

grammar.addModifiers(epures.modifiers.en_US)

console.log(grammar.flatten('#origin#'))

// I am an angry fox.

Integrating external sources of data

You can use the ES6 generator syntax to generate grammars with realistic data.

$ node samples/faker-data.js
const faker = require('faker/locale/en')
const epures = require('../index')

const getSamples = epures.generators.getSamples

const rawData = {
  bs: getSamples(20, faker.company.bs),
  catchPhraseDescriptor: getSamples(20, faker.company.catchPhraseDescriptor),
  catchPhraseAdjective: getSamples(20, faker.company.catchPhraseAdjective),
  catchPhraseNoun: getSamples(20, faker.company.catchPhraseNoun),
  bsAdjective: getSamples(20, faker.company.bsAdjective),
  origin: ['You can try our #bs# with #catchPhraseAdjective.lowerCase.a# #catchPhraseDescriptor# #catchPhraseNoun#.']
}
const grammar = epures.createGrammar(rawData)

grammar.addModifiers(epures.modifiers.en_US)

console.log(grammar.flatten('#origin#'))

// You can try our vertical visualize vortals with a streamlined even-keeled emulation.

Custom modifier

A modifier is a JavaScript object grouping a list a functions mapping string to string.

The base collection of modifiers supports common transformations on strings from the library VocaJS and is available for in various localizations. Feel free to add more supported language to contribute to the library!

You can reuse existing modifiers using epures.modifiers or define your own like in this example.

$ node samples/custom-modifier.js
const epures = require('../index')

const grammar = epures.createGrammar({
  animal: ['panda', 'fox', 'capybara', 'iguana'],
  emotion: ['sad', 'happy', 'angry', 'jealous'],
  origin: ['The #animal# is #emotion.passwordify#.']
})

const myModifier = {
  passwordify: s => new Array(s.length + 1).join('*')
}

grammar.addModifiers(myModifier)

console.log(grammar.flatten('#origin#'))

// The iguana is *****.

More usecases

More usecases can be found in the unit tests. You will also find more detailed information in the documentation.

Running with the browser

A bundle library can be generated using Webpack. Just include the epures.min.js file in the HTML, and use the library.

npm run build
$ open /samples/quickstart.html
<script src="https://cdn.jsdelivr.net/npm/epures/dist/epures.min.js"></script>

<script>
  const grammar = epures.createGrammar({
    animal: ['panda', 'fox', 'capybara', 'iguana'],
    emotion: ['sad', 'happy', 'angry', 'jealous'],
    origin: ['I am #emotion.a# #animal#.']
  })

  grammar.addModifiers(epures.modifiers.en_US)

  console.log(grammar.flatten('#origin#'))
</script>

Contributions

Changes and improvements are welcome! Feel free to fork and open a pull request into master.

Roadmap

  • [x] Fix the build (use xo, ava packages and Modern JS)
  • [x] Webpack support for client-side code.
  • [x] Merge existing unit tests (test1, test2)
  • [x] Better documentation using /samples
  • [x] Add support for more languages (notably French, German and Spanish). See faker.js for file structure and samples.
  • [ ] Add tests for the /utils classes
  • [ ] Add support for command line usage
  • [ ] Add support for Dialogflow import formats

Bugs

  • [x] Check why UglifyJsPlugin breaks the library
  • [ ] Check why 'Maximum call stack size exceeded' in the Create a grammar that can itself create valid grammars unit test. Appears randomly. Restarting Travis job solves the problem.

Running the tests

You can lint the code and run all unit tests using that script.

npm test

To run tests for only one file, I would suggest you using the ava command. For example, if you work on the English modifier only modifiers/en_US/base.js, a good practice is to run the corresponding test continuously.

ava modifiers/en_US/base.test.js --watch

Publishing

npm version [patch, minor, major]
npm publish

License

epures is licensed under the Apache 2.0 License.

References