|
6 | 6 |
|
7 | 7 | import base64 |
8 | 8 | import io |
9 | | -from functools import lru_cache |
10 | | -from urllib.request import urlopen |
| 9 | +from functools import cache |
11 | 10 |
|
12 | 11 | import dash |
13 | 12 | import dash_bootstrap_components as dbc |
14 | 13 | from dash import Input, Output, dcc, html |
15 | 14 | from wordcloud import WordCloud |
16 | 15 |
|
17 | | -BASE_URL = "https://cdn.opensource.faculty.ai/wordcloud" |
18 | | - |
19 | | -DOCUMENT_URLS = { |
20 | | - "midsummer": f"{BASE_URL}/a-midsummer-nights-dream.txt", |
21 | | - "venice": f"{BASE_URL}/the-merchant-of-venice.txt", |
22 | | - "randj": f"{BASE_URL}/romeo-and-juliet.txt", |
| 16 | +DOCUMENT_PATHS = { |
| 17 | + "midsummer": "data/a-midsummer-nights-dream.txt", |
| 18 | + "venice": "data/the-merchant-of-venice.txt", |
| 19 | + "randj": "data/romeo-and-juliet.txt", |
23 | 20 | } |
24 | 21 |
|
25 | 22 |
|
26 | 23 | # use lru_cache to memoise the frequencies |
27 | | -@lru_cache(maxsize=3) |
| 24 | +@cache |
28 | 25 | def load_word_frequencies(book): |
29 | | - url = DOCUMENT_URLS[book] |
| 26 | + path = DOCUMENT_PATHS[book] |
30 | 27 | WC = WordCloud(width=1000, height=600) |
31 | | - with urlopen(url) as f: |
32 | | - text = f.read().decode("utf-8") |
| 28 | + with open(path) as f: |
| 29 | + text = f.read() |
33 | 30 | return WC.process_text(text) |
34 | 31 |
|
35 | 32 |
|
@@ -104,6 +101,7 @@ def load_word_frequencies(book): |
104 | 101 | ], |
105 | 102 | ) |
106 | 103 | def make_wordcloud(book, min_freq, max_vocab): |
| 104 | + print(book) |
107 | 105 | # filter frequencies based on min_freq and max_vocab |
108 | 106 | sorted_frequencies = sorted( |
109 | 107 | load_word_frequencies(book).items(), key=lambda x: x[1], reverse=True |
|
0 commit comments