Skip to content
Go to file

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

NLGen: a library for creating recursive-descent natural language generators

These are pure PHP helper classes to implement recursive-descent natural language generators [1]. The classes provided are an abstract generator, an ontology container and a lexicon container.

These classes should help build simple to mid-level generators, speaking about their complexity. Emphasis has been made in keeping more advanced features out of the way for simpler cases (i.e., if there is no need to use the ontology or the lexicon, they can be skipped).

The generator keeps track of semantic annotations on the generated text, so as to enable further generation functions to reason about the text. A global context blackboard is also available.

For details on the multilingual example see the Make Web Not War talk. [2]

This is work in progress, see the ROADMAP for some insights in future development.

How to use it

Look at the examples/ folder, but in a nutshell, subclass the NLGen\Generator class and implemented a function named top. This function can return either a string or an array with a text and sem for semantic annotations on the returned text.

If you want to use other functions to assemble the text use $this->gen('name_of_the_function', $data_array_input_to_the_function) to call it (instead of $this->name_of_the_function($data_array_input_to_the_function). Or you can define your functions as protected and use function interposition, described below. The generator abstract class keeps track of the semantic annotations for you and other goodies.

If the functions that implement the grammar are protected, a dynamic class can be created with the NewSealed class method. This dynamic class will have function interception so you can call $this->name_of_function as usual but actually $this->gen will be called.

Either way you use it, to call the class, if your instantiated subclass is $my_gen then $my_gen->generate($input_data_as_an_array) will return the generated strings. If you want to access the semantic annotations, use $my_gen->semantics() afterward.

For different use cases, see the examples/ folder.

Most basic example

This example is grafted from the examples/basic folder. To be invoked command-line with php basic.php 0 0 0 0 (it produces Juan started working on Component ABC).

class BasicGenerator extends Generator {

  var $agents = array('Juan','Pedro','The helpdesk operator');
  var $events = array('started','is','finished');
  var $actions = array('working on','coding','doing QA on');
  var $themes = array('Component ABC','Item 25','the delivery subsystem');

  protected function top($data){
      $this->person($data[0]). " " .
      $this->action($data[1], $data[2]). " " .

  protected function person($agt){ return $this->agents[$agt]; }
  protected function action($evt, $act){ return $this->events[$evt]." ".$this->actions[$act]; }
  protected function item($thm) { return $this->themes[$thm];  }

global $argv,$argc;
$gen = BasicGenerator::NewSealed();
print $gen->generate(array_splice($argv,1) /*,array("debug"=>1)*/)."\n";

Learning more about NLG

I highly recommend Building Natural Language Generation Systems (2000) by Reiter and Dale.

The SIGGEN site [2] has plenty of good resources. You might also want to look at the NLG portal at the Association for Computational Linguistics wiki [3].

Last but not least, you might be interested in the author's blog [4] and the class notes of his recent NLG course [5].


Work on NLGen is sponsored by Textualization Software Ltd..


This library is licensed under the MIT License - See the LICENSE file for details.

You can’t perform that action at this time.