Spaces:
Runtime error

File size: 1,273 Bytes
39ebf7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import re
import random

LABEL_MAP_GERMEVAL = {0: "ok", 1: "hass", 2: "beleidigung", 3: "vulgaer"}
FLAG_LABELS = {"beleidigung", "hass", "drohung", "belaestigung"}

LEET = str.maketrans({"e": "3", "o": "0", "a": "4", "i": "1", "s": "5"})


def clean(text: str) -> str:
    t = str(text or "")
    t = re.sub(r"<@!?&?\d+>", "@user", t)
    t = re.sub(r"@\w+", "@user", t)
    t = re.sub(r"https?://\S+", "[link]", t)
    t = re.sub(r"\|LBR\|", " ", t)
    t = re.sub(r"(.)\1{3,}", r"\1\1\1", t)
    t = re.sub(r"(?<!\w)(?:\w[ .\-_*]){3,}\w(?!\w)", lambda m: re.sub(r"[ .\-_*]", "", m.group()), t)
    t = re.sub(r"\s+", " ", t).strip()
    return t[:1000]


def augment(text: str, rng: random.Random) -> list[str]:
    out = [text.lower()]
    words = text.split()
    if words:
        i = rng.randrange(len(words))
        w = words[i]
        if len(w) > 3:
            v = list(words)
            v[i] = w.lower().translate(LEET)
            out.append(" ".join(v))
            v2 = list(words)
            k = rng.randrange(len(w))
            v2[i] = w[:k] + w[k] * 3 + w[k + 1:]
            out.append(" ".join(v2))
    if len(text) < 60:
        out.append(text.lower().rstrip(".!?") + " " + rng.choice(["lol", "xd", "digga", "bro", "😂", "🤡"]))
    return out