liveBook · Manning

Sentiment analysis

As explained in Starting Data Analytics with Generative AI and Python

Sentiment analysis is the computational analysis of text to determine expressed opinions, emotions, or evaluative polarity.

A company with hundreds of customer reviews needs more than a count of complaints: it needs to know which experiences were positive, negative, or somewhere between. Sentiment analysis assigns those labels to text so teams can spot dissatisfaction, assess customer satisfaction, and decide what to investigate. It is a form of text classification, not a substitute for reading the reviews behind a finding. section 6.1 section 6.2

For Portuguese product reviews, start with a method that fits the data and the decision:

  • Search for positive words as a quick baseline. A review containing “bom” may still complain that the delivered product does not match its advertisement, while praise without a listed keyword may be missed. section 5.5.6 section 5.6.1
  • Use a sentiment lexicon when a fixed list is too thin. Check its actual file format, encoding, polarity notation, and word matching before trusting generated Python code: an example that assumes a CSV will not directly load the illustrated SentiLex text file. section 5.6.4
  • Ask a generative AI to classify reviews, or test pretrained classifiers. For Portuguese, compare language-specific, translated-input, multilingual, and zero-shot approaches rather than assuming that a convenient English-language tool transfers cleanly. section 5.5.6 section 6.2.9

A review that resists a simple label

“Good product, but what came to me does not match the photo in the ad” contains praise and a complaint. In a small direct test, generative AIs described this review as mixed or neutral. Inspect cases like this before reducing every answer to one of three labels. section 6.2.2

To test an API workflow, send each of 500 non-empty reviews with a prompt asking for its sentiment, then store the returned label. The demonstrated Python code detects labels by searching the response for “positive,” “neutral,” or “negative”; other responses become “unknown.” That shortcut can mishandle nuanced answers, so inspect both the raw responses and the assigned labels. API access also requires a key and may incur charges. section 6.2.3

Table 6.3 Quality of sentiment analysis with different pretrained models
Sentiment analysis ( n=500)
Number of reviews classified as positive
Sensitivity
Specificity
Reference review score
320 (63%)
true positives
1
1
Basic keyword search
163 (33%)
0.45
0.9
ChatGPT 4 API
250 (50%)
0.74
0.93
Suboptimal model
193 (39%)
0.56
0.93
Translated inputs
295 (59%)
0.86
0.89
Multilingual model
265 (53%)
0.79
0.93
Zero-shot learning model
325 (65%)
0.87
0.74

Compare sensitivity and specificity across the tested methods, not just how many reviews each calls positive. The 500-review comparison uses review scores as its reference. section 6.2.9

In that comparison, keyword search found fewer positive reviews than the tested API and several pretrained approaches. No method wins on every measure: the zero-shot model, for example, had higher sensitivity but lower specificity than the multilingual model. Test promising options on a manageable sample, check disagreements against the original reviews, and choose for the errors and costs that matter to the decision. section 6.2.9

How Starting Data Analytics with Generative AI and Python covers it

How deep it goes
Works through keyword, lexicon, API, and pretrained-model approaches, then compares sensitivity and specificity without detailing model internals.
Examples
Uses Portuguese product reviews, a mixed review, Python API calls, and a 500-review method comparison.
What it assumes
Defines sentiment classification from the ground up while assuming access to customer reviews and, for workflows, basic Python data handling.
What you can do after
Apply it via Python and an API or pretrained classifier, then evaluate labels against review scores and inspect questionable cases.
How it connects
Connects text classification with sentiment lexicons, natural language processing, emotion detection, and pretrained language models.

Also explained in

Compare how 12 books explain sentiment analysis →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Starting Data Analytics with Generative AI and Python. The section links point to the parts of the book it draws on.