liveBook · Manning

Portuguese

As explained in Starting Data Analytics with Generative AI and Python

Portuguese is a natural language spoken and written in Portuguese-speaking communities and supported by language-specific text-processing resources.

When customer reviews are in Portuguese, the language is a processing requirement, not just a detail to mention in a prompt. Counting common words calls for Portuguese stopwords and a Portuguese tokenizer; searching for positive feedback calls for Portuguese terms whose meanings fit the review context. section 5.3.4 section 5.5.3

For a Python word-frequency workflow:

  • Identify the DataFrame column containing the reviews, such as review_comment_message, and tell the AI assistant that its contents are in Portuguese. section 5.3.4
  • Check the generated code for both stopwords.words('portuguese') and word_tokenize(text.lower(), language='portuguese'). Specifying the language in the prompt did not make the first generated tokenizer use it. section 5.3.4
  • Inspect the resulting words and counts rather than assuming that language-specific code produces useful findings. 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))

The corrected listing shows the consequential tokenizer argument, language='portuguese', alongside Portuguese stopword removal. Inspect the complete code before running it. section 5.3.4

Finding positive reviews

A keyword request can return excelente (“excellent”), ótimo (“great”), and vale a pena (“worth it”). These are candidates for searching reviews, not proof that every matching review is positive: check the surrounding words and allow for inflections and contractions. section 5.5.3

For broader sentiment analysis, possible Portuguese lexicons include OPINLEX and SentiLex; a Portuguese version of LIWC is also mentioned but requires purchase. Check whether a resource is available and suitable before relying on it: non-English lexicons can be difficult to locate, and academic tools may cease to be maintained. section 5.6.2

How Starting Data Analytics with Generative AI and Python covers it

How deep it goes
Works through tokenizer and stopword choices plus keyword generation, while cautioning that generated output needs contextual checks.
Examples
Uses a customer-review DataFrame, corrected NLTK tokenizer code, and translated positive terms such as excelente and vale a pena.
What it assumes
Assumes a Python DataFrame containing customer reviews while making the need for Portuguese-specific preprocessing explicit.
What you can do after
Apply it via Pandas and NLTK to prepare Portuguese reviews for word counts, and evaluate candidate sentiment keywords against review context.
How it connects
Links Portuguese to tokenization, stopwords, word-frequency analysis, and 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.