Text analysis API with AI integration.
- Clone the repository
- Install dependencies:
npm install - Create a
.envfile 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
Development mode (with auto-reload):
npm run dev
Production mode:
npm start
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.
Run all tests:
npm test
Run tests with watch mode:
npm run test:watch
Generate test coverage report:
npm run test:coverage
GET /health
Returns the status of the API.
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"
}- 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
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"
}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.
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.
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
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"