Sentiment Analysis

Try My New Google SEO Extension ­čśë­čśÄ ! I would be delighted to receive your feedback, comments, and suggestions. Please feel free to share your thoughts!

Text analysis is a fundamental task in natural language processing (NLP) that allows us to gain insights from textual data. In this article, we will explore how to perform text analysis using Python. We will focus on extracting keywords from the text and conducting sentiment analysis.

Extracting Keywords

Keywords play a vital role in understanding the main topics and themes in a text. Let's see how we can extract keywords using the rake_nltk library.

Go Code!

Code for Extracting Keywords

import os
from rake_nltk import Rake

# Initialize Rake
rake_nltk_var = Rake()

# Example Text
text = """spaCy is an open-source software library for advanced natural language processing,
written in the programming languages Python and Cython. The library is published under the MIT license
and its main developers are Matthew Honnibal and Ines Montani, the founders of the software company Explosion."""

# Extract Keywords
keyword_extracted = rake_nltk_var.get_ranked_phrases_with_scores()

The above code initializes the Rake object from the rake_nltk library. We then provide the example text and extract keywords using the extract_keywords_from_text() method. The extracted keywords are stored in the keyword_extracted variable.

Sentiment Analysis

Sentiment analysis is the process of determining the emotional tone of a text. It helps us understand whether a piece of text expresses a positive, negative, or neutral sentiment.

Code for Sentiment Analysis

from textblob import TextBlob

# Perform Sentiment Analysis
blob = TextBlob(text)
sentiment_score = blob.sentiment.polarity
sentiment_label = "Positive" if sentiment_score > 0 else "Negative" if sentiment_score < 0 else "Neutral"

In the code above, we utilize the TextBlob library to perform sentiment analysis. We pass the text variable to the TextBlob constructor, and then access the sentiment polarity using the sentiment.polarity attribute. Based on the sentiment score, we assign a sentiment label to the sentiment_label variable.

Generating HTML Report

To present the results of our text analysis, we can generate an HTML report that includes the extracted keywords and sentiment analysis.

Code for Generating HTML Report

import webbrowser

# Generate HTML Content
html_content = """
    <title>Text Analysis Report</title>
    <h1>Keywords Extraction</h1>

# Add Extracted Keywords
for score, keyword in keyword_extracted:
    html_content += "<p><b>Keyword:</b> {}</p>".format(keyword)
    html_content += "<p><b>Score:</b> {}</p>".format(score)
    html_content += "<br>"

# Add Sentiment Analysis Results
html_content += """
    <h1>Sentiment Analysis</h1>
    <p><b>Sentiment Score:</b> {}</p>
    <p><b>Sentiment Label:</b> {}</p>

# Save HTML Content to File
html_filename = "text_analysis_report.html"
with open(html_filename, "w", encoding="utf-8") as file:

# Get Absolute Path of HTML File
html_path = os.path.abspath(html_filename)

# Open HTML File in Web Browser

print("HTML report generated successfully and opened in a web browser.")

In the code above, we define the HTML content that will be includedin the report. We iterate over the extracted keywords and append them to the HTML content. Finally, we add the sentiment analysis results to the HTML content. The content is then saved to an HTML file named "text_analysis_report.html". We use the webbrowser library to open the HTML file in a web browser.


In this article, we explored how to perform text analysis using Python. We learned how to extract keywords from text using the rake_nltk library and perform sentiment analysis using the TextBlob library. Additionally, we generated an HTML report to present the results of our text analysis.

Go Code!

By leveraging Python's powerful libraries, we can gain valuable insights from textual data for various applications. Whether it's analyzing customer feedback, understanding social media sentiment, or extracting key information from large text datasets, text analysis in Python opens up a world of possibilities.

Remember to experiment with different texts and explore the vast functionalities and customization options provided by the libraries mentioned in this article. Happy text analyzing!

por Alexis Galán

SEO & Python...

Deja una respuesta