-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloud.py
executable file
·39 lines (30 loc) · 962 Bytes
/
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
#!python3
import typer
from loguru import logger
from icecream import ic
from wordcloud import WordCloud
from openai_wrapper import get_text_from_path_or_stdin
app = typer.Typer(no_args_is_help=True)
@app.command()
def dump_cloud(path: str = "", top: int = 40):
wc = WordCloud(min_word_length=3)
raw_words = get_text_from_path_or_stdin(path)
# create dict of word frequencies
words = wc.process_text(raw_words)
# make tuples from words and frequencies
word_freqs = list(words.items())
# sort by frequency
word_freqs.sort(key=lambda x: x[1], reverse=True)
word_freqs = word_freqs[:top]
# ic(word_freqs)
# normalize the frequencies
sum_freqs = sum([freq for word, freq in word_freqs])
norm_freqs = [
(word, round(100 * (freq / sum_freqs), 2)) for word, freq in word_freqs
]
ic(norm_freqs)
@logger.catch()
def app_wrap_loguru():
app()
if __name__ == "__main__":
app_wrap_loguru()