npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@rathakrishna/sentiment-analysis

v1.0.1

Published

Reusable sentiment analysis library with Gemini & OpenAI support

Readme

📖 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

npm i @rathakrishna/sentiment-analysis

🔑 Setup

You’ll need an API key from either:

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

# For Gemini
GEMINI_API_KEY=your_gemini_api_key

# For OpenAI
OPENAI_API_KEY=your_openai_api_key

📦 Usage

Import

import { GeminiSentimentAnalyzer, OpenAISentimentAnalyzer } from "@rathakrishna/sentiment-analyzer";

1. Gemini Example

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

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)
await analyzer.analyzeReviews("Battery lasts all day but camera is bad.");
  • Multiple reviews (string[])
await analyzer.analyzeReviews([
  "Battery lasts all day!",
  "Camera is terrible at night.",
  "Support was helpful."
]);

📤 Output Format

The library always returns:

{
  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:

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

Output:

{
  "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
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