Code Monkey home page Code Monkey logo

nangram's Introduction

NanGram

Negative Nancy's expression generator & parser! XD

Write grammars, generate random expressions, parse expressions.

Installation

pip install nangram

Usage

First write yourself a nice little grammar like this!:

grammar.bnf:

determiner = "the " | "a " .
noun = "cat " | "dog " | "chair " .
verb = "runs " | "snacks " | "plays " .
adverb = "quickly" | "ferociously" | "sneakily" .

noun_phrase = determiner noun .
verb_phrase = verb adverb .
sentence = noun_phrase verb_phrase .

main = sentence .

Then import nangram and load the grammar source:

import nangram

grammar = nangram.Grammar.load_bnf('grammar.bnf')

Then generate some expressions!:

for expression in grammar.generate():
    print(expression)

And you get...

the cat plays ferociously
the dog plays ferociously
the chair snacks quickly
a cat snacks quickly
a dog runs quickly
a dog snacks ferociously
a dog plays quickly
a chair plays ferociously

Then try parsing something:

for parse_tree in grammar.parse('a cat snacks sneakily'):
    print(parse_tree)

And you get...

sentence = 'a cat snacks sneakily'
├ noun_phrase = 'a cat '
│ ├ determiner = 'a '
│ └ noun = 'cat '
└ verb_phrase = 'snacks sneakily'
  ├ verb = 'snacks '
  └ adverb = 'sneakily'

(The parser will yield as many parse trees as are valid, so if you have an ambiguous grammar, for example, you can parse all variations. If the expression is not in the language, you won't get any parse trees.)

See examples/english.bnf and examples/english.py for another example.

nangram's People

Contributors

negativefnnancy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

tubbz-alt

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.