liveBook · Manning

Frequency

As explained in Starting Data Analytics with Generative AI and Python

Information describing how often a word or expression occurs in a language or a specified text collection.

Frequency measures how often a word or expression appears in a language or a text collection. In text analysis, counts become more useful when they preserve context: “delivery” says little alone, while frequent phrases such as “failed delivery” or “super fast delivery” can reveal customer concerns or strengths. section 5.3.5 section 5.6.1

Use frequency analysis to:

  • Count common words when a broad view of the text is sufficient. section 5.3.5
  • Count consecutive word sequences, called n-grams, when context and nuance matter. section 5.3.5
  • Compare bigrams, trigrams, or higher-order n-grams according to the desired level of detail. section 5.3.5

Review analysis workflow

A practical workflow loads review data, tokenizes Portuguese text, removes punctuation and Portuguese stopwords, and counts trigrams. Inspect the resulting phrases before drawing conclusions, since recurrence can indicate trends or concerns but still requires interpretation. section 5.3.5

Listing 5.1 Code snippet for analyzing trigrams
import pandas as pd
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.collocations import TrigramAssocMeasures, 
TrigramCollocationFinder
from collections import Counter

# Make sure to download Portuguese stopwords
nltk.download('stopwords')
nltk.download('punkt')

# Load your DataFrame
df = pd.read_csv('olist_order_reviews_dataset.csv')

# Preprocessing function
def preprocess_text(text):
    # Tokenize, convert to lowercase, and remove punctuation
    tokens = word_tokenize(text.lower(), language='portuguese')
    words = [word for word in tokens if word.isalpha()]

    # Remove Portuguese stopwords
    stop_words = set(stopwords.words('portuguese'))
    filtered_words = [word for word in words if word not in stop_words]

    return filtered_words

# Apply preprocessing to the 'review_comment_message' column
df['processed_review'] = 
df['review_comment_message'].apply(preprocess_text)

# Calculate trigram frequencies
trigram_measures = TrigramAssocMeasures()
trigram_freq = Counter()

for _, row in df.iterrows():
    finder = TrigramCollocationFinder.from_words(row['processed_review'])
    trigram_freq.update(finder.ngram_fd)

# Print the most common trigrams and their frequencies
print(trigram_freq.most_common(10))

Inspect the Python listing for a concrete trigram-analysis implementation using pandas and NLTK. section 5.3.5

How Starting Data Analytics with Generative AI and Python covers it

How deep it goes
Explains word counts and n-gram choices with a workflow, but stops short of detailed statistical interpretation.
Examples
Illustrates frequency with delivery phrases, customer reviews, and Portuguese trigram preprocessing.
What you can do after
Apply frequency analysis via pandas and NLTK to extract recurring trigrams from review data.
How it connects
Connects frequency with n-grams, sentiment analysis, dictionaries, stopwords, and text preprocessing.

Also explained in

Compare how 7 books explain frequency →

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.