tenqz/lingua
Composer 安装命令:
composer require tenqz/lingua
包简介
A comprehensive PHP library for advanced text processing using Chain of Responsibility pattern
关键字:
README 文档
README
Lingua v2.1.1
Lingua is a comprehensive PHP library designed for advanced text processing. It implements the Chain of Responsibility pattern to provide flexible and extensible text processing capabilities.
Note: This library is currently under active development. The current version may not reflect the final quality and API stability. Breaking changes may occur in future releases.
Features
- Chain of Responsibility pattern for text processing
- Flexible scenario-based processing
- Easy to extend with custom handlers
- PHP 8.1+ support
Available Handlers
The library provides several built-in text processing handlers:
SpecialCharsHandler
Removes special characters from text while preserving words and spaces. This handler:
- Removes all special characters (punctuation marks, symbols, etc.)
- Replaces newlines with spaces
- Preserves spaces between words
Example:
use Tenqz\Lingua\Handlers\Basic\SpecialCharsHandler; $handler = new SpecialCharsHandler(); $result = $handler->handle("Hello! @#$%^&*() World..."); // Returns: "Hello World"
NormalizeSpacesHandler
Normalizes whitespace in text. This handler:
- Replaces multiple spaces, tabs, and newlines with a single space
- Preserves leading and trailing whitespace
Example:
use Tenqz\Lingua\Handlers\Basic\NormalizeSpacesHandler; $handler = new NormalizeSpacesHandler(); $result = $handler->handle("Hello World\t\nTest"); // Returns: "Hello World Test"
TrimHandler
Removes whitespace from the beginning and end of text. This handler:
- Removes spaces, tabs, and newlines from the beginning and end of text
- Preserves whitespace between words
Example:
use Tenqz\Lingua\Handlers\Basic\TrimHandler; $handler = new TrimHandler(); $result = $handler->handle(" Hello World "); // Returns: "Hello World"
HtmlTagsHandler
Removes HTML tags from text while preserving the content between them. This handler:
- Removes all HTML tags using PHP's
strip_tagsfunction - Preserves HTML entities
- Normalizes multiple spaces into single spaces
- Trims spaces from the beginning and end of text
Example:
use Tenqz\Lingua\Handlers\Basic\HtmlTagsHandler; $handler = new HtmlTagsHandler(); $result = $handler->handle("<p>Hello</p> <div>World</div>"); // Returns: "HelloWorld" $result = $handler->handle("<p>Hello & World</p>"); // Returns: "Hello & World"
Installation
composer require tenqz/lingua
Basic Usage
use Tenqz\Lingua\Core\TextProcessor; use Tenqz\Lingua\Core\AbstractTextHandler; use Tenqz\Lingua\Core\Contracts\TextHandlerInterface; // Create a custom handler class CustomHandler extends AbstractTextHandler { protected function process(string $text): string { // Your text processing logic here return $text; } } // Initialize the processor $processor = new TextProcessor(); // Add handlers to the chain $processor->addHandler(new CustomHandler()); // Process text $result = $processor->process('Your text here');
Chain of Handlers Example
You can combine multiple handlers to create a processing pipeline:
use Tenqz\Lingua\Core\TextProcessor; use Tenqz\Lingua\Handlers\Basic\SpecialCharsHandler; use Tenqz\Lingua\Handlers\Basic\NormalizeSpacesHandler; use Tenqz\Lingua\Handlers\Basic\TrimHandler; // Initialize the processor $processor = new TextProcessor(); // Create a chain of handlers $processor ->addHandler(new SpecialCharsHandler()) // First, remove special characters ->addHandler(new NormalizeSpacesHandler()) // Then, normalize spaces ->addHandler(new TrimHandler()); // Finally, trim whitespace // Process text through the entire chain $result = $processor->process(' Hello! @#$%^&*() World... '); // Result: "Hello World"
Architecture
The library consists of the following main components:
TextHandlerInterface
Interface that defines the contract for all text processing handlers:
interface TextHandlerInterface { public function handle(string $text): string; public function setNext(TextHandlerInterface $handler): TextHandlerInterface; public function getNext(): ?TextHandlerInterface; }
AbstractTextHandler
Abstract base class that implements the Chain of Responsibility pattern:
abstract class AbstractTextHandler implements TextHandlerInterface { protected ?TextHandlerInterface $nextHandler = null; public function setNext(TextHandlerInterface $handler): TextHandlerInterface { $this->nextHandler = $handler; return $handler; } public function getNext(): ?TextHandlerInterface { return $this->nextHandler; } public function handle(string $text): string { $processedText = $this->process($text); if ($this->nextHandler !== null) { return $this->nextHandler->handle($processedText); } return $processedText; } abstract protected function process(string $text): string; }
TextProcessor
Facade class for managing text processing chains:
class TextProcessor implements TextProcessorInterface { private ?TextHandlerInterface $handlersChain = null; public function addHandler(TextHandlerInterface $handler): self { if (!$this->handlersChain) { $this->handlersChain = $handler; return $this; } $lastHandler = $this->handlersChain; while ($lastHandler->getNext()) { $lastHandler = $lastHandler->getNext(); } $lastHandler->setNext($handler); return $this; } public function process(string $text): string { if (!$this->handlersChain) { throw new NotFoundHandlerException(); } return $this->handlersChain->handle($text); } }
Creating Custom Handlers
To create a custom handler, extend the AbstractTextHandler class and implement the process method:
use Tenqz\Lingua\Core\AbstractTextHandler; class MyCustomHandler extends AbstractTextHandler { protected function process(string $text): string { // Your text processing logic here return $text; } }
Error Handling
The library throws the following exceptions:
NotFoundHandlerException: Thrown when attempting to process text with no registered handlers
Testing
composer test
Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under the MIT License
tenqz/lingua 适用场景与选型建议
tenqz/lingua 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 4, 最近一次更新时间为 2025 年 05 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「nlp」 「chain-of-responsibility」 「text-processing」 「text-manipulation」 「text-analysis」 「text-normalization」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tenqz/lingua 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tenqz/lingua 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tenqz/lingua 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Interface for Babel Street Text Analytics
A light library that simplify the implementation of a chain of responsibility
A modern PHP client for the Hugging Face Inference API with full type safety and comprehensive model support
Alibaba Cloud Nlp SDK for PHP
A collection of fast and easy to use attributed string classes for PHP. Attributed strings can have multiple attributes for each character of the string and are for example used for word processors and natural language processing.
Convenient package to create polls (questionnaires) from pure text minimally organized in lines.
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 33
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-05-08