Spaces:
Runtime error
Runtime error
File size: 818 Bytes
2f9be58 a00fee9 547bc5b 2f9be58 6c2294e 2f9be58 6c2294e 2f9be58 6c2294e 2f9be58 6c2294e 2f9be58 6c2294e 2f9be58 6c2294e 2f9be58 6c2294e 2f9be58 6c2294e 2f9be58 6c2294e 2f9be58 | 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 | import re
import unicodedata
from absa.data.transliterate import transliterate
def clean(text: str, language: str) -> str:
"""Clean text by lowercasing, removing URLs/mentions/hashtags, normalizing unicode, stripping whitespace."""
if not text:
return ""
# lowercase
text = text.lower()
# remove URLs
text = re.sub(r"http\S+|www\.\S+", "", text)
# remove mentions
text = re.sub(r"@\w+", "", text)
# remove hashtags
text = re.sub(r"#\w+", "", text)
# Apply transliteration only for hi/hinglish inputs
if language in ["hi", "hinglish"]:
text = transliterate(text, language)
# normalize unicode
text = unicodedata.normalize("NFKC", text)
# strip whitespace
text = text.strip()
text = re.sub(r"\s+", " ", text)
return text
|