-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheasy-wordcloud.py
More file actions
243 lines (214 loc) · 6.29 KB
/
easy-wordcloud.py
File metadata and controls
243 lines (214 loc) · 6.29 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
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
import glob
import os
import re
from collections import Counter
import click
import numpy as np
from PIL import Image
from wordcloud import WordCloud, STOPWORDS
# Default filter terms to exclude
DEFAULT_FILTER_TERMS = [
"from",
"sent",
"to",
"subject",
"attachments",
"call",
"re",
"fw",
"no",
"pm",
"co",
"wrote",
"message",
"forwarded",
"date",
"recipients",
]
def load_mask_image(mask_filename: str):
try:
mask_image = np.array(Image.open(mask_filename))
return mask_image
except Exception as e:
click.echo(f"[!] Error loading mask image '{mask_filename}': {e}", err=True)
return None
def read_all_txt(folder_path: str, pattern: str = "*.txt") -> str:
text = ""
glob_pattern = os.path.join(folder_path, pattern)
files = glob.glob(glob_pattern)
if not files:
click.echo(f"[!] No files matched pattern: {glob_pattern}", err=True)
for file_path in files:
click.echo(f"[*] Reading: {file_path}")
try:
with open(file_path, "r", encoding="utf-8") as f:
text += f.read() + " "
except UnicodeDecodeError:
click.echo(
f"[!] Skipping {file_path}: Unable to decode with UTF-8.",
err=True,
)
continue
click.echo(f"[*] Total text length: {len(text)}")
return text
def generate_and_save_wordcloud(
text: str,
stopwords_enabled: bool,
image_filename: str,
txt_filename: str,
filter_terms,
mask_image,
max_words: int,
):
# Extract only alphabetic words
words = re.findall(r"\b[a-zA-Z]+\b", text.lower())
# Build set of words to remove
if stopwords_enabled:
blocked = set(STOPWORDS) | set(filter_terms)
else:
blocked = set(filter_terms)
# Filter words
words = [w for w in words if w not in blocked and len(w) > 1]
if not words:
click.echo("[!] No valid words left after filtering. Skipping.", err=True)
return
word_counts = Counter(words)
# Save frequencies to txt
with open(txt_filename, "w", encoding="utf-8") as txt_file:
for word, count in word_counts.items():
txt_file.write(f"{word}: {count}\n")
# Build word cloud
wc = WordCloud(
width=1920,
height=1080,
background_color="black",
colormap="twilight",
stopwords=STOPWORDS if stopwords_enabled else set(),
max_words=max_words,
max_font_size=100,
min_font_size=10,
contour_color="white",
contour_width=2,
relative_scaling=0.5,
normalize_plurals=False,
mask=mask_image,
)
cloud = wc.generate_from_frequencies(word_counts)
# Save image
cloud.to_file(image_filename)
click.echo(f"[+] Saved {image_filename} and {txt_filename}")
@click.command()
@click.option(
"-f",
"--folder",
"folder_path",
type=click.Path(exists=True, file_okay=False, readable=True),
default=".",
show_default=True,
help="Folder containing the text file(s).",
)
@click.option(
"-p",
"--pattern",
"filename_pattern",
default="*.txt",
show_default=True,
help="Glob pattern for input text files inside the folder.",
)
@click.option(
"-m",
"--mask",
"mask_filename",
type=click.Path(exists=True, dir_okay=False, readable=True),
default="mask.png",
show_default=True,
help="Path to mask image (PNG).",
)
@click.option(
"-o",
"--output-dir",
"output_dir",
type=click.Path(file_okay=False, dir_okay=True),
default=".",
show_default=True,
help="Directory to save output images and txt files.",
)
@click.option(
"--max-words",
type=int,
default=150,
show_default=True,
help="Maximum number of words in the word cloud.",
)
@click.option(
"--filter-term",
"-t",
"extra_filter_terms",
multiple=True,
help="Additional word(s) to filter out (can be passed multiple times).",
)
@click.option(
"--include-stopwords",
is_flag=True,
help="Only generate a word cloud that includes stopwords.",
)
@click.option(
"--exclude-stopwords",
is_flag=True,
help="Only generate a word cloud that excludes stopwords.",
)
def cli(
folder_path,
filename_pattern,
mask_filename,
output_dir,
max_words,
extra_filter_terms,
include_stopwords,
exclude_stopwords,
):
# Ensure output directory exists
os.makedirs(output_dir, exist_ok=True)
# Load mask image
mask_image = load_mask_image(mask_filename)
if mask_image is None:
raise click.ClickException("Failed to load mask image. Exiting.")
# Read text
text = read_all_txt(folder_path, pattern=filename_pattern)
if not text.strip():
raise click.ClickException("No valid text found in input files. Exiting.")
# Combine default + extra filter terms
filter_terms = DEFAULT_FILTER_TERMS + list(extra_filter_terms)
# Decide what to generate:
# - If neither include nor exclude is specified: generate both
# - If one is specified: generate only that one
generate_with = include_stopwords or not (include_stopwords or exclude_stopwords)
generate_without = exclude_stopwords or not (include_stopwords or exclude_stopwords)
if generate_with:
img_path = os.path.join(output_dir, "image_with_stopwords.png")
txt_path = os.path.join(output_dir, "words_with_stopwords.txt")
click.echo("[*] Generating word cloud WITH stopwords...")
generate_and_save_wordcloud(
text=text,
stopwords_enabled=True,
image_filename=img_path,
txt_filename=txt_path,
filter_terms=filter_terms,
mask_image=mask_image,
max_words=max_words,
)
if generate_without:
img_path = os.path.join(output_dir, "image_without_stopwords.png")
txt_path = os.path.join(output_dir, "words_without_stopwords.txt")
click.echo("[*] Generating word cloud WITHOUT stopwords...")
generate_and_save_wordcloud(
text=text,
stopwords_enabled=False,
image_filename=img_path,
txt_filename=txt_path,
filter_terms=filter_terms,
mask_image=mask_image,
max_words=max_words,
)
if __name__ == "__main__":
cli()