liveBook · Manning

Tokenizer

As explained in Starting Data Analytics with Generative AI and Python

A tokenizer is a software component that segments text into discrete tokens according to specified processing rules, often for a particular language.

A tokenizer segments text into discrete tokens using processing rules. In frequency analysis, tokenization determines which words are counted, so the rules should match the language of the reviews. section 5.3.4

Portuguese customer reviews

For a DataFrame column named review_comment_message, a practical workflow lowercases reviews, tokenizes them, keeps alphabetic tokens, removes Portuguese stopwords, and counts the remaining words. A generic tokenizer may silently use English defaults even after Portuguese data is specified; inspect the generated code rather than assuming it adapted correctly. section 5.3.4

import pandas as pd
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
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 word frequencies
word_freq = Counter()
for _, row in df.iterrows():
    word_freq.update(row['processed_review'])

# Print the most common words and their frequencies
print(word_freq.most_common(10))

Compare the language-aware NLTK call with a default tokenizer, then run the workflow against the actual review column and validate the resulting counts. section 5.3.4

How Starting Data Analytics with Generative AI and Python covers it

How deep it goes
Explains tokenization’s role and demonstrates a language-specific correction, but does not develop tokenizer internals.
Examples
Uses Portuguese customer reviews in review_comment_message and contrasts default and language-aware NLTK tokenization.
What it assumes
Assumes a Python DataFrame and basic text-frequency analysis while specifying the review column and language.
What you can do after
Apply it via NLTK to tokenize Portuguese reviews and diagnose language-default mistakes in generated code.
How it connects
Connects tokenization with stopwords, frequency analysis, Pandas, NLTK, and language-dependent text preprocessing.

Also explained in

Compare how 16 books explain tokenizer →

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.