Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -171,9 +171,8 @@ def build_circular_wordcloud_html(frequencies, max_words=75, width=760, height=5
|
|
| 171 |
sorted_words = sorted(frequencies.items(), key=lambda item: (-item[1], item[0]))[:max_words]
|
| 172 |
max_count = sorted_words[0][1]
|
| 173 |
min_count = sorted_words[-1][1]
|
| 174 |
-
palette = ["#
|
| 175 |
cx, cy = width / 2, height / 2
|
| 176 |
-
radius = min(width, height) / 2 - 16
|
| 177 |
placed_rects = []
|
| 178 |
svg_text_nodes = []
|
| 179 |
|
|
@@ -195,54 +194,48 @@ def build_circular_wordcloud_html(frequencies, max_words=75, width=760, height=5
|
|
| 195 |
|
| 196 |
def overlaps(rect):
|
| 197 |
x, y, w, h = rect
|
| 198 |
-
padding =
|
| 199 |
for ox, oy, ow, oh in placed_rects:
|
| 200 |
if not (x + w + padding < ox or ox + ow + padding < x or y + h + padding < oy or oy + oh + padding < y):
|
| 201 |
return True
|
| 202 |
return False
|
| 203 |
|
| 204 |
-
def
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
(x + w - 2, y + 2),
|
| 208 |
-
(x + 2, y + h - 2),
|
| 209 |
-
(x + w - 2, y + h - 2),
|
| 210 |
-
]
|
| 211 |
-
for px, py in test_points:
|
| 212 |
-
if math.hypot(px - cx, py - cy) > radius:
|
| 213 |
-
return False
|
| 214 |
-
return True
|
| 215 |
|
| 216 |
for index, (word, count) in enumerate(sorted_words):
|
| 217 |
if max_count == min_count:
|
| 218 |
-
font_size =
|
| 219 |
else:
|
| 220 |
ratio = (count - min_count) / (max_count - min_count)
|
| 221 |
-
|
|
|
|
|
|
|
| 222 |
|
| 223 |
color = palette[index % len(palette)]
|
| 224 |
text_units = estimate_text_units(word)
|
| 225 |
-
text_width = max(
|
| 226 |
text_height = max(24, font_size * 1.22)
|
| 227 |
|
| 228 |
placed = False
|
| 229 |
-
for step in range(1,
|
| 230 |
-
angle = step * 0.
|
| 231 |
-
spiral_radius =
|
| 232 |
x = cx + spiral_radius * math.cos(angle) - text_width / 2
|
| 233 |
y = cy + spiral_radius * math.sin(angle) - text_height / 2
|
| 234 |
rect = (x, y, text_width, text_height)
|
| 235 |
-
if not
|
| 236 |
continue
|
| 237 |
if overlaps(rect):
|
| 238 |
continue
|
| 239 |
|
| 240 |
placed_rects.append(rect)
|
| 241 |
safe_word = html.escape(word)
|
| 242 |
-
tx, ty = x +
|
| 243 |
svg_text_nodes.append(
|
| 244 |
f"<text x='{tx:.1f}' y='{ty:.1f}' fill='{color}' font-size='{font_size}' "
|
| 245 |
-
f"font-weight='
|
| 246 |
)
|
| 247 |
placed = True
|
| 248 |
break
|
|
@@ -251,10 +244,10 @@ def build_circular_wordcloud_html(frequencies, max_words=75, width=760, height=5
|
|
| 251 |
continue
|
| 252 |
|
| 253 |
return (
|
| 254 |
-
"<div style='padding:
|
| 255 |
f"<svg viewBox='0 0 {width} {height}' style='width:100%; height:auto; display:block;' "
|
| 256 |
"xmlns='http://www.w3.org/2000/svg'>"
|
| 257 |
-
|
| 258 |
+ "".join(svg_text_nodes) +
|
| 259 |
"</svg></div>"
|
| 260 |
)
|
|
|
|
| 171 |
sorted_words = sorted(frequencies.items(), key=lambda item: (-item[1], item[0]))[:max_words]
|
| 172 |
max_count = sorted_words[0][1]
|
| 173 |
min_count = sorted_words[-1][1]
|
| 174 |
+
palette = ["#00695C", "#0077B6", "#0B3D91", "#1F8EFA", "#A3CFE2"]
|
| 175 |
cx, cy = width / 2, height / 2
|
|
|
|
| 176 |
placed_rects = []
|
| 177 |
svg_text_nodes = []
|
| 178 |
|
|
|
|
| 194 |
|
| 195 |
def overlaps(rect):
|
| 196 |
x, y, w, h = rect
|
| 197 |
+
padding = 2
|
| 198 |
for ox, oy, ow, oh in placed_rects:
|
| 199 |
if not (x + w + padding < ox or ox + ow + padding < x or y + h + padding < oy or oy + oh + padding < y):
|
| 200 |
return True
|
| 201 |
return False
|
| 202 |
|
| 203 |
+
def is_inside_canvas(x, y, w, h):
|
| 204 |
+
margin = 10
|
| 205 |
+
return x >= margin and y >= margin and (x + w) <= (width - margin) and (y + h) <= (height - margin)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
|
| 207 |
for index, (word, count) in enumerate(sorted_words):
|
| 208 |
if max_count == min_count:
|
| 209 |
+
font_size = 28
|
| 210 |
else:
|
| 211 |
ratio = (count - min_count) / (max_count - min_count)
|
| 212 |
+
eased_ratio = ratio ** 0.85
|
| 213 |
+
font_size = int(18 + eased_ratio * 86)
|
| 214 |
+
font_size = min(font_size, 140)
|
| 215 |
|
| 216 |
color = palette[index % len(palette)]
|
| 217 |
text_units = estimate_text_units(word)
|
| 218 |
+
text_width = max(32, font_size * (text_units + 0.62))
|
| 219 |
text_height = max(24, font_size * 1.22)
|
| 220 |
|
| 221 |
placed = False
|
| 222 |
+
for step in range(1, 3200):
|
| 223 |
+
angle = step * 0.4 + index * 0.18
|
| 224 |
+
spiral_radius = 2 + step * 0.42
|
| 225 |
x = cx + spiral_radius * math.cos(angle) - text_width / 2
|
| 226 |
y = cy + spiral_radius * math.sin(angle) - text_height / 2
|
| 227 |
rect = (x, y, text_width, text_height)
|
| 228 |
+
if not is_inside_canvas(x, y, text_width, text_height):
|
| 229 |
continue
|
| 230 |
if overlaps(rect):
|
| 231 |
continue
|
| 232 |
|
| 233 |
placed_rects.append(rect)
|
| 234 |
safe_word = html.escape(word)
|
| 235 |
+
tx, ty = x + 1.5, y + text_height * 0.84
|
| 236 |
svg_text_nodes.append(
|
| 237 |
f"<text x='{tx:.1f}' y='{ty:.1f}' fill='{color}' font-size='{font_size}' "
|
| 238 |
+
f"font-weight='800' letter-spacing='-0.01em'>{safe_word}</text>"
|
| 239 |
)
|
| 240 |
placed = True
|
| 241 |
break
|
|
|
|
| 244 |
continue
|
| 245 |
|
| 246 |
return (
|
| 247 |
+
"<div style='padding:10px; border:1px solid #e9e9e9; border-radius:10px; background:#f3f5f7;'>"
|
| 248 |
f"<svg viewBox='0 0 {width} {height}' style='width:100%; height:auto; display:block;' "
|
| 249 |
"xmlns='http://www.w3.org/2000/svg'>"
|
| 250 |
+
"<rect x='0' y='0' width='100%' height='100%' fill='#f3f5f7' />"
|
| 251 |
+ "".join(svg_text_nodes) +
|
| 252 |
"</svg></div>"
|
| 253 |
)
|