# Sentiment & Theme Tagger (`swholmes/sentiment-theme-tagger`) Actor

Turn any pile of reviews, comments, or posts into decisions. Tags each item with sentiment, extracts themes/keywords, and scores sentiment per aspect (price, service, quality...). Lexicon-based out of the box, with optional LLM enrichment. Feed it a list or another Actor's dataset.

- **URL**: https://apify.com/swholmes/sentiment-theme-tagger.md
- **Developed by:** [Scott Holmes](https://apify.com/swholmes) (community)
- **Categories:** AI, Automation, Developer tools
- **Stats:** 1 total users, 0 monthly users, 90.0% runs succeeded, 0 bookmarks
- **User rating**: No ratings yet

## Pricing

from $2.00 / 1,000 item analyzeds

This Actor is paid per event. You are not charged for the Apify platform usage, but only a fixed price for specific events.

Learn more: https://docs.apify.com/platform/actors/running/actors-in-store#pay-per-event

## What's an Apify Actor?

Actors are web data automations that power AI and operations. They run on the Apify platform to scrape websites, process data, connect APIs, and automate workflows.
In Batch mode, an Actor accepts a well-defined JSON input, performs an action which can take anything from a few seconds to a few hours,
and optionally produces a well-defined JSON output, datasets with results, or files in key-value store.
In Standby mode, an Actor provides a web server which can be used as a website, API, or an MCP server.
Actors are written with capital "A".

## How to integrate an Actor?

If asked about integration, you help developers integrate Actors into their projects.
You adapt to their stack and deliver integrations that are safe, well-documented, and production-ready.
The best way to integrate Actors is as follows.

In JavaScript/TypeScript projects, use official [JavaScript/TypeScript client](https://docs.apify.com/api/client/js/docs.md):

```bash
npm install apify-client
```

In Python projects, use official [Python client library](https://docs.apify.com/api/client/python/docs.md):

```bash
pip install apify-client
```

In shell scripts, use [Apify CLI](https://docs.apify.com/cli/docs.md):

````bash
# MacOS / Linux
curl -fsSL https://apify.com/install-cli.sh | bash
# Windows
irm https://apify.com/install-cli.ps1 | iex
```bash

In AI frameworks, you might use the [Apify MCP server](https://docs.apify.com/integrations/mcp.md).

If your project is in a different language, use the [REST API](https://docs.apify.com/api/v2.md).

For usage examples, see the [API](#api) section below.

For more details, see Apify documentation as [Markdown index](https://docs.apify.com/llms.txt) and [Markdown full-text](https://docs.apify.com/llms-full.txt).


# README

## Sentiment & Theme Tagger

Turn any pile of reviews, comments, or posts into decisions. For each item it returns a **sentiment** (positive / negative / neutral + score), the top **themes/keywords**, and optional **per-aspect sentiment** (how people feel about price vs service vs quality, separately). The run also rolls up an overall sentiment split and the top themes across everything.

**Transformation Actor — it doesn't scrape.** Feed it a text list or another Actor's `datasetId`. No anti-bot maintenance, near-zero run cost, and it plugs on top of every review, comment, and social scraper on the platform (TripAdvisor Reviews, Reddit, Facebook/TikTok/YouTube comments, Trustpilot…).

### Input

| Field | Type | Notes |
|---|---|---|
| `texts` | array | Text items to analyze. |
| `inputDatasetId` | string | Pull text from another Actor's dataset instead. |
| `textField` | string | Field holding the text (default `text`). |
| `aspects` | array | Score sentiment separately per topic, e.g. `["price","service","quality"]`. |
| `openaiApiKey` | string (secret) | Optional. Enables LLM sentiment + themes + one-line summary. |
| `model` | string | LLM model when a key is set (default `gpt-4o-mini`). |
| `concurrency` | integer | Parallel items. |

### Output (per item)

```json
{
  "text": "The food was amazing but the service was painfully slow and overpriced.",
  "sentiment": "neutral",
  "sentimentScore": 0,
  "themes": ["food", "food amazing", "service"],
  "aspects": {
    "price": { "sentiment": "negative", "score": -0.667 },
    "service": { "sentiment": "negative", "score": -0.667 }
  },
  "summary": null,
  "method": "lexicon"
}
````

(A mixed review nets to `neutral` overall — the value is in the per-aspect split: price and service both flagged negative. Aspects with no mention, like `quality` here, are simply omitted. LLM mode produces a cleaner overall score plus a one-line summary.)

The run's `OUTPUT` holds the rollup: totals, positive %, and the top 15 themes across all items.

### Two modes

- **Lexicon (default, no key, no cost):** built-in AFINN-style sentiment with negation + intensifier handling, keyword/bigram theme extraction, and aspect scoring by sentence. Deterministic and cheap.
- **LLM (optional):** supply an OpenAI key for higher-accuracy sentiment, cleaner themes, and a one-line summary per item. Falls back to the lexicon automatically on any API error.

### Monetization (pay-per-event)

Charges one `item-analyzed` event per item. Because it consumes other Actors' datasets, its customers are the users of every review/comment scraper — position it as the "so what does it all mean?" layer they already need.

### Extending

The lexicon lives in `src/lexicon.js` — add domain words (e.g. product or hospitality vocabulary) to sharpen scores. For production-grade accuracy on nuanced text, run in LLM mode; the code is structured so you could swap in any provider by editing `analyzeLlm()`.

# Actor input Schema

## `texts` (type: `array`):

Plain text items to analyze (reviews, comments, posts). Ignored if inputDatasetId is set.

## `inputDatasetId` (type: `string`):

Pull text from another Actor's dataset (e.g. a reviews or comments run) instead of the list above.

## `textField` (type: `string`):

Which field in the input dataset holds the text.

## `aspects` (type: `array`):

Score sentiment separately for each of these topics where mentioned, e.g. \["price", "service", "quality", "shipping"].

## `openaiApiKey` (type: `string`):

If set, uses an LLM for richer sentiment, themes, and a one-line summary per item. Without it, a built-in lexicon is used (no key, no cost).

## `model` (type: `string`):

Model to use when an OpenAI key is provided.

## `concurrency` (type: `integer`):

How many items to process in parallel. Only affects runs using an LLM; lower this if you hit OpenAI rate limits.

## Actor input object example

```json
{
  "texts": [
    "The food was amazing but the service was painfully slow and overpriced.",
    "Absolutely love it, best purchase this year!"
  ],
  "textField": "text",
  "aspects": [
    "price",
    "service",
    "quality"
  ],
  "model": "gpt-4o-mini",
  "concurrency": 10
}
```

# API

You can run this Actor programmatically using our API. Below are code examples in JavaScript, Python, and CLI, as well as the OpenAPI specification and MCP server setup.

## JavaScript example

```javascript
import { ApifyClient } from 'apify-client';

// Initialize the ApifyClient with your Apify API token
// Replace the '<YOUR_API_TOKEN>' with your token
const client = new ApifyClient({
    token: '<YOUR_API_TOKEN>',
});

// Prepare Actor input
const input = {
    "texts": [
        "The food was amazing but the service was painfully slow and overpriced.",
        "Absolutely love it, best purchase this year!"
    ],
    "aspects": [
        "price",
        "service",
        "quality"
    ]
};

// Run the Actor and wait for it to finish
const run = await client.actor("swholmes/sentiment-theme-tagger").call(input);

// Fetch and print Actor results from the run's dataset (if any)
console.log('Results from dataset');
console.log(`💾 Check your data here: https://console.apify.com/storage/datasets/${run.defaultDatasetId}`);
const { items } = await client.dataset(run.defaultDatasetId).listItems();
items.forEach((item) => {
    console.dir(item);
});

// 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/js/docs

```

## Python example

```python
from apify_client import ApifyClient

# Initialize the ApifyClient with your Apify API token
# Replace '<YOUR_API_TOKEN>' with your token.
client = ApifyClient("<YOUR_API_TOKEN>")

# Prepare the Actor input
run_input = {
    "texts": [
        "The food was amazing but the service was painfully slow and overpriced.",
        "Absolutely love it, best purchase this year!",
    ],
    "aspects": [
        "price",
        "service",
        "quality",
    ],
}

# Run the Actor and wait for it to finish
run = client.actor("swholmes/sentiment-theme-tagger").call(run_input=run_input)

# Fetch and print Actor results from the run's dataset (if there are any)
print("💾 Check your data here: https://console.apify.com/storage/datasets/" + run["defaultDatasetId"])
for item in client.dataset(run["defaultDatasetId"]).iterate_items():
    print(item)

# 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/python/docs/quick-start

```

## CLI example

```bash
echo '{
  "texts": [
    "The food was amazing but the service was painfully slow and overpriced.",
    "Absolutely love it, best purchase this year!"
  ],
  "aspects": [
    "price",
    "service",
    "quality"
  ]
}' |
apify call swholmes/sentiment-theme-tagger --silent --output-dataset

```

## MCP server setup

```json
{
    "mcpServers": {
        "apify": {
            "command": "npx",
            "args": [
                "mcp-remote",
                "https://mcp.apify.com/?tools=swholmes/sentiment-theme-tagger",
                "--header",
                "Authorization: Bearer <YOUR_API_TOKEN>"
            ]
        }
    }
}

```

## OpenAPI specification

```json
{
    "openapi": "3.0.1",
    "info": {
        "title": "Sentiment & Theme Tagger",
        "description": "Turn any pile of reviews, comments, or posts into decisions. Tags each item with sentiment, extracts themes/keywords, and scores sentiment per aspect (price, service, quality...). Lexicon-based out of the box, with optional LLM enrichment. Feed it a list or another Actor's dataset.",
        "version": "0.1",
        "x-build-id": "iqPkSZAMyg5hsgvc1"
    },
    "servers": [
        {
            "url": "https://api.apify.com/v2"
        }
    ],
    "paths": {
        "/acts/swholmes~sentiment-theme-tagger/run-sync-get-dataset-items": {
            "post": {
                "operationId": "run-sync-get-dataset-items-swholmes-sentiment-theme-tagger",
                "x-openai-isConsequential": false,
                "summary": "Executes an Actor, waits for its completion, and returns Actor's dataset items in response.",
                "tags": [
                    "Run Actor"
                ],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/inputSchema"
                            }
                        }
                    }
                },
                "parameters": [
                    {
                        "name": "token",
                        "in": "query",
                        "required": true,
                        "schema": {
                            "type": "string"
                        },
                        "description": "Enter your Apify token here"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK"
                    }
                }
            }
        },
        "/acts/swholmes~sentiment-theme-tagger/runs": {
            "post": {
                "operationId": "runs-sync-swholmes-sentiment-theme-tagger",
                "x-openai-isConsequential": false,
                "summary": "Executes an Actor and returns information about the initiated run in response.",
                "tags": [
                    "Run Actor"
                ],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/inputSchema"
                            }
                        }
                    }
                },
                "parameters": [
                    {
                        "name": "token",
                        "in": "query",
                        "required": true,
                        "schema": {
                            "type": "string"
                        },
                        "description": "Enter your Apify token here"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/runsResponseSchema"
                                }
                            }
                        }
                    }
                }
            }
        },
        "/acts/swholmes~sentiment-theme-tagger/run-sync": {
            "post": {
                "operationId": "run-sync-swholmes-sentiment-theme-tagger",
                "x-openai-isConsequential": false,
                "summary": "Executes an Actor, waits for completion, and returns the OUTPUT from Key-value store in response.",
                "tags": [
                    "Run Actor"
                ],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/inputSchema"
                            }
                        }
                    }
                },
                "parameters": [
                    {
                        "name": "token",
                        "in": "query",
                        "required": true,
                        "schema": {
                            "type": "string"
                        },
                        "description": "Enter your Apify token here"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK"
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "inputSchema": {
                "type": "object",
                "properties": {
                    "texts": {
                        "title": "Texts",
                        "type": "array",
                        "description": "Plain text items to analyze (reviews, comments, posts). Ignored if inputDatasetId is set."
                    },
                    "inputDatasetId": {
                        "title": "Input dataset ID (optional)",
                        "type": "string",
                        "description": "Pull text from another Actor's dataset (e.g. a reviews or comments run) instead of the list above."
                    },
                    "textField": {
                        "title": "Text field name",
                        "type": "string",
                        "description": "Which field in the input dataset holds the text.",
                        "default": "text"
                    },
                    "aspects": {
                        "title": "Aspects to score (optional)",
                        "type": "array",
                        "description": "Score sentiment separately for each of these topics where mentioned, e.g. [\"price\", \"service\", \"quality\", \"shipping\"].",
                        "items": {
                            "type": "string"
                        }
                    },
                    "openaiApiKey": {
                        "title": "OpenAI API key (optional)",
                        "type": "string",
                        "description": "If set, uses an LLM for richer sentiment, themes, and a one-line summary per item. Without it, a built-in lexicon is used (no key, no cost)."
                    },
                    "model": {
                        "title": "LLM model",
                        "type": "string",
                        "description": "Model to use when an OpenAI key is provided.",
                        "default": "gpt-4o-mini"
                    },
                    "concurrency": {
                        "title": "Concurrency",
                        "minimum": 1,
                        "maximum": 30,
                        "type": "integer",
                        "description": "How many items to process in parallel. Only affects runs using an LLM; lower this if you hit OpenAI rate limits.",
                        "default": 10
                    }
                }
            },
            "runsResponseSchema": {
                "type": "object",
                "properties": {
                    "data": {
                        "type": "object",
                        "properties": {
                            "id": {
                                "type": "string"
                            },
                            "actId": {
                                "type": "string"
                            },
                            "userId": {
                                "type": "string"
                            },
                            "startedAt": {
                                "type": "string",
                                "format": "date-time",
                                "example": "2025-01-08T00:00:00.000Z"
                            },
                            "finishedAt": {
                                "type": "string",
                                "format": "date-time",
                                "example": "2025-01-08T00:00:00.000Z"
                            },
                            "status": {
                                "type": "string",
                                "example": "READY"
                            },
                            "meta": {
                                "type": "object",
                                "properties": {
                                    "origin": {
                                        "type": "string",
                                        "example": "API"
                                    },
                                    "userAgent": {
                                        "type": "string"
                                    }
                                }
                            },
                            "stats": {
                                "type": "object",
                                "properties": {
                                    "inputBodyLen": {
                                        "type": "integer",
                                        "example": 2000
                                    },
                                    "rebootCount": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "restartCount": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "resurrectCount": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "computeUnits": {
                                        "type": "integer",
                                        "example": 0
                                    }
                                }
                            },
                            "options": {
                                "type": "object",
                                "properties": {
                                    "build": {
                                        "type": "string",
                                        "example": "latest"
                                    },
                                    "timeoutSecs": {
                                        "type": "integer",
                                        "example": 300
                                    },
                                    "memoryMbytes": {
                                        "type": "integer",
                                        "example": 1024
                                    },
                                    "diskMbytes": {
                                        "type": "integer",
                                        "example": 2048
                                    }
                                }
                            },
                            "buildId": {
                                "type": "string"
                            },
                            "defaultKeyValueStoreId": {
                                "type": "string"
                            },
                            "defaultDatasetId": {
                                "type": "string"
                            },
                            "defaultRequestQueueId": {
                                "type": "string"
                            },
                            "buildNumber": {
                                "type": "string",
                                "example": "1.0.0"
                            },
                            "containerUrl": {
                                "type": "string"
                            },
                            "usage": {
                                "type": "object",
                                "properties": {
                                    "ACTOR_COMPUTE_UNITS": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "DATASET_READS": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "DATASET_WRITES": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "KEY_VALUE_STORE_READS": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "KEY_VALUE_STORE_WRITES": {
                                        "type": "integer",
                                        "example": 1
                                    },
                                    "KEY_VALUE_STORE_LISTS": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "REQUEST_QUEUE_READS": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "REQUEST_QUEUE_WRITES": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "DATA_TRANSFER_INTERNAL_GBYTES": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "DATA_TRANSFER_EXTERNAL_GBYTES": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "PROXY_RESIDENTIAL_TRANSFER_GBYTES": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "PROXY_SERPS": {
                                        "type": "integer",
                                        "example": 0
                                    }
                                }
                            },
                            "usageTotalUsd": {
                                "type": "number",
                                "example": 0.00005
                            },
                            "usageUsd": {
                                "type": "object",
                                "properties": {
                                    "ACTOR_COMPUTE_UNITS": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "DATASET_READS": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "DATASET_WRITES": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "KEY_VALUE_STORE_READS": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "KEY_VALUE_STORE_WRITES": {
                                        "type": "number",
                                        "example": 0.00005
                                    },
                                    "KEY_VALUE_STORE_LISTS": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "REQUEST_QUEUE_READS": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "REQUEST_QUEUE_WRITES": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "DATA_TRANSFER_INTERNAL_GBYTES": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "DATA_TRANSFER_EXTERNAL_GBYTES": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "PROXY_RESIDENTIAL_TRANSFER_GBYTES": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "PROXY_SERPS": {
                                        "type": "integer",
                                        "example": 0
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
```
