ingerid commited on
Commit
803fe81
1 Parent(s): cd2f006

feat: add normalisation functions

Browse files
Files changed (2) hide show
  1. nb_samtale.py +80 -11
  2. requirements.txt +1 -0
nb_samtale.py CHANGED
@@ -65,17 +65,6 @@ _DATA_URL= "https://huggingface.co/datasets/Sprakbanken/nb_samtale/resolve/main/
65
  # "test": ["test_bm_1.tar.gz", "test_nn_1.tar.gz"],
66
  #}
67
 
68
-
69
- def normalize_transcription(transcription: str, config="annotations"):
70
- """Normalize transcriptions according to orthographic standards, or verbatim."""
71
- # TODO: Implement normalization
72
- if config == "orthographic":
73
- return transcription
74
- elif config == "verbatim":
75
- return transcription
76
- return transcription
77
-
78
-
79
  class NBSamtaleConfig(datasets.BuilderConfig):
80
  """BuilderConfig for NBSamtale"""
81
 
@@ -179,3 +168,83 @@ class NBSamtale(datasets.GeneratorBasedBuilder):
179
  result["audio"] = {"path": path, "bytes": audio_file.read()}
180
  yield id_, result
181
  id_ += 1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  # "test": ["test_bm_1.tar.gz", "test_nn_1.tar.gz"],
66
  #}
67
 
 
 
 
 
 
 
 
 
 
 
 
68
  class NBSamtaleConfig(datasets.BuilderConfig):
69
  """BuilderConfig for NBSamtale"""
70
 
 
168
  result["audio"] = {"path": path, "bytes": audio_file.read()}
169
  yield id_, result
170
  id_ += 1
171
+
172
+
173
+ ### Normalization functions ###
174
+
175
+ from sprakbanken_normalizer.inverse_text_normalizer import inv_normalize
176
+ import re
177
+
178
+
179
+ def filter_backslash(text, left=True):
180
+ """Substitute backslash notation with the word to the left or right of it."""
181
+ regx = re.compile(r"\b([\w_-]+)\\([\w_-]+)\b")
182
+ if left:
183
+ return regx.sub(r"\1", text)
184
+ else:
185
+ return regx.sub(r"\2", text)
186
+
187
+ def remove_repeats(text):
188
+ """Remove repeated words."""
189
+ return re.sub(r"\b(\w+\s+)(\1){1,10}", "\1", text)
190
+
191
+ def bracket_metatags(text):
192
+ """Enclose unintelligible, foreign, overlapping and unknown words in angle brackets."""
193
+ regx = re.compile(r"%(unint|foreign|unk|overlapping)")
194
+ return regx.sub(r"<\1>", text)
195
+
196
+ def remove_metatags(text):
197
+ """Remove metatags for hesitations, laughter, paralinguistic sounds etc."""
198
+ return re.sub(r"%\w{3}\s", "", text)
199
+
200
+ def remove_percentage_sign(text):
201
+ """Remove percentage sign."""
202
+ return re.sub(r"%", "", text)
203
+
204
+ def remove_false_starts(text):
205
+ """Remove annotations of false starts and interruptions."""
206
+ return re.sub(r"\s\w+£", "", text)
207
+
208
+ def remove_pound_sign(text):
209
+ """Remove pound sign."""
210
+ return re.sub(r"£", "", text)
211
+
212
+ def replace_underscore(text):
213
+ """Replace underscore with a single whitespace."""
214
+ return re.sub(r"_", " ", text)
215
+
216
+ def remove_punctuation(text):
217
+ """Remove punctuation."""
218
+ return re.sub(r"[,\.\!\'-]", "", text)
219
+
220
+ def normalize_number_words(text):
221
+ """Normalize number words to integers."""
222
+ # TODO: convert hyphenated year-words to integers
223
+ # TODO: deal with punctuation at the end
224
+ inv_norm = inv_normalize(text)
225
+ return inv_norm
226
+
227
+
228
+
229
+ def normalize_transcription(transcription: str, config="annotations"):
230
+ """Normalize transcriptions according to orthographic standards, or verbatim."""
231
+ t = transcription
232
+ if config == "annotations":
233
+ # Nothing do, return as is
234
+ return t
235
+ if config == "orthographic":
236
+ t = remove_metatags(t)
237
+ t = remove_false_starts(t)
238
+ t = re.sub(r"CO-to", "CO2", t)
239
+ t = filter_backslash(t, left=False)
240
+ t = normalize_number_words(t)
241
+ elif config == "verbatim":
242
+ t = bracket_metatags(t)
243
+ t = remove_percentage_sign(t)
244
+ t = remove_pound_sign(t)
245
+ t = re.sub(r"C_O-to", "C O to", t)
246
+ t = filter_backslash(t, left=True)
247
+ t = remove_punctuation(t)
248
+ # For both, at the end:
249
+ t = replace_underscore(t)
250
+ return t
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ sprakbanken_normalizer