A library for trimming tokens in encoding and decoding in LLM (Large Language Model) applications.
It provides utilities to compress text by removing stopwords, punctuation, spaces, and applying stemming, with the ability to reverse the process as much as possible.
- Token trimming: Remove stopwords, punctuation, and spaces from text.
- Stemming: Supports Porter and Lancaster stemmers via natural.
- Negation preservation: Keeps negation words even when removing stopwords.
- Customizable: Options for language, stemming, and what to remove.
- Decoder: Attempts to reconstruct trimmed text.
npm install llmtrim
import { Encoder } from 'llmtrim';
const encoder = new Encoder();
const text = "The quick brown fox jumps over the lazy dog.";
const trimmed = encoder.trim(text, {
removeSpaces: true,
removeStopwords: true,
removePunctuation: true,
stemmer: 'porter'
});
console.log(trimmed); // Output: quickbrownfoxjumpsoverlazydog
import { Decoder } from 'llmtrim';
const decoder = new Decoder();
const original = decoder.detrim(trimmed, {
removeSpaces: true,
removeStopwords: true,
removePunctuation: true,
stemmer: 'porter'
});
console.log(original);
// Output: The quick brown fox jumps over the lazy dog.
Option | Type | Description |
---|---|---|
stemmer |
'porter' | 'lancaster' |
Selects the stemming algorithm to use. |
language |
'english' |
Sets the language for stopword removal (currently only English is supported). |
removeSpaces |
boolean |
If true , removes all spaces from the text. |
removeStopwords |
boolean |
If true , removes common stopwords (e.g., "the", "and") from the text. |
removePunctuation |
boolean |
If true , removes punctuation characters from the text. |
Run the tests using:
npm test
Contributions are welcome! Please open an issue or submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.