nb_samtale / preprocess_transcriptions.py
ingerid's picture
add columns for different transcription config options
52c1eb7 unverified
### 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