In the digital age, customer reviews are a goldmine of feedback, but reading thousands of text submissions manually is impossible for a growing business. Typically, companies rely on customers to select a star rating alongside their written thoughts. However, text reviews often contain deep emotional nuances that numbers alone cannot capture.
This project solves that problem by building a smart Python system that uses the Cohere AI API to automatically read written text reviews and accurately predict what the customer's star rating would be. By shifting from manual text sorting to automated AI classification, this tool allows businesses to process massive amounts of feedback instantly, automatically label unstructured text, and flag unhappy customers in real-time.
Customer support and product teams are often overwhelmed by unstructured text data from websites, app stores, or travel platforms. When a customer leaves a long paragraph describing their experience, a human must read it to understand if it is a major complaint or a rave review.
This project is highly relevant because it demonstrates how modern Large Language Models (LLMs) can be integrated into regular business workflows using Python. By leveraging text embeddings and classification models, any company can build an automated pipeline that acts as an "artificial reader," instantly organizing feedback, detecting customer satisfaction levels, and tracking brand reputation without human intervention.
The dataset used for this project is the TripAdvisor Hotel Reviews dataset. This is a popular, real-world public dataset consisting of authentic travel reviews scraped directly from the TripAdvisor platform to reflect how everyday people write online.
The data was collected by gathering historical hotel reviews containing raw customer write-ups paired with their corresponding official star ratings. The text reflects real-world language challenges, including casual slang, missing punctuation, and mixed feelings (e.g., loving the room but hating the parking price).
The dataset is structured as a streamlined tabular file containing two primary columns:
- Review:
Text / Object. The raw, written feedback provided by the traveler describing their stay, hotel staff, cleanliness, and overall experience. - Rating:
Integer. The target variable containing the numerical score given by the customer, ranging on a standard scale from 1 (worst experience) to 5 (best experience).
The engineering pipeline was implemented entirely within a Google Colab Notebook utilizing a structured Python workflow to ensure speed, reproducibility, and clear separation of concerns.
- Google Colab Virtual Drive Integration: Leveraged
google.colab.filesto upload local project assets directly into the dynamic cloud server instance. - Library Implementations: Deployed
pandasfor handling data frames,timeto space API rate requests safely, andsklearnfor executing performance diagnostic reports.
- Duplicate and Shape Assessment: Analyzed row structures and confirmed data table shapes to establish a healthy data baseline before passing inputs to the API.
- Structural Optimization: Inspected non-null text records to guarantee zero runtime failures when streaming raw strings into the language processor.
-
Algorithmic Range Definition: Designed a standard Python labeling function to translate discrete 1–5 numerical values into clean, comparative classifications:
-
Negative: Ratings
$\le 2$ -
Neutral: Ratings
$= 3$ -
Positive: Ratings
$\ge 4$
-
Negative: Ratings
- Class Distribution Check: Audited value distributions to measure the exact balance of text types entering the classification phase.
- API Environment Connection: Installed the native
coheredistribution toolkit via pip and established secure runtime authentication using a client key handler. - Rate Limit Protection: Implemented automated pacing commands inside the generation loop to avoid request flooding and ensure smooth data streaming across thousands of token segments.
Below is a step-by-step breakdown of the Python code used to build this automated sentiment system, moving from data preparation to AI evaluation.
from google.colab import files
uploaded = files.upload()- Opens a file selection box directly inside Google Colab, allowing you to upload the raw review spreadsheet from your personal computer to the workspace.
import pandas as pd
df = pd.read_csv("tripadvisor_hotel_reviews.csv")
df.head(5)- Imports the Pandas library, loads the CSV file into a structured data frame, and prints out the first 5 records to verify that the text columns loaded correctly.
print("Shape:", df.shape)
print("Columns:", df.columns.tolist())
print("\nData Types:\n", df.dtypes)
print("\nMissing Values:\n", df.isnull().sum())- Checks the health and size of the dataset. It prints the row count, confirms column names, looks at column types, and checks for any blank (missing) cells.
def label_sentiment(rating):
if rating <= 2:
return "Negative"
elif rating == 3:
return "Neutral"
else:
return "Positive"
df["Sentiment"] = df["Rating"].apply(label_sentiment)
print(df["Sentiment"].value_counts())
df.head()- Translates numerical star ratings into text labels so the AI can learn from them. 1-2 stars become "Negative", 3 stars become "Neutral", and 4-5 stars become "Positive". It then counts how many reviews fall into each bucket.
!pip install cohere- Downloads and installs the official Cohere Python library onto your workspace environment so you can connect to their large language models.
import cohere
co = cohere.ClientV2(api_key="YOUR_API_KEY")
response = co.chat(
model="command-r-08-2024",
messages=[{"role": "user", "content": "Say hello"}]
)
print(response.message.content[0].text)- Initializes the AI client using your private security key and sends a quick test message ("Say hello") to the command-r model to verify the system is communicating properly.
import time
def predict_sentiment_cohere(review):
prompt = f"""Classify the sentiment of this hotel review as exactly one word: Negative, Neutral, or Positive.
Review: {review}
Answer:"""
response = co.chat(
model="command-r-08-2024",
messages=[{"role": "user", "content": prompt}]
)
result = response.message.content[0].text.strip()
if "Positive" in result:
return "Positive"
elif "Negative" in result:
return "Negative"
else:
return "Neutral"
predictions_cohere = []
for i, review in enumerate(df["Review"]):
sentiment = predict_sentiment_cohere(review)
predictions_cohere.append(sentiment)
print(f"Row {i+1}: {sentiment}")
time.sleep(4)
df["Predicted_Sentiment_Cohere"] = predictions_cohere
print("\nDone!")- The engine of the project. It packages each written review into a targeted prompt, asks the AI to read it, and interprets the response. A time.sleep(4) command is included to space calls out by 4 seconds to comply with free API speed limits. The final results are stored in a new prediction column.
from sklearn.metrics import accuracy_score, classification_report
accuracy_cohere = accuracy_score(df["Sentiment"], df["Predicted_Sentiment_Cohere"])
print(f"Cohere Accuracy: {accuracy_cohere:.2%}\n")
print("Classification Report:")
print(classification_report(df["Sentiment"], df["Predicted_Sentiment_Cohere"]))- Uses a machine learning evaluation library to compare the AI's guesses against the actual customer ratings, outputting the exact accuracy percentage alongside a detailed breakdown of precision and recall scores.
df.to_csv("sentiment_analysis_results.csv", index=False)
print("File saved successfully!")
print(df.columns.tolist())- Saves the final table—complete with the original review text, ratings, ground truth labels, and AI predictions—as a new local CSV file.
from google.colab import files
files.download("sentiment_analysis_results.csv")- Triggers an automatic download prompt to save the newly completed results file straight onto your PC's hard drive.
By looking closely at the performance chart metrics, we can see exactly how well the AI model functions when sorting online customer reviews:
- High Global Reliability: The Cohere Command-R model achieved a strong overall 80.73% accuracy rate. This proves the AI can read unstructured text paragraphs and correctly align them with human-selected rating habits four out of five times.
- Exceptional Negative & Positive Detection: The system performs brilliantly on extreme sentiments. It hit an outstanding 0.91 Recall for Negative reviews and a 0.81 Recall for Positive reviews. This means the model successfully catches 91% of all angry customers and 81% of all happy customers, minimizing the chance that an escalation goes unnoticed.
- The Neutral Text Trap: The model struggles with mild or mixed reviews, showing a low F1-Score of 0.42 for the Neutral category. Looking at the Confusion Matrix, the AI mistakenly classified 5 neutral reviews as Negative and 2 as Positive. This happens because mixed feedback (e.g., "The bed was cozy, but the service was slow") confuses language metrics that look for direct emotional cues.
Based on the AI's performance habits, businesses should implement the following steps:
- Deploy an Automated Escalation Alert: Because the model successfully flags 91% of negative sentiments, it should be used as a live triaging system. Whenever the system flags a text review as "Negative," an automatic email alert should instantly route that ticket to a senior customer support manager for rapid resolution.
- Route Neutral Reviews to Humans: Since the AI has a tougher time distinguishing lukewarm or mixed reviews (42% F1-Score), any review labeled as "Neutral" by the model should be routed to a human reviewer to capture individual context and nuance.
- Refine the Prompt Framework: To boost the model's accuracy past 80%, update the prompt instructions with clear, boundary-setting examples of "Neutral" reviews so the model has a better reference point for balancing positive and negative phrases in the same block of text.
This project successfully establishes how modern Large Language Models can eliminate the exhausting process of sorting text feedback by hand. By setting up a live pipeline using Python and the Cohere API, raw customer feedback was automatically read, categorized, and scored with 80.73% accuracy.
While mixed or neutral text remains a classic hurdle for natural language systems, the engine's elite performance in isolating Critical Negative reviews (91% recall) makes it a powerful asset for any customer experience team. This framework successfully turns massive, unorganized text reviews into an automated, highly responsive diagnostic tracker for customer satisfaction.