-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
48 lines (36 loc) · 1.39 KB
/
main.py
File metadata and controls
48 lines (36 loc) · 1.39 KB
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
import json
from input_list import file_list
from stopwords import stopwords
from krwordrank.word import KRWordRank
from wordcloud import WordCloud
from PIL import Image
def generate_keywords(file_path):
texts = []
with open(file_path, encoding='utf-8') as json_file:
json_data = json.load(json_file)
json_comments_data = json_data["comments"]
for comments in json_comments_data:
if comments["headline"] != None: texts.append(comments["headline"])
if comments["review"] != None: texts.append(comments["review"])
wordrank_extractor = KRWordRank(min_count = 5, max_length = 10)
keywords, rank, graph = wordrank_extractor.extract(texts, beta = 0.85, max_iter = 10)
for word in stopwords:
keywords.pop(word)
return keywords
def generate_wordcloud(keywords):
wordcloud = WordCloud(
width = 800,
height = 800,
font_path = "fonts/JejuGothic.ttf",
background_color="white",
)
wordcloud = wordcloud.generate_from_frequencies(keywords)
image = Image.fromarray(wordcloud.to_array())
return image
if __name__ == "__main__":
for file_name in file_list:
keywords = generate_keywords("input/" + file_name + ".json")
image = generate_wordcloud(keywords)
## add result
image.save("result/" + file_name + ".jpeg")
print("image saved: " + file_name)