Spaces:
Running
Running
File size: 9,293 Bytes
d4ef46b a092d54 e1645d7 228859a a092d54 e1645d7 a092d54 2c1f9dd 1414454 2c1f9dd a092d54 e1645d7 2c1f9dd a092d54 2c1f9dd e1645d7 2c1f9dd 228859a 2c1f9dd 228859a 2c1f9dd e1645d7 447f97e e1645d7 447f97e 2c1f9dd 228859a 2c1f9dd e1645d7 2c1f9dd 228859a e1645d7 447f97e 2c1f9dd 228859a 2c1f9dd 447f97e 2c1f9dd 228859a 2c1f9dd 1414454 e1645d7 1414454 228859a 447f97e 228859a e1645d7 228859a e1645d7 228859a a092d54 d09d1f6 a092d54 d09d1f6 a092d54 d09d1f6 a092d54 d09d1f6 a092d54 d09d1f6 0ca5366 a092d54 183f8cd 2c1f9dd 183f8cd 2c1f9dd a092d54 d09d1f6 a092d54 2c1f9dd 183f8cd a092d54 |
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
"""Functions to load and preprocess text data."""
from __future__ import annotations
import bz2
import json
import re
from functools import lru_cache
from typing import TYPE_CHECKING, Literal, Sequence
import emoji
import pandas as pd
import spacy
from joblib import Parallel, delayed
from tqdm import tqdm
from app.constants import (
AMAZONREVIEWS_PATH,
AMAZONREVIEWS_URL,
IMDB50K_PATH,
IMDB50K_URL,
SENTIMENT140_PATH,
SENTIMENT140_URL,
SLANGMAP_PATH,
SLANGMAP_URL,
TEST_DATASET_PATH,
TEST_DATASET_URL,
)
if TYPE_CHECKING:
from re import Pattern
from spacy.tokens import Doc
__all__ = ["load_data", "tokenize"]
try:
nlp = spacy.load("en_core_web_sm")
except OSError:
print("Downloading spaCy model...")
from spacy.cli import download as spacy_download
spacy_download("en_core_web_sm")
nlp = spacy.load("en_core_web_sm")
@lru_cache(maxsize=1)
def slang() -> tuple[Pattern, dict[str, str]]:
"""Compile a re pattern for slang terms.
Returns:
Slang pattern and mapping
Raises:
FileNotFoundError: If the file is not found
"""
if not SLANGMAP_PATH.exists():
msg = (
f"Slang mapping file not found at: '{SLANGMAP_PATH}'\n"
"Please download the file from:\n"
f"{SLANGMAP_URL}"
) # fmt: off
raise FileNotFoundError(msg)
with SLANGMAP_PATH.open() as f:
mapping = json.load(f)
return re.compile(r"\b(" + "|".join(map(re.escape, mapping.keys())) + r")\b"), mapping
def _clean(text: str) -> str:
"""Perform basic text cleaning.
Args:
text: Text to clean
Returns:
Cleaned text
"""
# Make text lowercase
text = text.lower()
# Remove HTML tags
text = re.sub(r"<[^>]*>", "", text)
# Map slang terms
slang_pattern, slang_mapping = slang()
text = slang_pattern.sub(lambda x: slang_mapping[x.group()], text)
# Remove acronyms and abbreviations
text = re.sub(r"\b(?:[a-z]\.?)(?:[a-z]\.)\b", "", text)
# Remove honorifics
text = re.sub(r"\b(?:mr|mrs|ms|dr|prof|sr|jr)\.?\b", "", text)
# Remove year abbreviations
text = re.sub(r"\b(?:\d{3}0|\d0)s?\b", "", text)
# Remove hashtags
text = re.sub(r"#[^\s]+", "", text)
# Replace mentions with a generic tag
text = re.sub(r"@[^\s]+", "user", text)
# Replace X/Y with X or Y
text = re.sub(r"\b([a-z]+)[//]([a-z]+)\b", r"\1 or \2", text)
# Convert emojis to text
text = emoji.demojize(text, delimiters=("emoji_", ""))
# Remove special characters
text = re.sub(r"[^a-z0-9\s]", "", text)
# EXTRA: imdb50k specific cleaning
text = re.sub(r"mst3k", "", text) # Very common acronym for Mystery Science Theater 3000
return text.strip()
def _lemmatize(doc: Doc, threshold: int = 3) -> Sequence[str]:
"""Lemmatize the provided text using spaCy.
Args:
doc: spaCy document
threshold: Minimum character length of tokens
Returns:
Sequence of lemmatized tokens
"""
return [
tok
for token in doc
if not token.is_stop # Ignore stop words
and not token.is_punct # Ignore punctuation
and not token.like_email # Ignore email addresses
and not token.like_url # Ignore URLs
and not token.like_num # Ignore numbers
and token.is_alpha # Ignore non-alphabetic tokens
and (len(tok := token.lemma_.lower().strip()) >= threshold) # Ignore short tokens
]
def tokenize(
text_data: Sequence[str],
batch_size: int = 512,
n_jobs: int = 4,
character_threshold: int = 3,
show_progress: bool = True,
) -> Sequence[Sequence[str]]:
"""Tokenize the provided text using spaCy.
Args:
text_data: Text data to tokenize
batch_size: Batch size for tokenization
n_jobs: Number of parallel jobs
character_threshold: Minimum character length of tokens
show_progress: Whether to show a progress bar
Returns:
Tokenized text data
"""
text_data = Parallel(n_jobs=n_jobs)(
delayed(_clean)(text)
for text in tqdm(
text_data,
desc="Cleaning",
unit="doc",
disable=not show_progress,
)
)
return pd.Series(
[
_lemmatize(doc, character_threshold)
for doc in tqdm(
nlp.pipe(text_data, batch_size=batch_size, n_process=n_jobs, disable=["parser", "ner"]),
total=len(text_data),
desc="Lemmatization",
unit="doc",
disable=not show_progress,
)
],
)
def load_sentiment140(include_neutral: bool = False) -> tuple[list[str], list[int]]:
"""Load the sentiment140 dataset and make it suitable for use.
Args:
include_neutral: Whether to include neutral sentiment
Returns:
Text and label data
Raises:
FileNotFoundError: If the dataset is not found
"""
# Check if the dataset exists
if not SENTIMENT140_PATH.exists():
msg = (
f"Sentiment140 dataset not found at: '{SENTIMENT140_PATH}'\n"
"Please download the dataset from:\n"
f"{SENTIMENT140_URL}"
)
raise FileNotFoundError(msg)
# Load the dataset
data = pd.read_csv(
SENTIMENT140_PATH,
encoding="ISO-8859-1",
names=[
"target", # 0 = negative, 2 = neutral, 4 = positive
"id", # The id of the tweet
"date", # The date of the tweet
"flag", # The query, NO_QUERY if not present
"user", # The user that tweeted
"text", # The text of the tweet
],
)
# Ignore rows with neutral sentiment
if not include_neutral:
data = data[data["target"] != 2]
# Map sentiment values
data["sentiment"] = data["target"].map(
{
0: 0, # Negative
4: 1, # Positive
2: 2, # Neutral
},
)
# Return as lists
return data["text"].tolist(), data["sentiment"].tolist()
def load_amazonreviews() -> tuple[list[str], list[int]]:
"""Load the amazonreviews dataset and make it suitable for use.
Returns:
Text and label data
Raises:
FileNotFoundError: If the dataset is not found
"""
# Check if the dataset exists
if not AMAZONREVIEWS_PATH.exists():
msg = (
f"Amazonreviews dataset not found at: '{AMAZONREVIEWS_PATH}'\n"
"Please download the dataset from:\n"
f"{AMAZONREVIEWS_URL}"
)
raise FileNotFoundError(msg)
# Load the dataset
with bz2.BZ2File(AMAZONREVIEWS_PATH) as f:
dataset = [line.decode("utf-8") for line in f]
# Split the data into labels and text
labels, texts = zip(*(line.split(" ", 1) for line in dataset))
# Map sentiment values
sentiments = [int(label.split("__label__")[1]) - 1 for label in labels]
# Return as lists
return texts, sentiments
def load_imdb50k() -> tuple[list[str], list[int]]:
"""Load the imdb50k dataset and make it suitable for use.
Returns:
Text and label data
Raises:
FileNotFoundError: If the dataset is not found
"""
# Check if the dataset exists
if not IMDB50K_PATH.exists():
msg = (
f"IMDB50K dataset not found at: '{IMDB50K_PATH}'\n"
"Please download the dataset from:\n"
f"{IMDB50K_URL}"
) # fmt: off
raise FileNotFoundError(msg)
# Load the dataset
data = pd.read_csv(IMDB50K_PATH)
# Map sentiment values
data["sentiment"] = data["sentiment"].map(
{
"positive": 1,
"negative": 0,
},
)
# Return as lists
return data["review"].tolist(), data["sentiment"].tolist()
def load_test() -> tuple[list[str], list[int]]:
"""Load the test dataset and make it suitable for use.
Returns:
Text and label data
Raises:
FileNotFoundError: If the dataset is not found
"""
# Check if the dataset exists
if not TEST_DATASET_PATH.exists():
msg = (
f"Test dataset not found at: '{TEST_DATASET_PATH}'\n"
"Please download the dataset from:\n"
f"{TEST_DATASET_URL}"
)
raise FileNotFoundError(msg)
# Load the dataset
data = pd.read_csv(TEST_DATASET_PATH)
# Return as lists
return data["text"].tolist(), data["sentiment"].tolist()
def load_data(dataset: Literal["sentiment140", "amazonreviews", "imdb50k", "test"]) -> tuple[list[str], list[int]]:
"""Load and preprocess the specified dataset.
Args:
dataset: Dataset to load
Returns:
Text and label data
Raises:
ValueError: If the dataset is not recognized
"""
match dataset:
case "sentiment140":
return load_sentiment140(include_neutral=False)
case "amazonreviews":
return load_amazonreviews()
case "imdb50k":
return load_imdb50k()
case "test":
return load_test()
case _:
msg = f"Unknown dataset: {dataset}"
raise ValueError(msg)
|