Sentiment Analysis | Token | NLP Coping | PUNKT Library
The code show Sentence segmentation, Tokenization and Frequency distribution of tokens NLP Concept and TFIDF https://youtu.be/DeXh2ffLUO8?si=aqo5ymCqEXj_N_S5 Graph of Frequency distribution from nltk.tokenize import word_tokenize from nltk.probability import FreqDist import matplotlib.pyplot as plt text="""we hope this message finds you in good health and high spirits. We would like to inform you about the upcoming parent-teacher meeting (ptm) that will be held on 14th april 2023. We highly value your involvement in your child's education and appreciate your continued support in fostering a positive learning environment.""" token=word_tokenize(text) Frequensies=FreqDist(token) # Calculate frequency distribution #NLTK to plot a bar chart of the frequency distribution of the first 50 #tokens, and False (default): Shows a regular bar graph — each bar represents the individual frequency of a token. Frequensies.plot(10,cumulative=False) Code for the Sentiment Analysis from textblob import TextBlob import re def clean_text(text): # Remove emojis and special characters cleaned_text = re.sub('[^A-Za-z0-9]+', ' ', text) return cleaned_text def analyze_sentiment(text): # Perform sentiment analysis using TextBlob analysis = TextBlob(text) # Classify sentiment if analysis.sentiment.polarity greater than 0: #use the operator here return "Positive" elif analysis.sentiment.polarity less than 0: return "Negative" else: return "Neutral" def main(): print("Sentiment Analysis on Social Media Posts\n") # Get user input user_input = input("Enter a social media post or comment: ") # Clean the input text cleaned_input = clean_text(user_input) # Analyze sentiment sentiment = analyze_sentiment(cleaned_input) # Display results print("\nText:", user_input) print("Cleaned Text:", cleaned_input) print("Sentiment:", sentiment) # Optionally, visualize sentiment distribution using Matplotlib # This requires installing the 'matplotlib' library: pip install matplotlib try: import matplotlib.pyplot as plt # Create a simple pie chart labels = ['Positive', 'Negative', 'Neutral'] sizes = [sentiment.count('Positive'), sentiment.count('Negative'), sentiment.count('Neutral')] colors = ['lightcoral', 'lightblue', 'lightgreen'] explode = (0.1, 0, 0) plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=140) plt.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle. plt.title('Sentiment Distribution') plt.show() except ImportError: print("Matplotlib is not installed. Sentiment distribution will not be visualized.") if __name__ == "__main__": main()
Download
1 formatsVideo Formats
Right-click 'Download' and select 'Save Link As' if the file opens in a new tab.