Analyze Sentiment¶
Analyze text sentiment (reviews, comments, feedback, etc.) using AI-powered semantic understanding.
Sentiment analysis is multilingual, works across all supported languages.
Sentiments
Returns sentiment with one of the following values:
- Positive - Text expresses predominantly positive sentiment
- Negative - Text expresses predominantly negative sentiment
- Neutral - Text expresses a balanced or ambiguous sentiment
Analyze Sentiment vs. Classify
This endpoint (Analyze Sentiment) performs sentiment analysis using AI-powered semantic understanding.
If you have 1,000 or more examples (per sentiment) specific to your use case, you can train a custom machine learning (ML) model, and then use the Classify endpoint to perform sentiment analysis. This will provide even better results as it uses AI-powered pattern recognition plus semantic understanding.
Endpoint¶
API Request¶
-
Required Parameters
API request must contain either a document_id array or a text array.
document_id array of strings
Array specifying one or more document IDs.
Info
-
Example (single document ID):
-
Example (multiple document IDs):
-
For each document ID string:
MinLength: 1
MaxLength: 128
MinLength: 1
MaxLength: 10
text array of strings
Array specifying one or more text strings.
Info
-
Example (single text value):
-
Example (multiple text values):
"text": [ "I love my original Listerine. This came properly packaged so it didn鈥檛 leak. What more can I say. Kills germs by the millions. Great for use with my waterpick and I feel so clean.", "Muy mala calidad, no lo recomiendo. Compr茅 estos zapatos esperando algo de buena calidad, pero me llev茅 una gran decepci贸n. Despu茅s de solo una semana de uso, la suela comenz贸 a despegarse y el material se ve desgastado." ]
-
For each text string:
MinLength: 1
MaxLength: 1000
MinLength: 1
MaxLength: 10
-
-
POST /v20241104/analyze-sentiment
curl -X POST "https://api.gainly.ai/v20241104/analyze-sentiment" \ -H "Content-Type: application/json" \ -H "X-API-Key: YOUR_API_KEY_HERE" \ # (1)! -d '{ "text": [ "I love my original Listerine. This came properly packaged so it didn\'t leak. What more can I say. Kills germs by the millions. Great for use with my waterpick and I feel so clean.", "Muy mala calidad, no lo recomiendo. Compr茅 estos zapatos esperando algo de buena calidad, pero me llev茅 una gran decepci贸n. Despu茅s de solo una semana de uso, la suela comenz贸 a despegarse y el material se ve desgastado." ] }'
- Replace
YOUR_API_KEY_HERE
with the value of your API key.
# Prompt for AI coding assistants/IDEs (e.g., ChatGPT, Claude, GitHub Copilot, Cursor, Windsurf) Using the Gainly API: 1. Write code to call the analyze_sentiment operation (see OpenAPI spec: https://api.gainly.ai/v20241104/openapi.json) 2. Implement authentication using the header "X-API-Key" as described in the docs: https://docs.gainly.ai/latest/api-reference/authentication/ 3. Implement rate limit handling as described in the docs: https://docs.gainly.ai/latest/api-reference/rate-limits/ 4. Implement error handling 5. Handle the response according to the AnalyzeSentimentResults schema in the OpenAPI spec
using System.Net.Http; using System.Text.Json; using System.Text; var client = new HttpClient(); var url = "https://api.gainly.ai/v20241104/analyze-sentiment"; var payload = new { text = new[] { "I love my original Listerine. This came properly packaged so it didn't leak. What more can I say. Kills germs by the millions. Great for use with my waterpick and I feel so clean.", "Muy mala calidad, no lo recomiendo. Compr茅 estos zapatos esperando algo de buena calidad, pero me llev茅 una gran decepci贸n. Despu茅s de solo una semana de uso, la suela comenz贸 a despegarse y el material se ve desgastado." } }; var content = new StringContent( JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json" ); client.DefaultRequestHeaders.Add("X-API-Key", "YOUR_API_KEY_HERE"); // (1)! var response = await client.PostAsync(url, content); var result = await response.Content.ReadAsStringAsync(); Console.WriteLine(result);
- Replace
YOUR_API_KEY_HERE
with the value of your API key.
package main import ( "bytes" "encoding/json" "fmt" "net/http" ) func main() { url := "https://api.gainly.ai/v20241104/analyze-sentiment" payload := map[string]interface{}{ "text": []string{ "I love my original Listerine. This came properly packaged so it didn't leak. What more can I say. Kills germs by the millions. Great for use with my waterpick and I feel so clean.", "Muy mala calidad, no lo recomiendo. Compr茅 estos zapatos esperando algo de buena calidad, pero me llev茅 una gran decepci贸n. Despu茅s de solo una semana de uso, la suela comenz贸 a despegarse y el material se ve desgastado.", }, } jsonData, _ := json.Marshal(payload) req, _ := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-API-Key", "YOUR_API_KEY_HERE") // (1)! resp, _ := http.DefaultClient.Do(req) defer resp.Body.Close() var result map[string]interface{} json.NewDecoder(resp.Body).Decode(&result) fmt.Println(result) }
- Replace
YOUR_API_KEY_HERE
with the value of your API key.
import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.net.URI; var client = HttpClient.newHttpClient(); var url = "https://api.gainly.ai/v20241104/analyze-sentiment"; var payload = """ { "text": [ "I love my original Listerine. This came properly packaged so it didn't leak. What more can I say. Kills germs by the millions. Great for use with my waterpick and I feel so clean.", "Muy mala calidad, no lo recomiendo. Compr茅 estos zapatos esperando algo de buena calidad, pero me llev茅 una gran decepci贸n. Despu茅s de solo una semana de uso, la suela comenz贸 a despegarse y el material se ve desgastado." ] } """; var request = HttpRequest.newBuilder() .uri(URI.create(url)) .header("Content-Type", "application/json") .header("X-API-Key", "YOUR_API_KEY_HERE") // (1)! .POST(HttpRequest.BodyPublishers.ofString(payload)) .build(); var response = client.send(request, HttpResponse.BodyHandlers.ofString()); System.out.println(response.body());
- Replace
YOUR_API_KEY_HERE
with the value of your API key.
const axios = require('axios'); // or: import axios from 'axios'; const url = 'https://api.gainly.ai/v20241104/analyze-sentiment'; const payload = { text: [ 'I love my original Listerine. This came properly packaged so it didn\'t leak. What more can I say. Kills germs by the millions. Great for use with my waterpick and I feel so clean.', 'Muy mala calidad, no lo recomiendo. Compr茅 estos zapatos esperando algo de buena calidad, pero me llev茅 una gran decepci贸n. Despu茅s de solo una semana de uso, la suela comenz贸 a despegarse y el material se ve desgastado.' ] }; const headers = { 'Content-Type': 'application/json', 'X-API-Key': 'YOUR_API_KEY_HERE' // (1)! }; axios.post(url, payload, { headers }) .then(response => console.log(response.data)) .catch(error => console.error('Error:', error.message));
- Replace
YOUR_API_KEY_HERE
with the value of your API key.
<?php $client = new \GuzzleHttp\Client(); $url = 'https://api.gainly.ai/v20241104/analyze-sentiment'; $payload = [ 'text' => [ 'I love my original Listerine. This came properly packaged so it didn\'t leak. What more can I say. Kills germs by the millions. Great for use with my waterpick and I feel so clean.', 'Muy mala calidad, no lo recomiendo. Compr茅 estos zapatos esperando algo de buena calidad, pero me llev茅 una gran decepci贸n. Despu茅s de solo una semana de uso, la suela comenz贸 a despegarse y el material se ve desgastado.' ] ]; $response = $client->request('POST', $url, [ 'json' => $payload, 'headers' => [ 'Content-Type' => 'application/json', 'X-API-Key' => 'YOUR_API_KEY_HERE' # (1)! ], ]); echo $response->getBody();
- Replace
YOUR_API_KEY_HERE
with the value of your API key.
import requests url = "https://api.gainly.ai/v20241104/analyze-sentiment" payload = { "text": [ "I love my original Listerine. This came properly packaged so it didn't leak. What more can I say. Kills germs by the millions. Great for use with my waterpick and I feel so clean.", "Muy mala calidad, no lo recomiendo. Compr茅 estos zapatos esperando algo de buena calidad, pero me llev茅 una gran decepci贸n. Despu茅s de solo una semana de uso, la suela comenz贸 a despegarse y el material se ve desgastado." ] } headers = { "Content-Type": "application/json", "X-API-Key": "YOUR_API_KEY_HERE" # (1)! } response = requests.post(url, json=payload, headers=headers) data = response.json() print(data)
- Replace
YOUR_API_KEY_HERE
with the value of your API key.
require 'json' require 'uri' require 'net/http' require 'openssl' url = URI('https://api.gainly.ai/v20241104/analyze-sentiment') http = Net::HTTP.new(url.host, url.port) http.use_ssl = true request = Net::HTTP::Post.new(url) request['Content-Type'] = 'application/json' request['X-API-Key'] = 'YOUR_API_KEY_HERE' # (1)! request.body = { text: [ 'I love my original Listerine. This came properly packaged so it didn\'t leak. What more can I say. Kills germs by the millions. Great for use with my waterpick and I feel so clean.', 'Muy mala calidad, no lo recomiendo. Compr茅 estos zapatos esperando algo de buena calidad, pero me llev茅 una gran decepci贸n. Despu茅s de solo una semana de uso, la suela comenz贸 a despegarse y el material se ve desgastado.' ] }.to_json response = http.request(request) puts response.read_body
- Replace
YOUR_API_KEY_HERE
with the value of your API key.
- Replace
API Response¶
{
"object": "analyze_sentiment_result",
"url": "/v20241104/analyze-sentiment",
"data": [
{
"sentiment": "positive",
"confidence_level": "high",
"text": "I love my original Listerine. This came properly packaged so it didn鈥檛 leak. What more can I say. Kills germs by the millions. Great for use with my waterpick and I feel so clean.",
"document_id": null
},
{
"sentiment": "negative",
"confidence_level": "very_high",
"text": "Muy mala calidad, no lo recomiendo. Compr茅 estos zapatos esperando algo de buena calidad, pero me llev茅 una gran decepci贸n. Despu茅s de solo una semana de uso, la suela comenz贸 a despegarse y el material se ve desgastado.",
"document_id": null
}
],
"text": [
"I love my original Listerine. This came properly packaged so it didn鈥檛 leak. What more can I say. Kills germs by the millions. Great for use with my waterpick and I feel so clean.",
"Muy mala calidad, no lo recomiendo. Compr茅 estos zapatos esperando algo de buena calidad, pero me llev茅 una gran decepci贸n. Despu茅s de solo una semana de uso, la suela comenz贸 a despegarse y el material se ve desgastado."
],
"document_id": null,
"token_usage": {
"semantic_tokens": 179,
"llm_tokens": {
"llm_output_tokens": 0,
"llm_input_tokens": 0,
"model": null
}
},
"livemode": true
}
Sentiment¶
sentiment
indicates Gainly's assessment of the emotional tone of the text.
It will have one of the following values:
positive
negative
neutral
Confidence Level¶
confidence_level
represents Gainly's assessment of the degree of confidence in the sentiment analysis result.
It will have one of the following values:
very_high
high
medium
low