-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTexter.py
369 lines (312 loc) · 12.9 KB
/
Texter.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# Core Pkgs
import streamlit as st
import altair as alt
import pandas as pd
import numpy as np
import joblib
import matplotlib.pyplot as plt
from PIL import Image
from collections import Counter
import seaborn as sns
from wordcloud import WordCloud
import PyPDF2
from docx import Document
from ebooklib import epub
from pyth.plugins.rtf15.reader import Rtf15Reader
import tempfile
import shutil
import os
# NLP
import neattext.functions as nfx
import spacy
from spacy import displacy
# Text Downloader
import base64
import time
from urllib.request import urlopen
from bs4 import BeautifulSoup
# Sumy Summary Pkg
from sumy.parsers.plaintext import PlaintextParser
from sumy.nlp.tokenizers import Tokenizer
from sumy.summarizers.lex_rank import LexRankSummarizer
# Set up Spacy
nlp = spacy.load("en_core_web_sm")
# Load Emotion Classifier Model (Replace with your actual model path)
pipe_lr = joblib.load(open("path_to_your_emotion_classifier_model.pkl", "rb"))
# Global Variables
emotions_emoji_dict = {
"anger": "😠",
"disgust": "🤮",
"fear": "😨😱",
"happy": "🤗",
"joy": "😂",
"neutral": "😐",
"sad": "😔",
"sadness": "😔",
"shame": "😳",
"surprise": "😮",
}
# Function for cleaning text
def clean_text(raw_text, normalize_case=False, clean_stopwords=False):
if normalize_case:
raw_text = raw_text.lower()
if clean_stopwords:
stop_words = nfx.available_stopwords()
raw_text = nfx.remove_stopwords(raw_text, stopwords=stop_words)
return raw_text
# Function for extracting text from PDF documents
def extract_text_from_pdf(pdf_file):
text = ""
with open(pdf_file, "rb") as pdf:
pdf_reader = PyPDF2.PdfFileReader(pdf)
for page_num in range(pdf_reader.numPages):
text += pdf_reader.getPage(page_num).extractText()
return text
# Function for extracting text from DOCX documents
def extract_text_from_docx(docx_file):
text = ""
doc = Document(docx_file)
for paragraph in doc.paragraphs:
text += paragraph.text
return text
# Function for extracting text from EPUB documents
def extract_text_from_epub(epub_file):
text = ""
book = epub.read_epub(epub_file)
for item in book.get_items():
if isinstance(item, epub.EpubHtml):
text += item.get_body_content()
return text
# Function for extracting text from RTF documents
def extract_text_from_rtf(rtf_file):
text = ""
doc = Rtf15Reader.read(open(rtf_file, "rb"))
for elem in doc.content:
if isinstance(elem, str):
text += elem
return text
# Function to convert file extension
def convert_file_extension(file_path, new_extension):
# Create a temporary directory to store the converted file
temp_dir = tempfile.mkdtemp()
temp_file = f"{temp_dir}/temp_file.{new_extension}"
try:
# Copy the file to the temporary directory with the new extension
shutil.copy(file_path, temp_file)
return temp_file
except Exception as e:
return None
finally:
# Clean up the temporary directory
shutil.rmtree(temp_dir)
# Function for analyzing text
def analyze_text(text):
docx = nlp(text)
allData = [
(
token.text,
token.shape_,
token.pos_,
token.tag_,
token.lemma_,
token.is_alpha,
token.is_stop,
)
for token in docx
]
df = pd.DataFrame(
allData,
columns=["Token", "Shape", "PoS", "Tag", "Lemma", "IsAlpha", "Is_Stopword"],
)
return df
# Function for predicting emotions
def predict_emotions(docx):
results = pipe_lr.predict([docx])
return results[0]
# Function for getting prediction probabilities
def get_prediction_proba(docx):
results = pipe_lr.predict_proba([docx])
return results
# Function for rendering entities with highlighting
def render_entities(rawtext):
docx = nlp(rawtext)
html = displacy.render(docx, style="ent")
html = html.replace("\n\n", "\n")
result = HTML_WRAPPER.format(html)
return result
# Function for getting most common tokens
def get_most_common_tokens(docx, num=10):
word_freq = Counter(docx.split())
most_common_tokens = word_freq.most_common(num)
return dict(most_common_tokens)
# Function for downloading text as a file
def text_downloader(raw_text):
b64 = base64.b64encode(raw_text.encode()).decode()
new_filename = "clean_text_result_{}_.txt".format(time.strftime("%Y%m%d-%H%M%S"))
href = f'<a href="data:file/txt;base64,{b64}" download="{new_filename}">Download</a>'
st.markdown("### 📥 Download Cleaned Text file ")
st.markdown(href, unsafe_allow_html=True)
# Function for making dataframe downloadable as CSV
def make_downloadable(data):
csvfile = data.to_csv(index=False)
b64 = base64.b64encode(csvfile.encode()).decode()
new_filename = "nlp_result_{}_.csv".format(time.strftime("%Y%m%d-%H%M%S"))
st.markdown("### 📥 Download CSV file ")
href = f'<a href="data:file/csv;base64,{b64}" download="{new_filename}">Download</a>'
st.markdown(href, unsafe_allow_html=True)
# Streamlit app
def main():
image = Image.open("logo.png")
st.image(image, use_column_width=True)
st.title("Texter")
menu = ["Text Cleaner", "Emotion Classifier", "Summarizer and Entity Checker", "File Conversion", "About"]
choice = st.sidebar.selectbox("Menu", menu)
if choice == "Text Cleaner":
st.title("Text Cleaner")
menu = ["Text Cleaner", "About"]
choice = st.sidebar.selectbox("Select", menu)
if choice == "Text Cleaner":
text_file = st.file_uploader("Upload Txt File", type=["txt", "pdf", "docx", "epub", "rtf"])
normalize_case = st.sidebar.checkbox("Normalize Case")
clean_stopwords = st.sidebar.checkbox("Stopwords")
if text_file is not None:
file_details = {
"Filename": text_file.name,
"Filesize": text_file.size,
"Filetype": text_file.type,
}
st.write(file_details)
# Save uploaded file temporarily
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
temp_file.write(text_file.read())
# Extract text based on file type
if text_file.type == "text/plain":
raw_text = extract_text_from_txt(temp_file.name)
elif text_file.type == "application/pdf":
raw_text = extract_text_from_pdf(temp_file.name)
elif text_file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
raw_text = extract_text_from_docx(temp_file.name)
elif text_file.type == "application/epub+zip":
raw_text = extract_text_from_epub(temp_file.name)
elif text_file.type == "application/rtf":
raw_text = extract_text_from_rtf(temp_file.name)
col1, col2 = st.columns(2)
with col1:
with st.expander("Original Text"):
st.write(raw_text)
with col2:
with st.expander("Processed Text"):
raw_text = clean_text(raw_text, normalize_case, clean_stopwords)
st.write(raw_text)
text_downloader(raw_text)
with st.expander("Text Analysis"):
token_result_df = analyze_text(raw_text)
st.dataframe(token_result_df)
make_downloadable(token_result_df)
with st.expander("Plot Wordcloud"):
plot_wordcloud(raw_text)
with st.expander("Plot PoS Tags"):
fig = plt.figure()
sns.countplot(token_result_df["PoS"])
plt.xticks(rotation=45)
st.pyplot(fig)
else:
st.subheader("About")
st.markdown("It's an app.")
if choice == "Emotion Classifier":
st.title("Emotion Classifier")
menu = ["Home", "About"]
choice = st.sidebar.selectbox("Select", menu)
if choice == "Home":
st.subheader("Emotions in Text")
with st.form(key="emotion_clf_form"):
raw_text = st.text_area("Type Here")
submit_text = st.form_submit_button(label="Submit")
if submit_text:
col1, col2 = st.columns(2)
# Apply Function Here
prediction = predict_emotions(raw_text)
probability = get_prediction_proba(raw_text)
with col1:
st.success("Original Text")
st.write(raw_text)
st.success("Prediction")
emoji_icon = emotions_emoji_dict[prediction]
st.write("{}:{}".format(prediction, emoji_icon))
st.write("Confidence:{}".format(np.max(probability)))
with col2:
st.success("Prediction Probability")
proba_df = pd.DataFrame(
probability, columns=pipe_lr.classes_
).T.reset_index()
proba_df.columns = ["emotions", "probability"]
fig = alt.Chart(proba_df).mark_bar().encode(
x="emotions", y="probability", color="emotions"
)
st.altair_chart(fig, use_container_width=True)
else:
st.subheader("About")
st.markdown("It's an app.")
if choice == "Summarizer and Entity Checker":
st.title("Summarizer and Entity Checker")
menu = ["Summarize", "NER Checker", "NER For URLs", "About"]
choice = st.sidebar.selectbox("Select", menu)
if choice == "Summarize":
st.subheader("Summarize Document")
raw_text = st.text_area("Enter Text Here", "Type Here")
summarizer_type = st.selectbox("Summarizer Type", ["Gensim", "Sumy Lex Rank"])
if st.button("Summarize"):
if summarizer_type == "Gensim":
summary_result = summarize(raw_text)
elif summarizer_type == "Sumy Lex Rank":
summary_result = sumy_summarizer(raw_text)
st.write(summary_result)
if choice == "NER Checker":
st.subheader("Named Entity Recognition with Spacy")
raw_text = st.text_area("Enter Text Here", "Type Here")
if st.button("Analyze"):
docx = analyze_text(raw_text)
html = displacy.render(docx, style="ent")
html = html.replace("\n\n", "\n")
st.write(HTML_WRAPPER.format(html), unsafe_allow_html=True)
if choice == "NER For URLs":
st.subheader("Analysis on Text From URL")
raw_url = st.text_input("Enter URL Here", "Type here")
text_preview_length = st.slider("Length to Preview", 50, 100)
if st.button("Analyze"):
if raw_url != "Type here":
result = get_text(raw_url)
len_of_full_text = len(result)
len_of_short_text = round(len(result) / text_preview_length)
st.success("Length of Full Text: {}".format(len_of_full_text))
st.success("Length of Short Text: {}".format(len_of_short_text))
st.info(result[:len_of_short_text])
summarized_docx = sumy_summarizer(result)
docx = analyze_text(summarized_docx)
html = displacy.render(docx, style="ent")
html = html.replace("\n\n", "\n")
st.write(HTML_WRAPPER.format(html), unsafe_allow_html=True)
else:
st.subheader("About")
st.markdown("It's an app.")
if choice == "File Conversion":
st.title("File Conversion")
menu = ["Convert File", "About"]
choice = st.sidebar.selectbox("Select", menu)
if choice == "Convert File":
st.subheader("Convert File Extension")
uploaded_file = st.file_uploader("Upload a File", type=["txt", "pdf", "docx", "epub", "rtf"])
new_extension = st.selectbox("Select New Extension", ["txt", "pdf", "docx", "epub", "rtf"])
if uploaded_file is not None:
with st.spinner("Converting..."):
# Convert file extension and get the new file path
converted_file_path = convert_file_extension(uploaded_file, new_extension)
if converted_file_path is not None:
st.success(f"File converted successfully! [Download]({converted_file_path})")
else:
st.error("Conversion failed. Please try again.")
else:
st.subheader("About")
st.markdown("It's an app.")
if __name__ == "__main__":
main()