-
Notifications
You must be signed in to change notification settings - Fork 8
/
text_analysis.py
165 lines (126 loc) 路 4.1 KB
/
text_analysis.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
# File containing all the analysis functions for the web app
# Standard Libraries
import os
import re
import string
import numpy as np
from collections import Counter
# Text Processing Library
import nltk
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
from nltk.stem import WordNetLemmatizer
from nltk.util import ngrams
from textblob import TextBlob
from wordcloud import WordCloud
from gensim import utils
import streamlit as st
import pprint
import gensim
import gensim.downloader as api
import warnings
import spacy
from spacy import displacy
from pathlib import Path
from spacy.matcher import PhraseMatcher, Matcher
from spacy.tokens import Span
import tempfile
warnings.filterwarnings(action='ignore')
# Data Visualisation
import matplotlib.pyplot as plt
import seaborn as sns
import spacy_streamlit
from PIL import Image
# Constants
STOPWORDS = stopwords.words('english')
STOPWORDS + ['said']
# Text cleaning function
def clean_text(text):
'''
Function which returns a clean text
'''
# Lower case
text = text.lower()
# Remove numbers
text = re.sub(r'\d', '', text)
# Replace \n and \t functions
text = re.sub(r'\n', '', text)
text = text.strip()
# Remove punctuations
text = text.translate(str.maketrans('', '', string.punctuation))
# Remove Stopwords and Lemmatise the data
lemmatizer = WordNetLemmatizer()
text = [lemmatizer.lemmatize(word) for word in text.split() if word not in STOPWORDS]
text = ' '.join(text)
return text
# Create a word cloud function
def create_wordcloud(text, image_path = None):
'''
Pass a string to the function and output a word cloud
ARGS
text: The text for wordcloud
image_path (optional): The image mask with a white background (default None)
'''
st.write('Creating Word Cloud..')
text = clean_text(text)
if image_path == None:
# Generate the word cloud
wordcloud = WordCloud(width = 600, height = 600,
background_color ='white',
stopwords = STOPWORDS,
min_font_size = 10).generate(text)
else:
mask = np.array(Image.open(image_path))
wordcloud = WordCloud(width = 600, height = 600,
background_color ='white',
stopwords = STOPWORDS,
mask=mask,
min_font_size = 5).generate(text)
# plot the WordCloud image
plt.figure(figsize = (8, 8), facecolor = None)
plt.imshow(wordcloud, interpolation = 'nearest')
plt.axis("off")
plt.tight_layout(pad = 0)
plt.show()
# Function to plot the ngrams based on n and top k value
def plot_ngrams(text, n=2, topk=15):
'''
Function to plot the most commonly occuring n-grams in bar plots
ARGS
text: data to be enterred
n: n-gram parameters
topk: the top k phrases to be displayed
'''
st.write('Creating N-Gram Plot..')
text = clean_text(text)
tokens = text.split()
# get the ngrams
ngram_phrases = ngrams(tokens, n)
# Get the most common ones
most_common = Counter(ngram_phrases).most_common(topk)
# Make word and count lists
words, counts = [], []
for phrase, count in most_common:
word = ' '.join(phrase)
words.append(word)
counts.append(count)
# Plot the barplot
plt.figure(figsize=(10, 6))
title = "Most Common " + str(n) + "-grams in the text"
plt.title(title)
ax = plt.bar(words, counts)
plt.xlabel("n-grams found in the text")
plt.ylabel("Ngram frequencies")
plt.xticks(rotation=90)
plt.show()
# Function to return POS tags of a sentence
def pos_tagger(s):
# Define the tag dictionary
output = ''
# Remove punctuations
s = s.translate(str.maketrans('', '', string.punctuation))
tagged_sentence = nltk.pos_tag(nltk.word_tokenize(s))
for tag in tagged_sentence:
out = tag[0] + ' ---> ' + tag[1] + '<br>'
output += out
return output