Skip to content

Latest commit

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Sentiment API

A Rust-based API for Vercel Serverless Functions that provides sentiment analysis based on weighted post data.

Endpoints

1. GET /api/topics

Returns all available topics for a given category.

Query Parameters:

  • category (required): The category to filter topics by

Example:

GET /api/topics?category=politics

Response:

[
{
"id": 1,
"name": "2024 Election",
"category": "politics"
},
{
"id": 2,
"name": "Climate Policy",
"category": "politics"
}
]

2. GET /api/posts

Returns top posts for a topic, sorted by weight * relevancy.

Query Parameters:

  • topic_id (required): The ID of the topic
  • limit (optional): Maximum number of posts to return (default: 10)

Example:

GET /api/posts?topic_id=1&limit=5

Response:

[
{
"id": 1,
"topic_id": 1,
"content": "Post content here...",
"weight": 0.9,
"relevancy": 0.85,
"score": 0.765
}
]

3. GET /api/sentiment

Returns normalized sentiment probabilities for all outcomes of a topic.

The sentiment is calculated by:

  1. For each post and outcome: weight * relevancy * probability
  2. Sum all scores for each outcome
  3. Normalize probabilities so they sum to 1.0

Query Parameters:

  • topic_id (required): The ID of the topic

Example:

GET /api/sentiment?topic_id=1

Response:

{
"topic_id": 1,
"topic_name": "2024 Election",
"sentiments": [
{
"outcome_id": 1,
"outcome_name": "Candidate A Wins",
"probability": 0.55
},
{
"outcome_id": 2,
"outcome_name": "Candidate B Wins",
"probability": 0.45
}
]
}

Environment Variables

Create a .env file with the following variables:

DATABASE_URL=postgres://user:password@host:port/database

Database Schema

The API expects the following PostgreSQL tables:

CREATETABLEtopics (
id BIGSERIALPRIMARY KEY,
name VARCHAR(255) NOT NULL,
category VARCHAR(255) NOT NULL
);
CREATETABLEposts (
id BIGSERIALPRIMARY KEY,
topic_id BIGINTNOT NULLREFERENCES topics(id),
content TEXTNOT NULL,
weight DOUBLE PRECISIONNOT NULL,
relevancy DOUBLE PRECISIONNOT NULL
);
CREATETABLEoutcomes (
id BIGSERIALPRIMARY KEY,
topic_id BIGINTNOT NULLREFERENCES topics(id),
name VARCHAR(255) NOT NULL
);
CREATETABLEpost_outcome_probabilities (
id BIGSERIALPRIMARY KEY,
post_id BIGINTNOT NULLREFERENCES posts(id),
outcome_id BIGINTNOT NULLREFERENCES outcomes(id),
probability DOUBLE PRECISIONNOT NULL
);
CREATEINDEXidx_topics_categoryON topics(category);
CREATEINDEXidx_posts_topic_idON posts(topic_id);
CREATEINDEXidx_outcomes_topic_idON outcomes(topic_id);
CREATEINDEXidx_post_outcome_probabilities_post_idON post_outcome_probabilities(post_id);

Development

Prerequisites

  • Rust (latest stable)
  • PostgreSQL database

Local Development

  1. Install dependencies:

    cargo build
  2. Set up your .env file with database credentials

  3. Run locally with Vercel CLI:

    vercel dev

Deployment

Deploy to Vercel:

vercel

Make sure to set the DATABASE_URL environment variable in your Vercel project settings.

Project Structure

sentiment-api/
├── api/
│ ├── topics.rs # Topics endpoint handler
│ ├── posts.rs # Posts endpoint handler
│ └── sentiment.rs # Sentiment endpoint handler
├── src/
│ └── lib.rs # Shared database models and queries
├── Cargo.toml # Rust dependencies
├── vercel.json # Vercel configuration
└── README.md

About

Serverless API for weighted sentiment analysis using topic-based post scoring and probability normalization

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages