定制 nlgen/nlgen 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

nlgen/nlgen

Composer 安装命令:

composer require nlgen/nlgen

包简介

A library for creating recursive-descent natural language generators.

README 文档

README

Latest Stable Version Total Downloads Latest Unstable Version License

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.

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

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

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

Integrations

Sponsorship

Work on NLGen is sponsored by Textualization Software Ltd..

License

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

nlgen/nlgen 适用场景与选型建议

nlgen/nlgen 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 201.93k 次下载、GitHub Stars 达 56, 最近一次更新时间为 2020 年 09 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「generation」 「grammar」 「artificial intelligence」 「nlp」 「natural language processing」 「chatbot」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 nlgen/nlgen 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 nlgen/nlgen 我们能提供哪些服务?
定制开发 / 二次开发

基于 nlgen/nlgen 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 201.93k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 56
  • 点击次数: 34
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 56
  • Watchers: 7
  • Forks: 12
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-09-06