-
Notifications
You must be signed in to change notification settings - Fork 1
/
text_analyzer.py
295 lines (220 loc) · 8.42 KB
/
text_analyzer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# -*- coding: utf-8 -*-
"""
Created on Sun Aug 22 4:04:15 2021
@author: ACER
D. Analyzer is an app made in python,
to analyze the given text or voice-based input.
It can help in getting the sentiment of text,
word cloud, summary, word count,
char count, and line count.
An App by Ddhruv Arora
"""
# pylint: disable = E1121
# pylint: disable = W0703
# pylint: disable = C0206
import streamlit as st
import speech_recognition as sr
import nltk
from nltk.sentiment.vader import SentimentIntensityAnalyzer
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize, sent_tokenize
from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
nltk.download('vader_lexicon')
nltk.download('punkt')
nltk.download('stopwords')
def markdown_runner(md_code):
""" A function to run HTML in MD code """
st.markdown(md_code, unsafe_allow_html=True)
def intro_func():
""" provides title and intro """
title_text = '''<p style="font-size:45px;"> Welcome to
<span style='color:blue'> D. Analyzer </span></p>'''
markdown_runner(title_text)
st.subheader("It can help in analyzing text form files and audio as well")
def decorator(func):
""" A decorator function to decorate the output """
def inner(section_name, *args, **kwargs):
markdown_runner(
f"""<p style="font_size:25px"> {section_name} </p>""")
func(*args, **kwargs)
markdown_runner("""<hr/>""")
return inner
def speech_to_text(file):
""" Function to convert speech/audio to text/string """
rec = sr.Recognizer()
with sr.AudioFile(file) as source:
audio = rec.record(source)
try:
text = rec.recognize_google(audio)
except Exception as error:
st.write(error)
return text
@decorator
def display_input(message):
""" Function to display the given input in text form """
st.write(message)
@decorator
def sentiment_analyzer(message):
"""
A function to analyze sentiments
It can be +ve, -ve, N, C
"""
sid = SentimentIntensityAnalyzer()
scores_raw = sid.polarity_scores(message)
new_keys = ["negative", "neutral", "positive", "compound"]
scores = dict(zip(new_keys, list(scores_raw.values())))
score_lst = [[key, value] for key, value in scores.items() if value > 0]
for type_of_sentiment, value_of_it in score_lst:
st.write(
f"""Sentiment type {type_of_sentiment} and
percentage of it is {round((value_of_it * 100), 2)}""")
@decorator
def word_cloud(message, selected_word_count):
""" Function to make word cloud based on the given inputs """
stopword = set(STOPWORDS)
wordcloud = WordCloud(background_color="white",
max_words=selected_word_count, stopwords=stopword).generate(message)
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
st.set_option('deprecation.showPyplotGlobalUse', False)
plt.show()
st.pyplot()
@decorator
def text_summarizer(text):
"""
This function will try to summarize
the text for you based on cosine prob.
"""
stop_words = set(stopwords.words("english"))
words = word_tokenize(text)
freq_table = dict()
for word in words:
word = word.lower()
if word in stop_words:
continue
if word in freq_table:
freq_table[word] += 1
else:
freq_table[word] = 1
sentences = sent_tokenize(text)
sentence_value = dict()
for sentence in sentences:
for word, freq in freq_table.items():
if word in sentence.lower():
if sentence in sentence_value:
sentence_value[sentence] += freq
else:
sentence_value[sentence] = freq
sum_values = 0
for sentence in sentence_value:
sum_values += sentence_value[sentence]
average = int(sum_values / len(sentence_value))
summary = ''
for sentence in sentences:
if (sentence in sentence_value) and (sentence_value[sentence] > (1.2 * average)):
summary += " " + sentence
if summary != "":
st.write(summary)
else:
st.write("File too short")
@decorator
def text_descreption(message):
""" To provide the basic descp. of text """
num_lines = len(message.split("\n"))
num_words = len(message.split())
num_char = 0
for _ in message:
num_char += 1
st.write("Number of words in text file: ", num_words)
st.write("Number of characters in text file: ", num_char)
st.write("Number of lines in text file: ", num_lines)
@decorator
def word_finder(find_word, message):
""" A function to find word in given text """
find_word = find_word.strip()
print(find_word)
if (find_word != "Enter a word") and (find_word.casefold() in message.casefold()):
st.success(f"The given word {find_word} is Found!!")
find_word_highlight = f"""<span style="color:red">{find_word.upper()}</span>"""
occurences = message.replace(find_word.casefold(), find_word_highlight)
markdown_runner(occurences)
elif find_word == "Enter a word":
st.write()
else:
st.error(f"The given word {find_word} is not Found!!")
@decorator
def play_audio(audio_bytes):
""" A function to show to audio provided as input """
st.audio(audio_bytes, format=r'audio/ogg')
def analyser(message, selected_word_count, find_word):
"""
It is the function that will invoke all other functions
to perform analysis on data
"""
sentiment_analyzer("Sentiment Analyzer", message)
word_cloud("Word Cloud", message, selected_word_count)
display_input("Given Input Data", message)
text_descreption("Text Description", message)
text_summarizer("Text Summary", message)
if find_word is not None:
word_finder("Word Finder", find_word, message)
def text_analysis(selected_word_count, find_word):
""" Mode based function sends text to analyzer"""
markdown_runner(
"""<p style="font_size:22px"> Text Analyzer Mode Selected </p>""")
markdown_runner("""<hr/>""")
markdown_runner("""<p style="font_size:25px"> Upload Data </p>""")
file_bytes = st.file_uploader("uplaod a text file", type="txt", key="1")
if file_bytes is not None and file_bytes.type.lower() in "text/plain":
st.success(f"Successfully Uploaded {file_bytes.name}")
markdown_runner("""<hr/>""")
message = file_bytes.read().decode()
analyser(message, selected_word_count, find_word)
def speech_analysis(selected_word_count, find_word):
"""
Mode based function
firts invokes speech to text
and then sends text to analyzer
"""
markdown_runner(
"""<p style="font_size:22px"> Speech Analyzer Mode Selected </p>""")
markdown_runner("""<hr/>""")
markdown_runner("""<p style="font_size:25px"> Upload Data </p>""")
file_bytes = st.file_uploader("uplaod a text file", type="wav", key="1")
if file_bytes is not None and file_bytes.type.lower() in "audio/wav":
st.success(f"Successfully Uploaded {file_bytes.name}")
markdown_runner("""<hr/>""")
play_audio("Given Audio Input", file_bytes)
message = speech_to_text(file_bytes)
analyser(message, selected_word_count, find_word)
modes = ["Audio input", "Text file input"]
def input_mode(mode_type, selected_word_count, find_words):
""" mode selector for Streamlit app """
if mode_type == modes[1]:
text_analysis(selected_word_count, find_words)
else:
speech_analysis(selected_word_count, find_words)
def setter_func():
""" The MAIN function.... """
st.sidebar.header("Select Input Mode")
selected_mode = st.sidebar.selectbox('Modes', modes)
st.sidebar.header("Select number of words you want to use")
selected_word_count = st.sidebar.selectbox(
"No of words", range(10, 1000, 10))
typ = ["Yes", "No"]
st.sidebar.header("Are you looking for a specific word or phrase")
find_words = st.sidebar.radio("Select", typ)
if find_words in typ[0]:
find_word = st.sidebar.text_input("Enter here", value="Enter a word")
else:
find_word = None
st.sidebar.header("Click here to reload")
btn = st.sidebar.button("Reload")
if btn:
print("Click")
st.experimental_rerun()
input_mode(selected_mode, selected_word_count, find_word)
if __name__ == '__main__':
intro_func()
setter_func()