Skip to content

joaquimafn/Arbitrallis-API

Repository files navigation

Arbitrallis API | This is a challenge by Coodesh

Text analysis API with AI integration.

Setup

  1. Clone the repository
  2. Install dependencies:
    npm install
    
  3. Create a .env file in the root directory with the following variables:
    PORT=3000
    OPENAI_API_KEY=your_api_key_here  # Optional, for OpenAI integration
    AI_PROVIDER=mock                   # Options: openai, huggingface, mock
    HUGGINGFACE_API_KEY=your_api_key_here  # Optional, for Hugging Face integration
    

Running the API

Development mode (with auto-reload):

npm run dev

Production mode:

npm start

API Documentation

Interactive API documentation is available at:

http://localhost:3000/api-docs

This Swagger UI provides detailed information about all endpoints, request/response schemas, and allows you to test the API directly from the browser.

Running Tests

Run all tests:

npm test

Run tests with watch mode:

npm run test:watch

Generate test coverage report:

npm run test:coverage

API Endpoints

Health Check

GET /health

Returns the status of the API.

Analyze Text

POST /analyze-text

Request body:

{
  "text": "Your text to analyze here"
}

Response:

{
  "success": true,
  "wordCount": 5,
  "topWords": [
    { "word": "text", "count": 1 },
    { "word": "analyze", "count": 1 },
    { "word": "here", "count": 1 }
  ],
  "sentimentSummary": {
    "sentiment": "positive",
    "explanation": "The text contains positive language indicating satisfaction."
  },
  "text": "Your text to analyze here"
}

Features:

  • Word Count: Total number of words in the text
  • Top Words: The 5 most frequent words in the text, excluding common stopwords in Portuguese and English
  • Sentiment Analysis: Analysis of the text sentiment (positive, negative, or neutral) with explanation

Search Term

GET /search-term?term=keyword

Searches for a term in the last analyzed text.

Response:

{
  "found": true,
  "occurrences": 2,
  "term": "keyword"
}

If no text has been analyzed yet:

{
  "found": false,
  "occurrences": 0,
  "message": "No text has been analyzed yet"
}

AI Integration

The API supports multiple AI providers for sentiment analysis:

  • Mock (default): Uses a simple algorithm based on positive/negative word counting
  • OpenAI: Uses OpenAI's GPT models for more accurate sentiment analysis
  • Hugging Face: Uses Hugging Face's sentiment analysis models

To configure the AI provider, set the AI_PROVIDER environment variable to one of: mock, openai, or huggingface.

History Management

The API maintains a history of the last 10 text analyses in memory. The search endpoint uses this history to search for terms in the most recently analyzed text.

Project Structure

arbitrallis-api/
├── src/
│   ├── config/       # Configuration files
│   ├── services/     # Service layer
│   ├── utils/        # Utility functions
│   ├── app.js        # Express application
│   └── server.js     # Server entry point
├── tests/            # Test files
├── .env              # Environment variables
├── package.json      # Dependencies and scripts
└── README.md         # Documentation

Testing with cURL

Health check:

curl http://localhost:3000/health

Analyze text:

curl -X POST http://localhost:3000/analyze-text \
  -H "Content-Type: application/json" \
  -d '{"text": "This is a sample text to analyze. This text contains repeated words to demonstrate the frequency analysis feature of the API."}'

Search for a term:

curl "http://localhost:3000/search-term?term=sample"

About

No description or website provided.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published