Keyword-based extraction classifies or filters text by checking whether predefined terms appear in each record. For example, Portuguese review terms such as “ótimo,” “excelente,” and “recomendo” can flag likely positive reviews. section 5.5.7
import pandas as pd
# Assuming you have already loaded the dataframe and defined the
is_positive function
# Define a function to determine if a review is positive based on the
product score
def is_positive_based_on_score(score, threshold=4):
return score >= threshold
# Apply the function to the 'product_score' column
df['is_positive_based_on_score'] =
df['product_score'].apply(is_positive_based_on_score)
# Calculate the confusion matrix
TP = sum((df['is_positive'] == True) & (df['is_positive_based_on_score']
== True))
FP = sum((df['is_positive'] == True) & (df['is_positive_based_on_score']
== False))
TN = sum((df['is_positive'] == False) & (df['is_positive_based_on_score']
== False))
FN = sum((df['is_positive'] == False) & (df['is_positive_based_on_score']
== True))
# Calculate sensitivity (true positive rate) and specificity (true
negative rate)
sensitivity = TP / (TP + FN)
specificity = TN / (TN + FP)
print(f'Sensitivity: {sensitivity:.2f}')
print(f'Specificity: {specificity:.2f}')
Turns product scores into a second positive-or-negative label, builds a confusion matrix, and calculates sensitivity and specificity for the keyword extraction. section 5.5.7
