textanalysis-tool
Version:
A TypeScript module providing text analysis functionalities with various operations.
774 lines (605 loc) โข 27.1 kB
Markdown
# ๐ Text Analysis Tools ๐
[](https://www.npmjs.com/package/textanalysis-tool) [](https://opensource.org/licenses/MIT) [](https://www.typescriptlang.org/)
> ๐ A lightning-fast โก TypeScript library for ๐ค text analysis and ๐ง manipulation. Bundled with everything from simple cleanup to deep linguistic insights! ๐ฆ
---
## ๐๏ธ Table of Contents
- [๐ Text Analysis Tools ๐](#-text-analysis-tools-)
- [๐๏ธ Table of Contents](#๏ธ-table-of-contents)
- [โฌ๏ธ Installation](#๏ธ-installation)
- [๐งฉ Usage](#-usage)
- [๐ฐ Basic Usage](#-basic-usage)
- [โ๏ธ Working with Operations](#๏ธ-working-with-operations)
- [๐ง Advanced Analysis](#-advanced-analysis)
- [๐ญ Using Factory Methods](#-using-factory-methods)
- [๐ฆ Batch Processing](#-batch-processing)
- [๐ ๏ธ Available Operations](#๏ธ-available-operations)
- [๐๏ธ Text Removal](#๏ธ-text-removal)
- [๐ค Text Extraction](#-text-extraction)
- [๐ Text Transformation](#-text-transformation)
- [๐ข Text Counting](#-text-counting)
- [๐ฎ Advanced Analysis Options](#-advanced-analysis-options)
- [๐จ Custom Operations](#-custom-operations)
- [โ Adding Custom Operations](#-adding-custom-operations)
- [๐ With Metadata Extraction](#-with-metadata-extraction)
- [๐ Advanced Usage](#-advanced-usage)
- [๐ Toggling Operations](#-toggling-operations)
- [๐ Managing Multiple Operations](#-managing-multiple-operations)
- [๐ Resetting Text](#-resetting-text)
- [โ๏ธ Truncating Text](#๏ธ-truncating-text)
- [๐ API Reference](#-api-reference)
- [๐งฐ Tools.Analyser Class](#-toolsanalyser-class)
- [๐งฉ Tools.Operations Enum](#-toolsoperations-enum)
- [๐ Tools.ToolsConstant Class](#-toolstoolsconstant-class)
- [๐ Interface Types](#-interface-types)
- [๐ Extensions](#-extensions)
- [๐ SentimentAnalyzer](#-sentimentanalyzer)
- [๐ TextSummarizer](#-textsummarizer)
- [๐ TextStatistics](#-textstatistics)
- [๐ LanguageDetector](#-languagedetector)
- [๐ TextDiff](#-textdiff)
- [๐งฉ Interfaces \& Types](#-interfaces--types)
- [๐ SentimentResult](#-sentimentresult)
- [๐ท๏ธ SentimentClassification](#๏ธ-sentimentclassification)
- [๐ ReadabilityResult](#-readabilityresult)
- [๐ฃ๏ธ LanguageDetectionResult](#๏ธ-languagedetectionresult)
- [๐ TextDiffResult](#-textdiffresult)
- [๐ค Contributing](#-contributing)
- [๐ License](#-license)
---
## โฌ๏ธ Installation
> _Install in one click! โจ_
```bash
npm install textanalysis-tool
```
or via Yarn:
```bash
yarn add textanalysis-tool
```
---
## ๐งฉ Usage
### ๐ฐ Basic Usage
```typescript
import { Tools } from 'textanalysis-tool';
const text = "This is a sample text with 123 numbers and https://example.com URL!";
// Create analyzer with specific operations enabled
const analyser = new Tools.Analyser(text, {
[Tools.Operations.CountCharacters]: true,
[Tools.Operations.CountWords]: true,
[Tools.Operations.ExtractUrls]: true
});
// Run the analysis
analyser.main()
.then(result => {
console.log("Text analysis results:");
console.log(`- Character count: ${result.metadata.counts.characterCount}`);
console.log(`- Word count: ${result.metadata.counts.wordCount}`);
console.log(`- URLs found: ${result.metadata.urls?.join(', ')}`);
console.log(`- Operations performed: ${result.operations.join(', ')}`);
console.log(`- Execution time: ${result.executionTime}ms`);
})
.catch(error => {
console.error('Analysis failed:', error);
});
```
### โ๏ธ Working with Operations
```typescript
import { Tools } from 'textanalysis-tool';
const analyser = new Tools.Analyser("Hello, world! 123 #hashtag @mention", {
// Enable specific operations
[Tools.Operations.RemovePunctuations]: true,
[Tools.Operations.RemoveNumbers]: true,
[Tools.Operations.ConvertToUppercase]: true
});
analyser.main().then(result => {
console.log(result.output); // "HELLO WORLD HASHTAG MENTION"
});
```
### ๐ง Advanced Analysis
```typescript
import { Tools } from 'textanalysis-tool';
const paragraph = `This tool is amazing ๐ฏ. Sometimes you need exact control.`;
const analyser = new Tools.Analyser(paragraph, {
[Tools.Operations.AnalyzeSentiment]: { positive: ['amazing'], negative: ['need'] },
[Tools.Operations.SummarizeText]: { sentenceCount: 2 },
[Tools.Operations.CalculateReadability]: true,
[Tools.Operations.DetectLanguage]: true,
[Tools.Operations.CompareTexts]: { compareWith: 'Other example text.' }
});
analyser.main().then(result => {
console.log('โค๏ธ Sentiment:', result.sentiment);
console.log('โ๏ธ Summary:', result.summary);
console.log('๐ Readability:', result.readability);
console.log('๐ Language:', result.language);
console.log('๐ Text Diff:', result.diff);
});
```
### ๐ญ Using Factory Methods
```typescript
import { Tools } from 'textanalysis-tool';
// Create an analyzer with specific operations enabled
const analyser = Tools.Analyser.createWithEnabledOperations(
"Hello, world! 123",
['CountCharacters', 'CountWords', 'RemovePunctuations']
);
analyser.main().then(result => {
console.log(result.output); // "Hello world 123"
console.log(`Words: ${result.metadata.counts.wordCount}`);
console.log(`Characters: ${result.metadata.counts.characterCount}`);
});
```
### ๐ฆ Batch Processing
```typescript
import { Tools } from 'textanalysis-tool';
const texts = [
"First sample with https://example1.com",
"Second sample 12345 with #hashtags",
"Third sample with @mentions and emails@example.com"
];
const options = {
[Tools.Operations.CountWords]: true,
[Tools.Operations.ExtractUrls]: true,
[Tools.Operations.ExtractHashtags]: true,
[Tools.Operations.ExtractMentions]: true,
[Tools.Operations.ExtractEmails]: true
};
Tools.Analyser.batch(texts, options)
.then(results => {
results.forEach((result, index) => {
console.log(`\nAnalysis of text #${index + 1}:`);
console.log(`- Word count: ${result.metadata.counts.wordCount}`);
console.log(`- URLs: ${result.metadata.urls?.join(', ') || 'None'}`);
console.log(`- Hashtags: ${result.metadata.hashtags?.join(', ') || 'None'}`);
console.log(`- Mentions: ${result.metadata.mentions?.join(', ') || 'None'}`);
console.log(`- Emails: ${result.metadata.emails?.join(', ') || 'None'}`);
});
});
```
## ๐ ๏ธ Available Operations
### ๐๏ธ Text Removal
| Operation | Description | Example Input | Example Output |
| --------------------- | -------------------------- | ------------------- | ----------------- |
| `RemovePunctuations` | ๐งน Remove punctuation | "Hello, world!" | "Hello world" |
| `RemoveNumbers` | ๐ข Remove numbers | "abc123def" | "abcdef" |
| `RemoveAlphabets` | ๐ก Remove alphabets | "abc123def" | "123" |
| `RemoveSpecialChars` | โจ Remove special chars | "Hi @you #1!" | "Hi you 1" |
| `RemoveNewlines` | โฉ๏ธ Remove newlines | "Hello\nWorld" | "Hello World" |
| `RemoveExtraSpaces` | ๐ Trim extra spaces | " Hi there " | "Hi there" |
### ๐ค Text Extraction
| Operation | Description | Example Input | Example Output |
| ---------------------- | ------------------------ | ------------------------------------ | ------------------------- |
| `ExtractUrls` | ๐ Extract URLs | "Visit https://a.com and b.org" | ["https://a.com"] |
| `ExtractEmails` | โ๏ธ Extract emails | "Email me at user@test.com" | ["user@test.com"] |
| `ExtractPhoneNumbers` | ๐ Extract phone numbers | "Call 123-456-7890" | ["123-456-7890"] |
| `ExtractHashtags` | #๏ธโฃ Extract hashtags | "#fun #code" | ["#fun","#code"] |
| `ExtractMentions` | Extract mentions | "Hi @user!" | ["@user"] |
### ๐ Text Transformation
| Operation | Description | Example Input | Example Output |
| --------------------- | ----------------------- | ---------------------- | ---------------- |
| `ConvertToUppercase` | ๐ UPPERCASE conversion | "Hello World" | "HELLO WORLD" |
| `ConvertToLowercase` | ๐ก lowercase conversion | "Hello World" | "hello world" |
| `ConvertToTitleCase` | ๐ Title Case | "hello world" | "Hello World" |
| `ReverseText` | ๐ Reverse text | "abcde" | "edcba" |
| `Truncate` | โ๏ธ Truncate text | (maxLength=5) "abcdef"| "abcde..." |
### ๐ข Text Counting
| Operation | Description | Example Input | Example Output |
| ---------------------- | ------------------------ | ------------- | ------------------- |
| `CountCharacters` | ๐ Count non-space chars | "Hi!" | 3 |
| `CountAlphabets` | ๐ Count letters | "A1b2C" | 3 |
| `CountNumbers` | ๐ข Count digits | "A1b2C" | 2 |
| `CountAlphanumeric` | ๐ค Letters+digits count | "A1 b2!" | { alph:3, num:2 } |
| `CountWords` | ๐ Count words | "Hello world"| 2 |
| `CountSentences` | ๐ Count sentences | "Hi. Bye?" | 2 |
### ๐ฎ Advanced Analysis Options
| Operation | Description | Example Input | Example Output |
| ----------------------- | -------------------------------- | ------------------------------------ | ------------------------------- |
| `AnalyzeSentiment` | โค๏ธ Sentiment analysis | "I love this!" | { score:1, positive:["love"] }|
| `SummarizeText` | โ๏ธ Extractive summary | (3 sentences) long paragraph | (2 sentence summary) |
| `CalculateReadability` | ๐ Flesch-Kincaid score | "The quick brown fox jumps..." | { score:70, grade:5 } |
| `DetectLanguage` | ๐ Language detection | "Bonjour le monde" | "fr" |
| `CompareTexts` | ๐ Text diff | { text1, text2 } | { added:[...], removed:[...] } |
---
## ๐จ Custom Operations
_Easily plug in your own workflows! โจ_
### โ Adding Custom Operations
You can extend functionality by adding your own custom operations:
```typescript
import { Tools } from 'textanalysis-tool';
const analyser = new Tools.Analyser("Sample text for custom operation");
// Add a simple custom operation
await analyser.addCustomOperation(
"surroundWithAsterisks", // Command name
"Surround With Asterisks", // Log name
{
operation: (text) => `*${text}*`, // Operation function
isEnabled: true, // Enable immediately
metadata: { decorationType: "asterisks" } // Additional metadata
}
);
// Run the analysis with the custom operation
const result = await analyser.main();
console.log(result.output); // "*Sample text for custom operation*"
console.log(result.metadata.custom.surroundWithAsterisks); // { decorationType: "asterisks" }
```
### ๐ With Metadata Extraction
```typescript
import { Tools } from 'textanalysis-tool';
const analyser = new Tools.Analyser("The code is 12345 and the pin is 6789");
// Add a custom operation with metadata extraction
await analyser.addCustomOperation(
"extractNumericCodes",
"Extract Numeric Codes",
{
operation: (text) => text, // Operation doesn't change the text
isEnabled: true,
metadataExtractor: (text) => {
const allNumbers = text.match(/\d+/g) || [];
return {
codes: allNumbers,
codeCount: allNumbers.length
};
}
}
);
const result = await analyser.main();
console.log(result.metadata.extractNumericCodes);
// Output: { codes: ["12345", "6789"], codeCount: 2 }
```
---
## ๐ Advanced Usage
### ๐ Toggling Operations
Enable or disable operations dynamically:
```typescript
import { Tools } from 'textanalysis-tool';
const analyser = new Tools.Analyser("Sample text with 123 numbers");
// Enable specific operations
await analyser.toggleOperation(Tools.Operations.RemoveNumbers, true);
await analyser.toggleOperation(Tools.Operations.CountCharacters, true);
// Run analysis
let result = await analyser.main();
console.log(result.output); // "Sample text with numbers"
// Disable and enable different operations
await analyser.toggleOperation(Tools.Operations.RemoveNumbers, false);
await analyser.toggleOperation(Tools.Operations.ConvertToUppercase, true);
// Run analysis again with new settings
result = await analyser.main();
console.log(result.output); // "SAMPLE TEXT WITH 123 NUMBERS"
```
### ๐ Managing Multiple Operations
```typescript
import { Tools } from 'textanalysis-tool';
const analyser = new Tools.Analyser("Hello, world! 123");
// Enable all available operations
await analyser.enableAllOperations();
// Run with all operations
let result = await analyser.main();
console.log("With all operations:", result.output);
// Disable all operations
await analyser.disableAllOperations();
// Enable only specific operations
await analyser.toggleOperation(Tools.Operations.RemovePunctuations, true);
await analyser.toggleOperation(Tools.Operations.ConvertToUppercase, true);
// Run with only selected operations
result = await analyser.main();
console.log("With selected operations:", result.output); // "HELLO WORLD 123"
```
### ๐ Resetting Text
```typescript
import { Tools } from 'textanalysis-tool';
const analyser = new Tools.Analyser("Original text", {
[Tools.Operations.ConvertToUppercase]: true
});
// Run first analysis
let result = await analyser.main();
console.log(result.output); // "ORIGINAL TEXT"
// Reset with new text
await analyser.resetText("New content");
// Run analysis again
result = await analyser.main();
console.log(result.output); // "NEW CONTENT"
```
### โ๏ธ Truncating Text
```typescript
import { Tools } from 'textanalysis-tool';
const longText = "This is a very long text that needs to be truncated to a reasonable length.";
const analyser = new Tools.Analyser(longText, {
[Tools.Operations.Truncate]: {
maxLength: 20,
suffix: "..." // Optional, defaults to "..."
}
});
analyser.main().then(result => {
console.log(result.output); // "This is a very long..."
});
```
---
## ๐ API Reference
### ๐งฐ Tools.Analyser Class
The main class for text analysis operations.
**Constructor:**
```typescript
constructor(raw_text: string, options: AnalyserBuiltInOptions = {})
```
**Properties:**
| Property | Type | Description |
|----------|------|-------------|
| `raw_text` | string | The input text being analyzed |
| `count` | number | The character count |
| `alphacount` | number | The alphabetic character count |
| `numericcount` | number | The numeric character count |
| `wordCount` | number | The word count |
| `sentenceCount` | number | The sentence count |
| `urls` | string[] | Extracted URLs |
| `emails` | string[] | Extracted email addresses |
| `phoneNumbers` | string[] | Extracted phone numbers |
| `hashtags` | string[] | Extracted hashtags |
| `mentions` | string[] | Extracted mentions |
| `operations` | string[] | Log of operations performed |
| `availableOperations` | Record<string, string> | All available operations |
| `options` | AnalyserBuiltInOptions | Current operation options |
**Methods:**
| Method | Parameters | Return Type | Description |
|--------|------------|-------------|-------------|
| `main` | None | Promise<AnalyserResult> | Executes all enabled operations |
| `addCustomOperation` | commandName: string, logName: string, config: object | Promise<void> | Adds a custom operation |
| `toggleOperation` | commandName: string, isEnabled: boolean | Promise<void> | Enables/disables an operation |
| `enableAllOperations` | None | Promise<void> | Enables all operations |
| `disableAllOperations` | None | Promise<void> | Disables all operations |
| `resetText` | newText?: string | Promise<void> | Resets text to original or new value |
**Static Methods:**
| Method | Parameters | Return Type | Description |
|--------|------------|-------------|-------------|
| `createWithEnabledOperations` | text: string, operations: (keyof typeof Operations)[] | Analyser | Creates instance with specific operations |
| `batch` | texts: string[], options: AnalyserBuiltInOptions | Promise<AnalyserResult[]> | Processes multiple texts with same options |
### ๐งฉ Tools.Operations Enum
Enum of all built-in operations:
```typescript
export enum Operations {
RemovePunctuations = "removepunc",
RemoveNumbers = "removenum",
RemoveAlphabets = "removealpha",
RemoveSpecialChars = "removespecialchar",
RemoveNewlines = "newlineremover",
RemoveExtraSpaces = "extraspaceremover",
ExtractUrls = "extractUrls",
ExtractEmails = "extractEmails",
ExtractPhoneNumbers = "extractPhoneNumbers",
ExtractHashtags = "extractHashtags",
ExtractMentions = "extractMentions",
ConvertToUppercase = "fullcaps",
ConvertToLowercase = "lowercaps",
ConvertToTitleCase = "titlecase",
CountCharacters = "charcount",
CountAlphabets = "alphacount",
CountNumbers = "numcount",
CountAlphanumeric = "alphanumericcount",
CountWords = "wordcount",
CountSentences = "sentencecount",
ReverseText = "reversetext",
Truncate = "truncate",
AnalyzeSentiment = "analyzeSentiment",
SummarizeText = "summarizeText",
CalculateReadability = "calculateReadability",
DetectLanguage = "detectLanguage",
CompareTexts = "compareTexts",
}
```
### ๐ Tools.ToolsConstant Class
Contains regular expression patterns used throughout the library:
```typescript
export class ToolsConstant {
static readonly regex = {
alphabets: /[a-zA-Z]/g,
numbers: /\d/g,
punctuations: /[!"#$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~]/g,
specialCharacters: /[^a-zA-Z0-9\s!"#$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~]/g,
urls: /https?:\/\/\S+/gi,
newlines: /^\s*$(?:\r\n?|\n)/gm,
extraSpaces: / +/g,
character: /[^\s\p{Cf}]/gu,
email: /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g,
phoneNumber: /(?:\+\d{1,3}[-\s]?)?\(?\d{3}\)?[-\s]?\d{3}[-\s]?\d{4}/g,
hashtags: /#[a-zA-Z0-9_]+/g,
mentions: /@[a-zA-Z0-9_]+/g
};
}
```
### ๐ Interface Types
**AnalyserBuiltInOptions:**
```typescript
type AnalyserBuiltInOptions = Partial<Record<Operations | string, boolean | any>>;
```
**AnalyserResult:**
```typescript
interface AnalyserResult {
purpose: string;
output: string;
metadata: {
counts: {
characterCount: number;
alphabetCount: number;
numericCount: number;
wordCount?: number;
sentenceCount?: number;
};
urls?: string[];
emails?: string[];
phoneNumbers?: string[];
hashtags?: string[];
mentions?: string[];
custom?: {
[key: string]: any;
};
};
operations: string[];
builtInOperations: string[];
customOperations: string[];
executionTime?: number;
}
```
**AnalyserResult (Extended):**
```typescript
interface AnalyserExtendedResult extends AnalyserResult {
sentiment?: SentimentResult;
summary?: string;
readability?: ReadabilityResult;
languageDetection?: LanguageDetectionResult;
textComparison?: TextDiffResult;
}
```
**TruncateConfig:**
```typescript
interface TruncateConfig {
maxLength: number;
suffix?: string;
}
```
### ๐ Extensions
## ๐ SentimentAnalyzer
A utility class for analyzing text sentiment.
**Constructor:**
```typescript
constructor()
```
**Properties:**
| Property | Type | Description |
|----------------|----------------|--------------------------------------|
| `positiveWords`| `Set<string>` | The set of positive sentiment words |
| `negativeWords`| `Set<string>` | The set of negative sentiment words |
**Methods:**
| Method | Parameters | Return Type | Description |
|--------------------|---------------------------------------------------------|-------------------|--------------------------------------------------|
| `analyze` | `text: string` | `SentimentResult` | Executes sentiment analysis on the input text |
| `addCustomLexicon` | `lexicon: { positive?: string[]; negative?: string[] }` | `void` | Adds custom positive/negative words to the lexicons |
**Static Methods:**
_None_
---
## ๐ TextSummarizer
A utility class for generating extractive summaries from text.
**Constructor:**
```typescript
constructor()
```
**Properties:**
| Property | Type | Description |
|-------------|----------------|--------------------------------------------------|
| `stopWords` | `Set<string>` | The set of stop words to ignore when scoring sentences |
**Methods:**
| Method | Parameters | Return Type | Description |
|----------------------|------------------------------------------|-------------|--------------------------------------------------------|
| `extractiveSummarize` | `text: string, sentenceCount?: number` | `string` | Generates an extractive summary with the top sentences |
| `addStopWords` | `words: string[]` | `void` | Adds custom words to the stopโword list |
**Static Methods:**
_None_
---
## ๐ TextStatistics
A utility class for computing readability metrics such as the FleschโKincaid score.
**Constructor:**
```typescript
// No constructor parameters; all methods are stateless
constructor()
```
**Properties:**
_None_
**Methods:**
| Method | Parameters | Return Type | Description |
|----------------------------|----------------|---------------------|-----------------------------------------------|
| `fleschKincaidReadability` | `text: string` | `ReadabilityResult` | Calculates readability and grade level scores |
**Static Methods:**
_None_
---
## ๐ LanguageDetector
A trigramโbased language detection utility supporting English, Spanish, French, and German.
**Constructor:**
```typescript
constructor()
```
**Properties:**
| Property | Type | Description |
|--------------------|------------------------------------|--------------------------------------------------|
| `languageProfiles` | `Map<string, Map<string, number>>` | Mapping of language names to frequency profiles |
**Methods:**
| Method | Parameters | Return Type | Description |
|----------------------|----------------------------------------------|---------------------------|------------------------------------------------|
| `detect` | `text: string` | `LanguageDetectionResult` | Detects the most likely language for the text |
| `addCustomLanguage` | `language: string, profile: Record<string, number>` | `void` | Registers a new language profile |
**Static Methods:**
_None_
---
## ๐ TextDiff
A utility class for comparing two texts and computing similarity metrics.
**Constructor:**
```typescript
// No constructor parameters; all methods are invoked directly
constructor()
```
**Properties:**
_None_
**Methods:**
| Method | Parameters | Return Type | Description |
|-----------|--------------------------------|------------------|----------------------------------------------------------|
| `compare` | `text1: string, text2: string` | `TextDiffResult` | Computes similarity, edit distance, and common substrings |
**Static Methods:**
_None_
---
## ๐งฉ Interfaces & Types
### ๐ SentimentResult
```typescript
interface SentimentResult {
score: number;
positiveWordCount: number;
negativeWordCount: number;
totalWords: number;
classification: SentimentClassification;
}
```
### ๐ท๏ธ SentimentClassification
```typescript
type SentimentClassification = "positive" | "negative" | "neutral";
```
### ๐ ReadabilityResult
```typescript
interface ReadabilityResult {
readabilityScore: number;
gradeLevel: number;
wordCount: number;
sentenceCount: number;
syllableCount: number;
avgWordsPerSentence: number;
avgSyllablesPerWord: number;
complexity: string;
}
```
### ๐ฃ๏ธ LanguageDetectionResult
```typescript
interface LanguageDetectionResult {
detectedLanguage: string;
confidence: number;
scores: Record<string, number>;
}
```
### ๐ TextDiffResult
```typescript
interface TextDiffResult {
similarity: number;
editDistance: number;
commonSubstrings: Array<{ substring: string; length: number }>;
wordDifference: {
added: string[];
removed: string[];
unchanged: string[];
addedCount: number;
removedCount: number;
unchangedCount: number;
};
}
```
---
## ๐ค Contributing
Love it? Spread the word! ๐
1. Fork the repo ๐ด
2. Create a branch `git checkout -b feature/amazing-feature` ๐ฟ
3. Commit your changes `git commit -m 'Add some amazing feature'` โจ
4. Push and create a PR `git push origin feature/amazing-feature` ๐
---
## ๐ License
This project is licensed under the MIT License - see the [LICENSE](https://github.com/abindent/textanalyser/blob/master/LICENSE) file for details.# ๐ Text Analysis Tools ๐