sentiment-analysis-textblob
Version:
A Node.js library for sentiment analysis using TextBlob
163 lines (118 loc) โข 5.39 kB
Markdown
# Sentiment Analysis TextBlob ๐จ
[](https://www.npmjs.com/package/sentiment-analysis-textblob)
[](https://opensource.org/licenses/MIT)
[](https://nodejs.org/)
[](https://www.npmjs.com/package/sentiment-analysis-textblob)
<div style="border: 2px solid #4CAF50; padding: 15px; background: #f5f5f5; border-radius: 8px;">
<h3 style="color: #4CAF50;">Overview</h3>
A powerful Node.js library for sentiment analysis using [TextBlob](https://textblob.readthedocs.io/). Detect ๐ positive, ๐ neutral, or ๐ negative sentiments across **Windows**, **macOS**, and **Linux**. Ideal for reviews and feedback!
</div>
## ๐ About the Author
<div style="background: #e8f5e9; padding: 10px; border-left: 4px solid #4CAF50;">
<strong>Nguyแป
n ฤแปฉc Trแปng</strong>
๐ง [trong0196@gmail.com](mailto:trong0196@gmail.com)
Passionate about NLP and JavaScript, creating tools for text analysis.
</div>
## ๐ 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
```bash
npm install sentiment-analysis-textblob
```
<div style="background: #e3f2fd; padding: 10px; border-left: 4px solid #2196F3;">
Auto-installs TextBlob with Python detection (`python` or `python3`). Manual setup required if it fails.
</div>
### Requirements
- **Node.js**: v14.2+
- **Python**: v3.6+, in PATH
- **TextBlob**: NLP library
**Verify Python**:
```bash
python3 --version || python --version
```
**Manual Install**:
```bash
pip3 install textblob || pip install textblob
```
## ๐ฏ Quick Start
### Promise Example
```javascript
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
```javascript
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) <br> ๐ **0**: Neutral (e.g., 'A product' ~ 0) <br> ๐ **< 0**: Negative (e.g., 'Bad!' ~ -0.6) |
| `subjectivity` | 0 to 1 | ๐ **0โ0.3**: Objective (e.g., '100 pages' ~ 0) <br> ๐ค **0.3โ0.7**: Mixed (e.g., 'Okay' ~ 0.5) <br> ๐ญ **0.7โ1**: Subjective (e.g., 'Love it!' ~ 0.9) |
**Example**:
```javascript
// 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](https://www.python.org/downloads/) and add to PATH.
- Check: `python3 --version` or `python --version`.
- macOS/Linux: `export PATH="/usr/local/bin:$PATH"` in `~/.bashrc`.
- **TextBlob Failed**:
- Run: `pip3 install textblob` or `pip install textblob`.
- Test: `python3 scripts/sentiment.py 'Test text'` (Expected: `{"polarity": 0.0, "subjectivity": 0.0}`).
- **Custom Path** (Rare):
- Edit `src/index.js`:
```javascript
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]
};
```
## ๐ค Contribute
๐ก Ideas or bugs?
๐ [GitHub](https://github.com/trong0196/sentiment-analysis-textblob)
๐ง [trong0196@gmail.com](mailto:trong0196@gmail.com)
## ๐ License
[MIT](https://opensource.org/licenses/MIT)