PHPackages                             tenqz/lingua - 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. tenqz/lingua

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

tenqz/lingua
============

A comprehensive PHP library for advanced text processing using Chain of Responsibility pattern

v2.1.1(1y ago)41MITPHPPHP ^8.1CI passing

Since May 8Pushed 1y ago1 watchersCompare

[ Source](https://github.com/tenqz/lingua)[ Packagist](https://packagist.org/packages/tenqz/lingua)[ Docs](https://github.com/tenqz/lingua)[ RSS](/packages/tenqz-lingua/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (2)Versions (16)Used By (0)

[![Build Status](https://github.com/tenqz/lingua/workflows/Tests/badge.svg)](https://github.com/tenqz/lingua/actions)[![Total Downloads](https://camo.githubusercontent.com/1686b704c453f51f65dc933e01f2e68ccb71a9beb8e198d0c0f45d5a985e7a4d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f74656e717a2f6c696e677561)](https://packagist.org/packages/tenqz/lingua)[![Latest Stable Version](https://camo.githubusercontent.com/950c3bf8e8854f8d9bee233319d7af2418117664d831c9e43b4ee6a447b0e4c5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f74656e717a2f6c696e677561)](https://packagist.org/packages/tenqz/lingua)[![License](https://camo.githubusercontent.com/e93564c478f4ea7751ade151a8f8f0f8b0ebb182ef294c2270716c4c3ea62987/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f74656e717a2f6c696e677561)](https://packagist.org/packages/tenqz/lingua)

Lingua v2.1.1
=============

[](#lingua-v211)

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

[](#features)

- Chain of Responsibility pattern for text processing
- Flexible scenario-based processing
- Easy to extend with custom handlers
- PHP 8.1+ support

Available Handlers
------------------

[](#available-handlers)

The library provides several built-in text processing handlers:

### SpecialCharsHandler

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

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

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

[](#htmltagshandler)

Removes HTML tags from text while preserving the content between them. This handler:

- Removes all HTML tags using PHP's `strip_tags` function
- 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("Hello World"); // Returns: "HelloWorld"
$result = $handler->handle("Hello &amp; World"); // Returns: "Hello &amp; World"
```

Installation
------------

[](#installation)

```
composer require tenqz/lingua
```

Basic Usage
-----------

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

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

[](#architecture)

The library consists of the following main components:

### TextHandlerInterface

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

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

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

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

[](#error-handling)

The library throws the following exceptions:

- `NotFoundHandlerException`: Thrown when attempting to process text with no registered handlers

Testing
-------

[](#testing)

```
composer test
```

Contributing
------------

[](#contributing)

1. Fork the repository
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request

License
-------

[](#license)

This project is licensed under the MIT License

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance46

Moderate activity, may be stable

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~0 days

Total

15

Last Release

420d ago

Major Versions

v1.1.8 → v2.0.02025-05-11

PHP version history (2 changes)v1.0.0PHP ^8.0

v1.1.7PHP ^8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/12949394?v=4)[Oleg Patsay](/maintainers/tenqz)[@tenqz](https://github.com/tenqz)

---

Top Contributors

[![tenqz](https://avatars.githubusercontent.com/u/12949394?v=4)](https://github.com/tenqz "tenqz (18 commits)")

---

Tags

nlptext-processingtext analysischain of responsibilitytext normalizationtext-manipulationrussian-languagetext-cleanup

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

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

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

###  Alternatives

[rubix/ml

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

2.2k1.5M28](/packages/rubix-ml)[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

541420.5k4](/packages/yooper-php-text-analysis)[nlp-tools/nlp-tools

NlpTools is a set of php 5.3+ classes for beginner to semi advanced natural language processing work.

775672.1k5](/packages/nlp-tools-nlp-tools)[codewithkyrian/transformers

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

758267.3k9](/packages/codewithkyrian-transformers)[davmixcool/php-sentiment-analyzer

PHP Sentiment Analyzer is a lexicon and rule-based sentiment analysis tool that is used to understand sentiments in a sentence using VADER (Valence Aware Dictionary and sentiment Reasoner).

136173.7k1](/packages/davmixcool-php-sentiment-analyzer)[nlgen/nlgen

A library for creating recursive-descent natural language generators.

56202.6k](/packages/nlgen-nlgen)

PHPackages © 2026

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