unit tests fixed

This commit is contained in:
Grégoire Mureau 2025-07-16 11:39:34 +02:00
parent 98f9dd1729
commit 3a7e603952
2 changed files with 15 additions and 7 deletions

View file

@ -28,6 +28,9 @@ def preprocess_tweet(tweet):
if not isinstance(tweet, str): if not isinstance(tweet, str):
return "" return ""
# Decode HTML entities (e.g., & → &)
tweet = html.unescape(tweet)
# Convert text to lowercase # Convert text to lowercase
tweet = tweet.lower() tweet = tweet.lower()
@ -40,14 +43,11 @@ def preprocess_tweet(tweet):
# Separate hashtags from the word to preserve semantics during tokenization # Separate hashtags from the word to preserve semantics during tokenization
tweet = re.sub(r'#(\w+)', r'# \1', tweet) tweet = re.sub(r'#(\w+)', r'# \1', tweet)
# Decode HTML entities (e.g., & → &)
tweet = html.unescape(tweet)
# Remove unwanted punctuation and special characters (except useful ones like < > @ # ! ?) # Remove unwanted punctuation and special characters (except useful ones like < > @ # ! ?)
tweet = re.sub(r'[^\w\s<>@#!?]', '', tweet) tweet = re.sub(r'[^\w\s<>@#!?]', '', tweet)
# Remove non-printable/control characters (e.g., \n, \t, emojis, corrupted unicode) # Remove non-printable/control characters (e.g., \n, \t, emojis, corrupted unicode)
# tweet = re.sub(r'[^\x20-\x7E]', '', tweet) tweet = re.sub(r'[^\x20-\x7E]', '', tweet)
# Tokenize the tweet into individual words # Tokenize the tweet into individual words
tokens = word_tokenize(tweet) tokens = word_tokenize(tweet)

View file

@ -8,10 +8,10 @@ from preprocessing import preprocess_tweet
def test_preprocess_tweet_removes_hashtags(): def test_preprocess_tweet_removes_hashtags():
tweet = """ tweet = """
Just flew w/ @AirParadis &#128640; &amp; I&#8217;m in LOVE! &Eacute;pic service, comfy seats. Just flew w/ @AirParadis &#128640; &amp; I&#8217;m in LOVE! Epic service, comfy seats.
#bestflight #AirParadis #bestflight #AirParadis
""" """
assert preprocess_tweet(tweet) == "flew w < MENTION > im love ! épic service comfy seat # bestflight # airparadis" assert preprocess_tweet(tweet) == "flew w < MENTION > im love ! epic service comfy seat # bestflight # airparadis"
def test_preprocess_tweet_removes_mentions(): def test_preprocess_tweet_removes_mentions():
tweet = """ tweet = """
@ -25,4 +25,12 @@ def test_preprocess_tweet_lowercases_text():
My 3rd time w/ @AirParadis &amp; still amazed &#128525; Crew = lovely &agrave; every step! My 3rd time w/ @AirParadis &amp; still amazed &#128525; Crew = lovely &agrave; every step!
#flyhappy #AirParadis #flyhappy #AirParadis
""" """
assert preprocess_tweet(tweet) == '3rd time w < MENTION > still amazed crew lovely à every step ! # flyhappy # airparadis' assert preprocess_tweet(tweet) == '3rd time w < MENTION > still amazed crew lovely à every step ! # flyhappy # airparadis'
tweet = """
Just flew w/ @AirParadis &#128640; &amp; I&#8217;m in LOVE! Epic service, comfy seats.
#bestflight #AirParadis
"""
preprocess_tweet(tweet)