PHPackages                             nlgen/nlgen - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. nlgen/nlgen

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

nlgen/nlgen
===========

A library for creating recursive-descent natural language generators.

v0.18(3y ago)56181.3k—2.9%12[4 issues](https://github.com/DrDub/php-nlgen/issues)MITPHPPHP &gt;=7.4.0

Since Sep 6Pushed 2y ago7 watchersCompare

[ Source](https://github.com/DrDub/php-nlgen)[ Packagist](https://packagist.org/packages/nlgen/nlgen)[ Docs](http://wiki.duboue.net/PHP-NLGen)[ Fund](https://ko-fi.com/textualization)[ RSS](/packages/nlgen-nlgen/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (7)Used By (0)

[![Latest Stable Version](https://camo.githubusercontent.com/8b230194654be0573f765b7cc2acd87cebd13adb36e7f8f51b0c5c49075da586/687474703a2f2f706f7365722e707567782e6f72672f6e6c67656e2f6e6c67656e2f76)](https://packagist.org/packages/nlgen/nlgen) [![Total Downloads](https://camo.githubusercontent.com/d62099620df2fd03d67e5c51db76a3d7c04feea43bcf8a3fd565d4b50296aca0/687474703a2f2f706f7365722e707567782e6f72672f6e6c67656e2f6e6c67656e2f646f776e6c6f616473)](https://packagist.org/packages/nlgen/nlgen) [![Latest Unstable Version](https://camo.githubusercontent.com/55c5d9fe4555612775b9f59532440e0df0be29501356268824c675274e74ebb7/687474703a2f2f706f7365722e707567782e6f72672f6e6c67656e2f6e6c67656e2f762f756e737461626c65)](https://packagist.org/packages/nlgen/nlgen) [![License](https://camo.githubusercontent.com/ff3aa64f8cfe984740bf848bbe9394fd34cbf9793989681e6aff7aba7940990b/687474703a2f2f706f7365722e707567782e6f72672f6e6c67656e2f6e6c67656e2f6c6963656e7365)](https://packagist.org/packages/nlgen/nlgen)

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

[](#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.

- \[1\]
- \[2\]

Available Generation Grammars
-----------------------------

[](#available-generation-grammars)

NLGen ships with a generation grammar ready to use, that constructs text descriptions for weekly schedules. The grammar is accessible by importing `\NLGen\Grammars\Availability\AvailabilityGenerator`.

The method `generateAvailability` receives a list of "busy times" in the form of

`[ day-of-week, [ start hour, start minute ], [ end hour, end minute ] ]`

a list of ranges indicating when the scheduled day starts and ends (in the form of `[ day-of-week => [ start hour, start minute ], [ end hour, end minute ] ]`) and a constant indicating how "coarse" should be the text (one liner summarizing or very detailed).

See `examples/availability` and `tests/Availability/AvailabilityTest`.

Example:

```
use NLGen\Grammars\Availability\AvailabilityGenerator;

$gen = new AvailabilityGenerator();
$busyList = [
  [3, [16, 30], [17, 30] ],
  [6, [ 6, 55], [11, 41] ],
  [6, [14, 32], [22, 05] ]
];
$fullRanges = [];
foreach(range(0, 6) as $dow) {
 $fullRanges[$dow] = [ [6, 0], [24, 0] ];
}
echo $gen->generateAvailability($busyList, $fullRanges, AvailabilityGenerator::BASE, null);
```

Produces *All week is mostly free all day. Sunday is busy from late 6 AM to late 11 AM, and from half past 14 PM to 22 PM; the rest is free.*

Using it in your own projects
-----------------------------

[](#using-it-in-your-own-projects)

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
------------------

[](#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){
    return
      $this->person($data[0]). " " .
      $this->action($data[1], $data[2]). " " .
      $this->item($data[3]);
  }

  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
-----------------------

[](#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\].

- \[2\]
- \[3\] [http://aclweb.org/aclwiki/index.php?title=Natural\_Language\_Generation\_Portal](http://aclweb.org/aclwiki/index.php?title=Natural_Language_Generation_Portal)
- \[4\]
- \[5\] [http://wiki.duboue.net/index.php/2011\_FaMAF\_Intro\_to\_NLG](http://wiki.duboue.net/index.php/2011_FaMAF_Intro_to_NLG)

Integrations
------------

[](#integrations)

-

Sponsorship
-----------

[](#sponsorship)

Work on NLGen is sponsored by [Textualization Software Ltd.](https://textualization.com).

License
-------

[](#license)

This library is licensed under the MIT License - See the [LICENSE](LICENSE) file for details.

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity47

Moderate usage in the ecosystem

Community19

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 87.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~153 days

Recently: every ~191 days

Total

6

Last Release

1315d ago

PHP version history (2 changes)v0.11PHP &gt;=7.2

v0.18PHP &gt;=7.4.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/72d8618f45b40d65bb8ec0425d745086f27f5847402e5c67a2ef4558ac42e375?d=identicon)[DrDub](/maintainers/DrDub)

---

Top Contributors

[![DrDub](https://avatars.githubusercontent.com/u/315403?v=4)](https://github.com/DrDub "DrDub (63 commits)")[![marclaporte](https://avatars.githubusercontent.com/u/1004261?v=4)](https://github.com/marclaporte "marclaporte (4 commits)")[![Baraka24](https://avatars.githubusercontent.com/u/81784141?v=4)](https://github.com/Baraka24 "Baraka24 (2 commits)")[![renoirb](https://avatars.githubusercontent.com/u/296940?v=4)](https://github.com/renoirb "renoirb (2 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")

---

Tags

aiartificial-intelligencechatbotgrammarnatural-language-generationnlgnlpphpgrammarnlpgenerationchatbotartificial intelligencenatural language processingnatural language generationnlgtext constructionrecursive descentcomputational linguistics

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/nlgen-nlgen/health.svg)

```
[![Health](https://phpackages.com/badges/nlgen-nlgen/health.svg)](https://phpackages.com/packages/nlgen-nlgen)
```

###  Alternatives

[rubix/ml

A high-level machine learning and deep learning library for the PHP language.

2.2k1.4M28](/packages/rubix-ml)[codewithkyrian/transformers

State-of-the-art Machine Learning for PHP. Run Transformers in PHP

749231.8k5](/packages/codewithkyrian-transformers)[yooper/php-text-analysis

PHP Text Analysis is a library for performing Information Retrieval (IR) and Natural Language Processing (NLP) tasks using the PHP language

539393.0k2](/packages/yooper-php-text-analysis)[patrickschur/stanford-nlp-tagger

PHP wrapper for the Stanford Natural Language Processing library. Supports POSTagger and CRFClassifier.

7426.7k1](/packages/patrickschur-stanford-nlp-tagger)[grok-php/laravel

Seamlessly integrate Grok AI into Laravel applications with an elegant, developer-friendly package. Leverage powerful AI models for chat, automation, and NLP while maintaining Laravel's expressive simplicity.

1633.4k](/packages/grok-php-laravel)[grok-php/client

Grok PHP Client is a robust and community-driven PHP client library for seamless integration with Grok AI API, offering efficient access to advanced AI and data processing capabilities.

325.9k4](/packages/grok-php-client)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
