liveBook · Manning

Lexical search

As explained in Starting Data Analytics with Generative AI and Python

A text-mining technique that locates and extracts information from text collections by matching specified words or phrases.

What if customers rarely mention a feature your team considers a major selling point? Frequency analysis may miss the question you actually need answered. Lexical search—also called keyword search—locates text containing specified words or phrases so you can pull out the relevant records and inspect what customers say. section 5.5

Think of a keyword list as a set of labels for sorting a crowded inbox. Searching reviews for “durable,” “enduring,” and “lasting” can gather comments about product quality. Repeating the search can help track mentions over time; it does not, by itself, establish what every customer thinks. section 5.5.1

For a first pass through customer reviews:

  • Choose the question and matching terms. A named feature calls for its name; positive-feedback extraction calls for words associated with praise. For reviews in another language, choose terms in that language. section 5.5 section 5.5.6
  • Ask an AI assistant for Python that checks a review column against the list, marks matches, and filters the matching rows. Specify the column name and language in the prompt. section 5.5.6
  • Run and inspect the code and extracted reviews. Ask follow-up questions about missing reviews and cleaning rather than assuming the first answer handles them. section 5.5.6 section 5.0

Positive reviews in Portuguese

A practical prompt is: “I have a dataframe with a review_comment_message column containing Portuguese customer reviews. Propose Python code to extract positive reviews using a keyword search.” One generated approach lists Portuguese praise words, applies a matching function to each review, creates a True/False column, and saves the filtered rows. Its basic substring check needs further attention to missing values and text cleaning. section 5.5.6

Improve matches by cleaning text before searching. The demonstrated steps include lowercasing, removing punctuation and extra whitespace, and stemming; lemmatization is another option. Apply the same processing consistently when checking terms. These choices can improve relevance, but the result still depends on the words selected. section 5.5 section 5.5.5

Listing 5.8 Data cleaning before keyword search
import string
import nltk
from nltk.stem import RSLPStemmer

nltk.download('rslp')
stemmer = RSLPStemmer()


def remove_punctuation(text):
    return text.translate(str.maketrans("", "", string.punctuation))

def remove_extra_whitespace(text):
        return ' '.join(text.split())

def stem_words(text):
        words = text.split()
    stemmed_words = [stemmer.stem(word) for word in words]
    return ' '.join(stemmed_words)

def clean_text(text):
        if not isinstance(text, str):
                 return ""
    text = text.lower()
    text = remove_punctuation(text)
    text = remove_extra_whitespace(text)
    text = stem_words(text)
    return text
df['cleaned_review_text'] = df['review_text'].apply(clean_text)

Inspect the cleaning function as a starting point, including its check for non-string values, before adapting it to a review column. section 5.5.5

Validate positive-review labels against a reference, such as review scores. Sensitivity measures how many reference-positive reviews the search finds; specificity measures how many reference-negative reviews it leaves out. In one 500-review comparison, basic keyword search reached 0.45 sensitivity and 0.90 specificity: it missed many positives despite rejecting most negatives. Treat a match as a lead for inspection, not a verdict on sentiment. section 5.5.5 section 6.2.9

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 the keyword baseline with other sentiment-analysis approaches using both sensitivity and specificity, rather than the number of positive labels alone. section 6.2.9

How Starting Data Analytics with Generative AI and Python covers it

How deep it goes
Works through generated filtering code, cleaning choices, and classification checks, while showing that default AI code may omit needed handling.
Examples
Uses Portuguese customer-review filtering and a 500-review sentiment comparison to expose the strengths and misses of keyword matching.
What it assumes
Assumes access to review data and benefits from enough text-mining knowledge to request cleaning and missing-value handling from an AI assistant.
What you can do after
Apply it via pandas to extract matching reviews, then evaluate positive-review labels with sensitivity and specificity.
How it connects
Connects keyword matching to frequency analysis, sentiment analysis, text preprocessing, and binary classification.

Also explained in

Compare how 8 books explain lexical search →

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.