-
Notifications
You must be signed in to change notification settings - Fork 0
/
classes.py
266 lines (193 loc) · 8.99 KB
/
classes.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
# import necessary libraries
import base64
from io import BytesIO
import requests
from bs4 import BeautifulSoup
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_agg import FigureCanvasAgg
from matplotlib.backends.backend_svg import FigureCanvasSVG
from wordcloud import WordCloud, ImageColorGenerator
from bidi.algorithm import get_display
import arabic_reshaper
from flask import Flask, Response, request
import io
class Wikipedia:
def __init__(self, url):
self.url = url
page = requests.get(self.url)
self.soup = BeautifulSoup(page.text, 'html.parser')
# Get all paragraphs
paragraphs = self.soup.find_all('p')
self.article_text = ""
for para in paragraphs:
self.article_text += para.text
# Get the title
self.articletitle = self.soup.find('title').get_text()
self.articletitle = get_display(arabic_reshaper.reshape(self.articletitle)) # For arabic font
def wordCounter(self):
# split() to slice the string and len() to count the items of the list
wordcount = len(self.article_text.split(" "))
return str(wordcount)+" Words"
def links(self):
links = self.soup.find_all('a')
self.linksText = []
# extract the links
for link in links:
if str(link.get('href')).startswith("http"):
self.linksText.append(link.get('href'))
return "\n".join(self.linksText)
def process_text(self):
with open('data/stops_en.txt', 'r', encoding="utf-8") as myfile:
stopwords_en = myfile.read().split("\n")
with open('data/stops_de.txt', 'r', encoding="utf-8") as myfile:
stopwords_de = myfile.read().split("\n")
with open('data/stops_ar.txt', 'r', encoding="utf-8") as myfile:
stopwords_ar = myfile.read().split("\n")
# remove \n \t
text = self.article_text.replace("\n", " ").replace("\t", " ")
# lowercase words
text = text.lower()
# split the text to a list of words
wordlist = text.split(" ")
# remove stopwords
stops = [word for word in wordlist if (word not in stopwords_en) and (word not in stopwords_de)and (word not in stopwords_ar)]
# to remove the punctuation we will loop on every charachter and check it with isalnum()
# isalnum() retrun Alphabetic charachters and numbers
puncts = "".join([char for char in " ".join(stops) if char.isalnum() or char == " "]).strip()
# remove words less than 4 charachters
tokenized = [word for word in puncts.split() if len(word) > 3]
processed = " ".join(tokenized)
data = get_display(arabic_reshaper.reshape(processed)) # For arabic font
return data
def BagOfWords(self, freq=10):
tokenized = self.process_text().split()
# create a dictionary with key = word and value = frequency of a word
dic = {}
for word in tokenized:
if word in dic:
dic[word] += 1
else:
dic[word] = 1
sorted_dic = {}
for key, value in sorted(dic.items(), key=lambda x:x[1], reverse=True):
sorted_dic.update({key:value})
counter = {word:sorted_dic[word] for word in sorted_dic.keys() if sorted_dic[word] >= freq }
return counter
def wordcloud(self, cmap='viridis'):
text = self.process_text()
# Create and generate a word cloud image:
wordcloud = WordCloud(font_path='static/fonts/arial.ttf',
width = 800, height = 800,
background_color="white",
colormap= cmap, include_numbers=True).generate(text)
fig, ax = plt.subplots()
fig.set_size_inches(10.5, 10.5)
ax.imshow(wordcloud, interpolation='bilinear')
ax.axis("off")
ax.set_title("Word Cloud - "+self.articletitle, fontsize= 15)
output = io.BytesIO()
FigureCanvasAgg(fig).print_png(output)
return Response(output.getvalue(), mimetype="image/png")
def boxplot(self,freq=1, style='seaborn', color= '#333399',
title='Horizontal Bar Chart - Word Frequency'):
bow = self.BagOfWords(freq)
word,count = zip(*bow.items())
plt.style.use(style)
fig, ax = plt.subplots()
fig.set_size_inches(10, 10)
plt.subplots_adjust(bottom=0.3)
ax.set_ylabel('Words Count')
ax.set_title(title)
#labels = ax.get_xticklabels()
#plt.setp(labels, rotation=90, horizontalalignment='right')
box = ax.boxplot(count, patch_artist=True, vert=False,widths = 0.6)
colors = [color]
for p, c in zip(box['boxes'], colors):
p.set_facecolor(c)
output = io.BytesIO()
FigureCanvasAgg(fig).print_png(output)
return Response(output.getvalue(), mimetype="image/png")
def lineplot(self,freq=1, style='seaborn', color= '#333399',
title='Horizontal Bar Chart - Word Frequency'):
bow = self.BagOfWords(freq)
word,count = zip(*bow.items())
plt.style.use(style)
fig, ax = plt.subplots()
fig.set_size_inches(15, 7.5)
plt.subplots_adjust(bottom=0.3)
ax.set_ylabel('Words Count')
ax.set_title(title)
fig.suptitle('This is the figure title', fontsize=10)
labels = ax.get_xticklabels()
plt.setp(labels, rotation=90, horizontalalignment='right')
word = [w.capitalize() for w in word]
ax.plot(word, count, color=color)
#ax.hist(count, color=color, bins = 300, density=True)
output = io.BytesIO()
FigureCanvasAgg(fig).print_png(output)
return Response(output.getvalue(), mimetype="image/png")
def barplot(self, freq=20, style='seaborn', color= '#333399',
title='Horizontal Bar Chart - Word Frequency'):
bow = self.BagOfWords(freq)
word,count = zip(*bow.items())
plt.style.use(style)
fig, ax = plt.subplots()
fig.set_size_inches(10, 10)
plt.subplots_adjust(bottom=0.3)
ax.set_ylabel('Words Count')
ax.set_title(title)
labels = ax.get_xticklabels()
plt.setp(labels, rotation=90, horizontalalignment='right')
ax.bar(word, count, align='center', color=color)
output = io.BytesIO()
FigureCanvasAgg(fig).print_png(output)
return Response(output.getvalue(), mimetype="image/png")
def barplot_h(self, freq=20, style='seaborn', color= '#333399',
title='Horizontal Bar Chart - Word Frequency'):
bow = self.BagOfWords(freq)
word,count = zip(*bow.items())
plt.style.use(style)
fig, ax = plt.subplots()
fig.set_size_inches(10, 10)
plt.subplots_adjust(bottom=0.3)
ax.set_xlabel('Words Count')
ax.set_title(title)
labels = ax.get_yticklabels()
ax.barh(word, count, align='center', color=color)
ax.invert_yaxis() # labels read top-to-bottom
output = io.BytesIO()
FigureCanvasAgg(fig).print_png(output)
return Response(output.getvalue(), mimetype="image/png")
def pieplot(self,freq=20, style='seaborn',
title='Pie Chart - Word Frequency'):
bow = self.BagOfWords(freq)
word,count = zip(*bow.items())
plt.style.use(style)
explode = np.zeros(len(word))
explode[0:3] = [0.1,0.05,0.025]
fig, ax = plt.subplots()
fig.set_size_inches(10, 10)
ax.pie(count, labels=word, radius=1, autopct='%1.1f%%',
textprops={'fontsize': 12},
startangle=90, shadow=True, explode=explode)
ax.set_title(title)
ax.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
plt.subplots_adjust(left=0.2, bottom=0.2)
#ax.legend()
output = io.BytesIO()
FigureCanvasAgg(fig).print_png(output)
return Response(output.getvalue(), mimetype="image/png")
def table(self, freq=10, title="Table"):
bow = self.BagOfWords(freq)
data = np.array(list(bow.items()))
fig, ax =plt.subplots()
collabel=("Word", "Frequency - Word Frequency")
#ax.axis('tight')
ax.axis('off')
ax.table(cellText=data,colLabels=collabel, loc='center', cellLoc='left') #,loc='center',cellLoc='left', colWidths = [2, 1]
ax.set_title(title)
output = io.BytesIO()
FigureCanvasAgg(fig).print_png(output)
return Response(output.getvalue(), mimetype="image/png")