-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
55 lines (49 loc) · 1.95 KB
/
main.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
import wordcloud
import numpy as np
from matplotlib import pyplot as plt
from IPython.display import display
import fileupload
import io
import sys
import re
def _upload():
filename = "words.txt"
global file_contents
with open(filename) as f:
content = f.readlines()
file_contents = content
_upload()
def calculate_frequencies(file_contents):
# Here is a list of punctuations and uninteresting words you can use to process your text
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
uninteresting_words = ["the", "a", "to", "if", "is", "it", "of", "and", "or", "an", "as", "i", "me", "my", \
"we", "our", "ours", "you", "your", "yours", "he", "she", "him", "his", "her", "hers", "its", "they", "them", \
"their", "what", "which", "who", "whom", "this", "that", "am", "are", "was", "were", "be", "been", "being", \
"have", "has", "had", "do", "does", "did", "but", "at", "by", "with", "from", "here", "when", "where", "how", \
"all", "any", "both", "each", "few", "more", "some", "such", "no", "nor", "too", "very", "can", "will", "just"]
# LEARNER CODE START HERE
words = {}
for word in file_contents:
if word in uninteresting_words:
pass
else:
if word.isalpha():
if word.lower() in words:
words[word.lower()] += 1
elif word.lower() not in words:
words[word.lower()] = 1
else:
stripped = re.sub(r'\W+', '', word)
stripped = stripped.lower()
if stripped.lower() in words:
words[stripped.lower()] += 1
else:
words[stripped.lower()] = 1
#wordcloud
cloud = wordcloud.WordCloud()
cloud.generate_from_frequencies(words)
return cloud.to_array()
myimage = calculate_frequencies(file_contents)
plt.imshow(myimage, interpolation = 'nearest')
plt.axis('off')
plt.show()