multilingual-sentiment-analysis
v1.0.0
Published
A high-performance multilingual sentiment analysis module for Node.js powered by Hugging Face Transformers.
Maintainers
Readme
Multilingual Sentiment Analysis
A high-performance multilingual sentiment analysis module for Node.js, leveraging state-of-the-art transformer models (via Hugging Face Transformers) to provide accurate sentiment predictions across multiple languages.
Features
- Multilingual Support: Works out-of-the-box with English, Spanish, French, German, Italian, Portuguese, and more.
- Transformer-based: Uses the
distilbert-base-multilingual-cased-sentiments-studentmodel for efficient and accurate inference. - Simple API: Provides an easy-to-use async interface for analyzing text.
- Reliability Scoring: Includes a confidence score and a reliability flag for each prediction.
- Lightweight Inference: Built with
@huggingface/transformersfor optimized execution in Node.js environments.
Installation
npm installUsage
Basic Example
import { getAnalyzer } from 'multilingual-sentiment-analysis';
async function main() {
const analyzer = getAnalyzer();
// The first call might take a moment to load the model
const result = await analyzer.analyze('This product is absolutely wonderful!');
console.log(result);
// { sentiment: 'positive', confidence: 0.99, isReliable: true }
}
main();Multilingual Support
const esResult = await analyzer.analyze('¡Este producto es excelente!');
// { sentiment: 'positive', confidence: 0.98, isReliable: true }
const frResult = await analyzer.analyze('C\'est terrible, je déteste ça.');
// { sentiment: 'negative', confidence: 0.97, isReliable: true }Advanced Usage (Custom Models)
You can instantiate the SentimentAnalyzer with a custom model from the Hugging Face Hub:
import { SentimentAnalyzer } from 'multilingual-sentiment-analysis';
const customAnalyzer = new SentimentAnalyzer('Xenova/bert-base-multilingual-uncased-sentiment');
await customAnalyzer.waitForReady();
const result = await customAnalyzer.analyze('I love this!');API Reference
getAnalyzer(): SentimentAnalyzer
Returns a singleton instance of the default sentiment analyzer.
SentimentAnalyzer Class
constructor(modelName?: string): Create a new analyzer with an optional custom model.analyze(text: string): Promise<SentimentResult>: Analyzes the sentiment of the input text.waitForReady(): Promise<void>: Ensures the underlying transformer model is loaded and ready.
SentimentResult Object
sentiment: 'positive' | 'negative' | 'neutral'confidence: number(0.0 to 1.0)isReliable: boolean(True if confidence > 0.6)
Development Scripts
npm run build: Compile TypeScript to JavaScript.npm run test: Run the test suite using Mocha.npm run evaluate: Run an evaluation script with a battery of test cases across different languages.npm run evaluate -- -i: Run evaluation in interactive mode for custom testing.npm run lint: Run ESLint to ensure code quality.
License
ISC
