API‐Documentation - SubhaNAG2001/sentiment-analysis GitHub Wiki

API Documentation

The Sentiment Analysis Web Application provides a RESTful API that allows you to analyze text sentiment programmatically.

Base URL

When running locally, the base URL is:

http://localhost:5000

Authentication

Currently, the API does not require authentication. Rate limiting is applied based on the client's IP address.

Endpoints

Analyze Sentiment

Analyzes the sentiment of the provided text.

URL: /api/sentiment

Method: POST

Content Type: application/json

Request Body:

Field Type Description
text string The text to analyze (required)

Example Request:

{
  "text": "This product exceeded my expectations. Highly recommended!"
}

Response:

Field Type Description
text string The original text that was analyzed
sentiment string The predicted sentiment (Positive, Negative, or Neutral)
confidence string The model's confidence in the prediction (as a percentage)

Example Response:

{
  "text": "This product exceeded my expectations. Highly recommended!",
  "sentiment": "Positive",
  "confidence": "92.45%"
}

Status Codes:

Status Code Description
200 Success
400 Bad request (e.g., missing text field)
500 Server error

Error Handling

The API returns error messages in JSON format:

{
  "error": "Error message description"
}

Rate Limiting

The API is rate-limited to prevent abuse. The default limit is 100 requests per minute per IP address. This can be configured in the config.py file.

Code Examples

cURL

curl -X POST http://localhost:5000/api/sentiment \
  -H "Content-Type: application/json" \
  -d '{"text":"This is a great product, I love it!"}'

Python

import requests
import json

url = "http://localhost:5000/api/sentiment"
data = {"text": "This is a great product, I love it!"}
headers = {"Content-Type": "application/json"}

response = requests.post(url, data=json.dumps(data), headers=headers)
result = response.json()
print(result)

JavaScript

fetch('http://localhost:5000/api/sentiment', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    text: 'This is a great product, I love it!'
  })
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));