sentiment-analysis-textblob
v1.0.3
Published
A Node.js library for sentiment analysis using TextBlob
Maintainers
Readme
Sentiment Analysis TextBlob 🎨
🌟 About the Author
🚀 Features
- Simple API: Supports callbacks and Promises.
- Sentiment Scores: Polarity (-1 to 1) & Subjectivity (0 to 1).
- Cross-Platform: Works on all major OSes.
- Practical Use: Perfect for social media or customer insights.
📦 Installation
npm install sentiment-analysis-textblobRequirements
- Node.js: v14.2+
- Python: v3.6+, in PATH
- TextBlob: NLP library
Verify Python:
python3 --version || python --versionManual Install:
pip3 install textblob || pip install textblob🎯 Quick Start
Promise Example
const { analyzeSentimentPromise } = require('sentiment-analysis-textblob');
(async () => {
try {
const result = await analyzeSentimentPromise('This is awesome!');
console.log('Result:', result); // { polarity: 0.7, subjectivity: 0.85 }
} catch (err) {
console.error('Error:', err.message);
}
})();Callback Example
const { analyzeSentiment } = require('sentiment-analysis-textblob');
analyzeSentiment('Great experience!', (err, result) => {
if (err) console.error('Error:', err.message);
else console.log('Result:', result); // { polarity: 0.6, subjectivity: 0.8 }
});📋 API
| Method | Description | Returns |
|-------------------------|------------------------------------------|------------------|
| analyzeSentiment(text, callback) | Analyze text via callback | void |
| analyzeSentimentPromise(text) | Analyze text via Promise | Promise<Object>|
- text:
string- Text input. - callback:
function(err, result)- Returns{ polarity, subjectivity }or error. - Promise: Resolves to
{ polarity, subjectivity }.
📊 Output Details
| Property | Range | Meaning |
|----------------|-------------|----------------------------------------------|
| polarity | -1 to 1 | 😊 > 0: Positive (e.g., 'Great!' ~ 0.7) 😐 0: Neutral (e.g., 'A product' ~ 0) 😞 < 0: Negative (e.g., 'Bad!' ~ -0.6) |
| subjectivity | 0 to 1 | 📝 0–0.3: Objective (e.g., '100 pages' ~ 0) 🤔 0.3–0.7: Mixed (e.g., 'Okay' ~ 0.5) 💭 0.7–1: Subjective (e.g., 'Love it!' ~ 0.9) |
Example:
// Input: 'Fantastic performance!'
// Output: { polarity: 0.8, subjectivity: 0.75 }
// Meaning: Strong positive, highly opinionatedℹ️ Notes
- Language: Optimized for English; non-English needs preprocessing.
- Dependencies: Requires Python + TextBlob (auto-installed).
- Performance: Suitable for small projects; use VADER for advanced cases.
🛠️ Troubleshooting
Python Not Found:
- Install from python.org and add to PATH.
- Check:
python3 --versionorpython --version. - macOS/Linux:
export PATH="/usr/local/bin:$PATH"in~/.bashrc.
TextBlob Failed:
- Run:
pip3 install textbloborpip install textblob. - Test:
python3 scripts/sentiment.py 'Test text'(Expected:{"polarity": 0.0, "subjectivity": 0.0}).
- Run:
Custom Path (Rare):
- Edit
src/index.js:const options = { mode: 'text', pythonPath: '/usr/bin/python3', // e.g., 'C:\\Python39\\python.exe' on Windows pythonOptions: ['-u'], scriptPath: path.join(__dirname, '../scripts'), args: [text] };
- Edit
🤝 Contribute
💡 Ideas or bugs?
👉 GitHub
📧 [email protected]
