-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcloud.py
94 lines (74 loc) · 2.15 KB
/
cloud.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
#!/usr/bin/env python
import os
import sys
import getopt
import numpy as np
import matplotlib.pyplot as plt
from os import path
from PIL import Image
from nltk.corpus import stopwords
from wordcloud import WordCloud
dir = path.dirname(__file__) if "__file__" in locals() else os.getcwd()
def makeWordCloud(inputfile, maskfile=None):
# Read the whole text.
text = open(path.join(dir, inputfile)).read()
# read the mask image
# taken from
# http://www.stencilry.org/stencils/movies/alice%20in%20wonderland/255fk.jpg
image_mask = None
width = 800
height = 800
if(maskfile is not None):
image_mask = np.array(Image.open(path.join(dir, maskfile)))
stopWords = set(stopwords.words('spanish'))
wc = WordCloud(
background_color="white",
max_words=2000,
mask=image_mask,
stopwords=stopWords,
contour_width=3,
contour_color='steelblue',
width=width,
height=height
)
# generate word cloud
wc.generate(text)
# store to file
basename = path.basename(path.splitext(inputfile)[0])
if(maskfile is None):
filename = "cloud.png"
else :
filename = "cloud-mask.png"
outputFilename = path.join(dir, "output", basename, filename)
outputFolder = os.path.dirname(outputFilename)
if not os.path.exists(outputFolder):
os.makedirs(outputFolder)
wc.to_file(outputFilename)
def printCmd():
print('cloud.py -i <input> -m <mask>')
def main(argv):
inputfile = ''
maskfile = None
try:
opts, args = getopt.getopt(argv, "hi:m:", [
"input=",
"mask=",
])
except getopt.GetoptError:
printCmd()
sys.exit(2)
if len(opts) < 1:
printCmd()
sys.exit()
else:
for opt, arg in opts:
if opt == '-h':
printCmd()
sys.exit()
elif opt in ("-i", "--input"):
inputfile = arg.strip()
elif opt in ("-m", "--mask"):
maskfile = arg.strip()
return makeWordCloud(inputfile, maskfile)
if __name__ == "__main__":
main(sys.argv[1:])