-
Notifications
You must be signed in to change notification settings - Fork 1
/
wordcloud2_specific_author.py
155 lines (116 loc) · 3.92 KB
/
wordcloud2_specific_author.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
# importing the necessery modules
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import csv
data_source = 'scraped_articles_DATE.csv'
COMMON_STOPWORDS = ["the", "and","but", "if", "to", "of", "at", "with",
"we", "it", "on", "in", "from", "for", "her", "him",
"up", "just", "had", "they", "that", "said", "got", "did",
"really", "their", "or", "who", "is", "its", "be", "am", "are"
"was", "about", "so", "also", "this", "you", "would", "she",
"could", "can", "he", "has", "do"]
def get_content_at_line(line):
'''
Returns: a list of all the urls of articles from huntington news,
read from a preexisting csv file
'''
text = ''
with open('scraped_articles_DATE.csv', encoding="ISO-8859-1") as file:
csv_reader = csv.reader(file)
# skip the headers
next(csv_reader, None)
counter = 2
for row in csv_reader:
url = row[0]
headline = row[1]
byline = row[2]
date = row[3]
content = row[4]
if counter == line:
return content
counter += 1
return ''
def get_discography(author):
'''
Returns: a list of all the urls of articles from huntington news,
read from a preexisting csv file
'''
text = ''
with open('scraped_articles_DATE.csv', encoding="ISO-8859-1") as file:
csv_reader = csv.reader(file)
# skip the headers
next(csv_reader, None)
temporary = ''
for row in csv_reader:
url = row[0]
headline = row[1]
byline = row[2]
date = row[3]
content = row[4]
if author.lower() in byline.lower():
temporary += content
return temporary
def multiple_lines(start, end, csv_name):
'''
Returns: a list of all the urls of articles from huntington news,
read from a preexisting csv file
'''
text = ''
with open(csv_name, encoding="ISO-8859-1") as file:
csv_reader = csv.reader(file)
# skip the headers
next(csv_reader, None)
temporary = ''
counter = 2
selected = False
for row in csv_reader:
url = row[0]
headline = row[1]
byline = row[2]
date = row[3]
content = row[4]
if counter == start:
selected = True
print('counter', counter)
if selected == True:
temporary += content
print('count', counter)
if counter == end:
selected = False
counter += 1
return temporary
def do_wordcloud(wordswordswords):
wordcloud = WordCloud(width=480, height=480,
stopwords = COMMON_STOPWORDS).generate(wordswordswords)
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.margins(x=0, y=0)
plt.show()
#Uncomment the appropriate chunk of code as well as the plt function at the end in order to generate word-cloud
'''
line = int(input('Which line? '))
article = get_content_at_line(line)
wordcloud = WordCloud(width=480, height=480,
stopwords = COMMON_STOPWORDS).generate(article)
'''
'''
#author = input('Which author? ')
#one_author = get_discography(author).lower()
#wordcloud = WordCloud(width=480, height=480,
#stopwords = COMMON_STOPWORDS).generate(one_author)
'''
'''
start = int(input('Enter start: '))
end = int(input('Enter end: '))
section = multiple_lines(start, end, data_source).lower()
wordcloud = WordCloud(width=480, height=480,
stopwords = COMMON_STOPWORDS).generate(section)
'''
'''
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.margins(x=0, y=0)
plt.show()
'''