File size: 6,687 Bytes
575adcc |
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 |
import streamlit as st
import pandas as pd
import streamlit.components.v1 as stc
import docx2txt
# NLP Package-used for text analysis
import nltk
from nltk.tokenize import word_tokenize
from nltk.tag import pos_tag
from nltk.stem import WordNetLemmatizer
from nltk.corpus import stopwords
# from nltk import ne_chunk
from nltk.tag import StanfordNERTagger
from collections import Counter
from textblob import TextBlob
import seaborn as sns
import matplotlib.pyplot as plt
from wordcloud import WordCloud
import base64
import time
from app_utils import *
HTML_BANNER = """
<div style="background-color:green;padding:10px;border-radius:10px">
<h1 style="color:white;text-align:center;">Text Analysis App </h1>
</div>
"""
def text_analysis():
stc.html(HTML_BANNER)
menu=['Text-analysis','Upload_Files']
choice=st.sidebar.selectbox('Menu',menu)
if choice=='Text-analysis':
st.subheader('Analyse Text')
text=st.text_area("Enter the text to anlayze")
if (st.button("Analyze")):
st.success("Success")
with st.expander('Original Text'):
st.write(text)
with st.expander('Text Analysis'):
token_analysis=nlp_analysis(text)
st.dataframe(token_analysis)
with st.expander('Entitites'):
entity_result=find_entities(text)
stc.html(entity_result, height=100, scrolling=True)
col1,col2=st.columns(2)
with col1:
with st.expander("Word Stats"):
st.info("Word Statistics")
docx = nt.TextFrame(text)
st.write(docx.word_stats())
with st.expander("Top keywords"):
keywords=get_most_common_tokens(text)
st.write(keywords)
with st.expander('Tagged Keywords'):
data= pos_tag(text)
st.dataframe(data)
visualize_tags=tag_visualize(data)
stc.html(visualize_tags,scrolling=True)
with st.expander("Sentiment"):
sent_result=get_semantics(text)
st.write(sent_result)
with col2:
with st.expander("Plot word freq"):
try:
fig, ax = plt.subplots()
most_common_tokens = dict(token_analysis["Token"].value_counts())
sns.countplot(data=token_analysis[token_analysis["Token"].isin(most_common_tokens)], x="Token", ax=ax)
ax.set_xlabel('PoS')
ax.set_ylabel('Frequency')
ax.tick_params(axis='x' , rotation=45)
st.pyplot(fig)
except:
st.warning('Insufficient data')
with st.expander("Plot part of speech"):
try:
fig, ax = plt.subplots()
most_common_tokens = dict(token_analysis["Position"].value_counts())
sns.countplot(data=token_analysis[token_analysis["Position"].isin(most_common_tokens)], x="Position", ax=ax)
ax.set_xlabel('PoS')
ax.set_ylabel('Frequency')
ax.tick_params(axis='x' , rotation=45)
st.pyplot(fig)
except:
st.warning('Insufficient data')
with st.expander("Plot word cloud"):
try:
plot_wordcloud(text)
except:
st.warning('Insufficient data')
with st.expander('Download Results'):
file_download(token_analysis)
elif choice == 'Upload_Files':
text_file = st.file_uploader('Upload Files', type=['docx'])
if text_file is not None:
if text_file.type == 'text/plain':
text = str(text_file.read(), "utf-8")
else:
text = docx2txt.process(text_file)
if (st.button("Analyze")):
with st.expander('Original Text'):
st.write(text)
with st.expander('Text Analysis'):
token_analysis = nlp_analysis(text)
st.dataframe(token_analysis)
with st.expander('Entities'):
entity_result = find_entities(text)
stc.html(entity_result, height=100, scrolling=True)
col1, col2 = st.columns(2)
with col1:
with st.expander("Word Stats"):
st.info("Word Statistics")
docx = nt.TextFrame(text)
st.write(docx.word_stats())
with st.expander("Top keywords"):
keywords = get_most_common_tokens(text)
st.write(keywords)
with st.expander("Sentiment"):
sent_result = get_semantics(text)
st.write(sent_result)
with col2:
with st.expander("Plot word freq"):
fig, ax = plt.subplots()
num_tokens = 10 # Adjust the number of tokens to display as desired
most_common_tokens = dict(token_analysis["Token"].value_counts().head(num_tokens))
sns.countplot(data=token_analysis[token_analysis["Token"].isin(most_common_tokens)], x="Token", ax=ax)
ax.set_xlabel('Token')
ax.set_ylabel('Frequency')
ax.tick_params(axis='x', rotation=45)
st.pyplot(fig)
with st.expander("Plot part of speech"):
fig, ax = plt.subplots()
most_common_tokens = dict(token_analysis["Position"].value_counts())
sns.countplot(data=token_analysis[token_analysis["Position"].isin(most_common_tokens)], x="Position", ax=ax)
ax.set_xlabel('PoS')
ax.set_ylabel('Frequency')
ax.tick_params(axis='x', rotation=45)
st.pyplot(fig)
with st.expander("Plot word cloud"):
plot_wordcloud(text)
with st.expander('Download Results'):
file_download(token_analysis)
|