Ashar086 commited on
Commit
e4de637
·
verified ·
1 Parent(s): 76ce3f2

Create text_analyzer.py

Browse files
Files changed (1) hide show
  1. text_analyzer.py +71 -0
text_analyzer.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import nltk
4
+ from nltk.corpus import stopwords
5
+ from nltk.tokenize import word_tokenize
6
+ from nltk.sentiment import SentimentIntensityAnalyzer
7
+ from collections import Counter
8
+ from wordcloud import WordCloud
9
+ import matplotlib.pyplot as plt
10
+
11
+ nltk.download('punkt')
12
+ nltk.download('stopwords')
13
+ nltk.download('vader_lexicon')
14
+
15
+ class TextAnalyzer:
16
+ def analyze_text(self, df):
17
+ text_columns = df.select_dtypes(include=['object']).columns
18
+
19
+ if len(text_columns) > 0:
20
+ text_column = st.selectbox("Select text column for analysis", text_columns)
21
+ analysis_type = st.selectbox("Select analysis type", ["Word Frequency", "Sentiment Analysis", "Word Cloud"])
22
+
23
+ if analysis_type == "Word Frequency":
24
+ self.perform_word_frequency(df[text_column])
25
+ elif analysis_type == "Sentiment Analysis":
26
+ self.perform_sentiment_analysis(df[text_column])
27
+ elif analysis_type == "Word Cloud":
28
+ self.generate_word_cloud(df[text_column])
29
+ else:
30
+ st.write("No text columns found in the dataset.")
31
+
32
+ def perform_word_frequency(self, text_series):
33
+ stop_words = set(stopwords.words('english'))
34
+ word_freq = Counter()
35
+
36
+ for text in text_series:
37
+ tokens = word_tokenize(text.lower())
38
+ words = [word for word in tokens if word.isalnum() and word not in stop_words]
39
+ word_freq.update(words)
40
+
41
+ st.subheader("Word Frequency Analysis")
42
+ n_words = st.slider("Select number of top words to display", min_value=5, max_value=50, value=20)
43
+
44
+ top_words = word_freq.most_common(n_words)
45
+ fig = px.bar(x=[word for word, _ in top_words], y=[freq for _, freq in top_words], title="Top Words")
46
+ st.plotly_chart(fig)
47
+
48
+ def perform_sentiment_analysis(self, text_series):
49
+ sia = SentimentIntensityAnalyzer()
50
+ sentiments = text_series.apply(lambda x: sia.polarity_scores(x))
51
+
52
+ st.subheader("Sentiment Analysis")
53
+ sentiment_df = pd.DataFrame(sentiments.tolist())
54
+
55
+ fig = px.histogram(sentiment_df, x='compound', title="Sentiment Distribution")
56
+ st.plotly_chart(fig)
57
+
58
+ st.write("Average Sentiment Scores:")
59
+ st.write(sentiment_df.mean())
60
+
61
+ def generate_word_cloud(self, text_series):
62
+ stop_words = set(stopwords.words('english'))
63
+ text = ' '.join(text_series)
64
+
65
+ wordcloud = WordCloud(width=800, height=400, background_color='white', stopwords=stop_words).generate(text)
66
+
67
+ st.subheader("Word Cloud")
68
+ fig, ax = plt.subplots()
69
+ ax.imshow(wordcloud, interpolation='bilinear')
70
+ ax.axis('off')
71
+ st.pyplot(fig)