PHPackages                             codechap/context-trimmer - 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. codechap/context-trimmer

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

codechap/context-trimmer
========================

A tokenizer-agnostic text preprocessor to trim context for LLMs.

02PHP

Since Mar 13Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/codeChap/contextTrimmer)[ Packagist](https://packagist.org/packages/codechap/context-trimmer)[ RSS](/packages/codechap-context-trimmer/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

Context Trimmer
===============

[](#context-trimmer)

**A tokenizer-agnostic text preprocessor for trimming context in LLM applications.**

*Requires PHP 8.2 or higher.*

This library provides functions to process, trim, and optimize text for large language model (LLM) context windows. It includes options for removing short words, stripping extraneous punctuation, and compressing whitespace.

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

[](#installation)

Install via Composer:

```
composer require codechap/context-trimmer:"dev-master"
```

Usage
-----

[](#usage)

Create a file (for example, `run.php`) with the following code to see the ContextTrimmer in action:

```
require_once 'vendor/autoload.php';

use codechap\ContextTrimmer\ContextTrimmer;

// Load your context from a file
$input = file_get_contents('context.txt');

// Configure and trim the input text using chained setters
$result = new ContextTrimmer()
    ->set('removeShortWords', true)
    ->set('minWordLength', 2)
    ->set('removeExtraneous', true)
    ->set('maxTokens', 50)
    ->trim($input);

// Output the trimmed text segments as JSON
echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
```

In this example, the `ContextTrimmer` is configured to remove short words, strip extraneous punctuation, and limit tokens per segment (50 tokens in this case). The resulting trimmed output is returned as an array of text segments.

Running Tests
-------------

[](#running-tests)

To run the tests, use:

```
composer test
```

License
-------

[](#license)

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

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

[](#contributing)

Contributions and pull requests are welcome! Please follow the existing coding standards and include tests for new functionality.

###  Health Score

19

—

LowBetter than 9% of packages

Maintenance53

Moderate activity, may be stable

Popularity2

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity12

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/eb9295442ae3080252d831794578a67b53968136834c0ebb1878d4f495a1f79a?d=identicon)[CodeChap](/maintainers/CodeChap)

---

Top Contributors

[![codeChap](https://avatars.githubusercontent.com/u/451621?v=4)](https://github.com/codeChap "codeChap (7 commits)")

### Embed Badge

![Health badge](/badges/codechap-context-trimmer/health.svg)

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

###  Alternatives

[cypresslab/gitelephant

An abstraction layer for git written in PHP

6131.2M31](/packages/cypresslab-gitelephant)[phmlabs/annovent

Simple lightweight event dispatcher using annotations for registering listeners

113.3k](/packages/phmlabs-annovent)

PHPackages © 2026

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