npm package: @rathakrishna/sentiment-analysis
Description: Reusable sentiment analysis library with Gemini & OpenAI support
Author: Rathakrishnan
Latest version: 1.0.1

# 📖 Sentiment Analyzer

A **reusable sentiment analysis library** for Node.js / TypeScript that works with **Gemini** (Google Generative Language API) and **OpenAI** (Chat Completions API).  

It accepts **single or multiple reviews** and returns:  
- Overall **sentiment** (positive / negative / neutral)  
- **Aspects** detected in the text (e.g., *battery*, *camera*, *support*)  
- **Aspect-based sentiment** (per aspect, whether positive/negative/neutral)  
- **Aggregated summary** across multiple reviews  

---

## 🚀 Installation

```bash
npm i @rathakrishna/sentiment-analysis
```

---

## 🔑 Setup

You’ll need an API key from either:  
- **Gemini (Google Generative Language API)** → [Get a key](https://makersuite.google.com/app/apikey)  
- **OpenAI** → [Get a key](https://platform.openai.com/api-keys)  

Save it in your environment (`.env` file or process env vars):

```env
# For Gemini
GEMINI_API_KEY=your_gemini_api_key

# For OpenAI
OPENAI_API_KEY=your_openai_api_key
```

---

## 📦 Usage

### Import
```ts
import { GeminiSentimentAnalyzer, OpenAISentimentAnalyzer } from "@rathakrishna/sentiment-analyzer";
```

---

### 1. Gemini Example
```ts
const analyzer = new GeminiSentimentAnalyzer(process.env.GEMINI_API_KEY!);

const result = await analyzer.analyzeReviews([
  "The battery life is amazing, but the camera is disappointing.",
  "Support was fast and helpful."
]);

console.log(JSON.stringify(result, null, 2));
```

---

### 2. OpenAI Example
```ts
const analyzer = new OpenAISentimentAnalyzer(process.env.OPENAI_API_KEY!);

const result = await analyzer.analyzeReviews("The screen is bright but the speakers are weak.");

console.log(JSON.stringify(result, null, 2));
```

---

## 📥 Input Format

You can pass either:

- **Single review (string)**  
```ts
await analyzer.analyzeReviews("Battery lasts all day but camera is bad.");
```

- **Multiple reviews (string[])**  
```ts
await analyzer.analyzeReviews([
  "Battery lasts all day!",
  "Camera is terrible at night.",
  "Support was helpful."
]);
```

---

## 📤 Output Format

The library always returns:

```ts
{
  reviews: [
    {
      text: string;                 // Original review
      sentiment: "positive" | "negative" | "neutral";
      aspects: string[];             // Detected aspects
      aspect_sentiments: Array<{
        aspect: string;
        sentiment: "positive" | "negative" | "neutral";
      }>;
    },
    ...
  ],
  aggregated: {
    overall_sentiment: "positive" | "negative" | "neutral";
    aspect_summary: {
      [aspect: string]: {
        positive: number;
        negative: number;
        neutral: number;
      }
    }
  }
}
```

---

### ✅ Example Response

Input:
```json
{
  "reviews": [
    "Battery lasts all day!",
    "Camera is terrible at night.",
    "Support was helpful."
  ]
}
```

Output:
```json
{
  "reviews": [
    {
      "text": "Battery lasts all day!",
      "sentiment": "positive",
      "aspects": ["battery"],
      "aspect_sentiments": [
        { "aspect": "battery", "sentiment": "positive" }
      ]
    },
    {
      "text": "Camera is terrible at night.",
      "sentiment": "negative",
      "aspects": ["camera", "low light"],
      "aspect_sentiments": [
        { "aspect": "camera", "sentiment": "negative" },
        { "aspect": "low light", "sentiment": "negative" }
      ]
    },
    {
      "text": "Support was helpful.",
      "sentiment": "positive",
      "aspects": ["support"],
      "aspect_sentiments": [
        { "aspect": "support", "sentiment": "positive" }
      ]
    }
  ],
  "aggregated": {
    "overall_sentiment": "positive",
    "aspect_summary": {
      "battery": { "positive": 1, "negative": 0, "neutral": 0 },
      "camera": { "positive": 0, "negative": 1, "neutral": 0 },
      "low light": { "positive": 0, "negative": 1, "neutral": 0 },
      "support": { "positive": 1, "negative": 0, "neutral": 0 }
    }
  }
}
```

---

## 📚 API

### `analyzeReviews(reviews: string | string[]): Promise<AnalysisResult>`

- **Input**: Single string or array of review strings  
- **Output**: `AnalysisResult`

```ts
interface AspectSentiment {
  aspect: string;
  sentiment: "positive" | "negative" | "neutral";
}

interface ReviewAnalysis {
  text: string;
  sentiment: "positive" | "negative" | "neutral";
  aspects: string[];
  aspect_sentiments: AspectSentiment[];
}

interface AggregatedAnalysis {
  overall_sentiment: "positive" | "negative" | "neutral";
  aspect_summary: Record<string, { positive: number; negative: number; neutral: number }>;
}

interface AnalysisResult {
  reviews: ReviewAnalysis[];
  aggregated: AggregatedAnalysis;
}
```

---

## ⚡ Features
- Works with **both Gemini & OpenAI**  
- Supports **single or multiple reviews**  
- Returns **overall + aspect-based sentiment**  
- TypeScript-ready with **.d.ts typings**  
- Can be used in **Node.js backends**, serverless functions, or APIs  

---

## 🛡 License
MIT © 2025 Rathakrishnan