File size: 2,595 Bytes
e2235e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52c1eb7
e2235e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52c1eb7
e2235e2
 
 
 
 
 
 
 
 
 
 
52c1eb7
 
e2235e2
 
 
 
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
### Normalization functions ###

from sprakbanken_normalizer.inverse_text_normalizer import inv_normalize
import re


def filter_backslash(text, left=True):
    """Substitute backslash notation with the word to the left or right of it."""
    regx = re.compile(r"\b([\w_-]+)\\([\w_-]+)\b")
    if left:
        return regx.sub(r"\1", text)
    else:
        return regx.sub(r"\2", text)

def remove_repeats(text):
    """Remove repeated words."""
    return re.sub(r"\b(\w+\s+)(\1){1,10}", "\1", text)

def bracket_metatags(text):
    """Enclose unintelligible, foreign, overlapping and unknown words in angle brackets."""
    regx = re.compile(r"%(unint|foreign|unk|overlapping)")
    return regx.sub(r"<\1>", text)

def remove_metatags(text):
    """Remove metatags for hesitations, laughter, paralinguistic sounds etc."""
    return re.sub(r"%\w+\s", "", text)

def remove_percentage_sign(text):
    """Remove percentage sign."""
    return re.sub(r"%", "", text)

def remove_false_starts(text):
    """Remove annotations of false starts and interruptions."""
    return re.sub(r"\s\w+£", "", text)

def remove_pound_sign(text):
    """Remove pound sign."""
    return re.sub(r"£", "", text)

def replace_underscore(text):
    """Replace underscore with a single whitespace."""
    return re.sub(r"_", " ", text)

def remove_punctuation(text):
    """Remove punctuation."""
    return re.sub(r"[,\.\!\'-]", "", text)

def normalize_number_words(text):
    """Normalize number words to integers."""
    # TODO: convert hyphenated year-words to integers
    # TODO: deal with punctuation at the end
    inv_norm = inv_normalize(text)
    return inv_norm



def normalize_transcription(transcription: str, config="annotations"):
    """Normalize transcriptions according to orthographic standards, or verbatim."""
    t = transcription
    if config == "annotations":
        # Nothing to do, return as is
        return t
    if config == "orthographic":
        t = remove_metatags(t)
        t = remove_false_starts(t)
        t = re.sub(r"CO-to", "CO2", t)
        t = filter_backslash(t, left=False)
        t = normalize_number_words(t)
    elif config == "verbatim":
        t = bracket_metatags(t)
        t = remove_percentage_sign(t)
        t = remove_pound_sign(t)
        t = re.sub(r"C_O-to", "C O to", t)  ## NB! "dette C_O-to-pro..." becomes "dette C O topros..."
                                            # TODO: handle hyphens instead of removing them?
        t = filter_backslash(t, left=True)
        t = remove_punctuation(t)
    t = replace_underscore(t)
    return t