liveBook · Manning

Lemmatization

As explained in Starting Data Analytics with Generative AI and Python

Lemmatization is the process of converting inflected words into their canonical dictionary forms using linguistic analysis.

A keyword search can miss a customer’s opinion when a review uses an inflected word but the search term or sentiment dictionary uses its base form. Lemmatization converts words to base forms using linguistic knowledge, making those forms easier to match. Unlike stemming, which reduces words by stripping inflections or suffixes, it relies on language analysis. section 5.5 section 5.6.5

This matters especially for a dictionary-based sentiment search: a dictionary may store nouns and verbs as lemmas, while reviews contain other forms. If the reviews are searched without lemmatization, that mismatch can contribute to missed positive reviews and low sensitivity. Applying the same kind of preprocessing to the reviews can bring them into closer alignment with the dictionary. section 5.6.5

A Portuguese-review workflow

Suppose a keyword search over Portuguese customer reviews misses expected matches. Ask an AI assistant whether preprocessing could help, then check its suggestions rather than applying every step. Lowercasing, removing punctuation, and trimming extra whitespace may be useful; removing stopwords may be irrelevant to a simple keyword search. To try lemmatization, load spaCy’s Portuguese model, pt_core_news_sm, process each review, and collect each token’s lemma_ into a cleaned review column. Search that column, then inspect sample matches and misses against the original reviews. section 5.5.5 section 5.6.5

Do not assume the change improves results merely because it is linguistically motivated. The demonstrated cleaning workflow uses stemming, while lemmatization is offered as an alternative and as a fix for a lemma-based dictionary. Test the chosen workflow on the dataset: compare search results, examine false positives and false negatives, and revisit the preprocessing if useful reviews still go unmatched. section 5.5.5 section 5.6.5

import spacy
nlp = spacy.load("pt_core_news_sm")
def lemmatize_text(text):
    doc = nlp(text)
    lemmatized_words = [token.lemma_ for token in doc]
    return ' '.join(lemmatized_words)
def clean_text(text):
    if not isinstance(text, str):
        return ""
    text = text.lower()
    text = remove_punctuation(text)
    text = remove_extra_whitespace(text)
    text = lemmatize_text(text)
    return text
df['cleaned_review_text'] = df['review_text'].apply(clean_text)

This alternative cleaning snippet shows where spaCy lemmatization fits before searching a cleaned review column; it is distinct from the stemming-based workflow. section 5.5.5

How Starting Data Analytics with Generative AI and Python covers it

How deep it goes
Defines the technique and sketches spaCy preprocessing, but does not detail its linguistic internals.
Examples
Uses Portuguese customer reviews, a lemma-based sentiment dictionary, and a spaCy Portuguese-model preprocessing snippet.
What it assumes
Introduces lemmatization in plain terms while assuming a review-search workflow and, for code, a Python DataFrame.
What you can do after
Apply it via spaCy to preprocess reviews for keyword or dictionary searches, then check whether missed matches decrease.
How it connects
Connects lemmatization to stemming, keyword search, text preprocessing, sentiment analysis, and sensitivity.

Also explained in

Compare how 3 books explain lemmatization →

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.