From 9be93ec7ee585797e4deaf625efde09402a7884c Mon Sep 17 00:00:00 2001 From: Liam Steckler Date: Sat, 3 May 2025 16:35:03 -0700 Subject: [PATCH] Better text cleanup for hashtags --- scl.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/scl.py b/scl.py index f230123..8f1452d 100644 --- a/scl.py +++ b/scl.py @@ -1,4 +1,5 @@ import io +import re from datetime import datetime from typing import Optional @@ -97,6 +98,10 @@ def classify_event_size(num_people: int) -> dict[str, str | bool]: } +def clean_hashtag_text(hashtag_text: str) -> str: + return re.sub(r"[\W^_]+", "", hashtag_text.title()) + + def get_hashtag_string(event) -> str: city = str() try: @@ -108,12 +113,12 @@ def get_hashtag_string(event) -> str: try: neighborhood = event["neighborhood"] if neighborhood != city: - neighborhood_text = " #{}".format(neighborhood.title().replace(" ", "")) + neighborhood_text = " #{}".format(clean_hashtag_text(neighborhood)) except KeyError: pass hashtag_string = "#SeattleCityLightOutage #SCLOutage{} #{}".format( - neighborhood_text, city.title().replace(" ", "") + neighborhood_text, clean_hashtag_text(city) ) return hashtag_string