ingerid commited on
Commit
e2235e2
1 Parent(s): f458b54

add separate preprocessing module

Browse files
Files changed (2) hide show
  1. nb_samtale.py +1 -79
  2. preprocess_transcriptions.py +78 -0
nb_samtale.py CHANGED
@@ -152,7 +152,7 @@ class NBSamtale(datasets.GeneratorBasedBuilder):
152
  for row in datalines:
153
  data = json.loads(row)
154
  audio_path = data["file_name"]
155
- data["transcription"] = normalize_transcription(data["transcription"], config=self.config.name)
156
  meta[audio_path] = data
157
 
158
  id_ = 0
@@ -170,81 +170,3 @@ class NBSamtale(datasets.GeneratorBasedBuilder):
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\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
 
152
  for row in datalines:
153
  data = json.loads(row)
154
  audio_path = data["file_name"]
155
+ data["transcription"] = data[self.config.name]
156
  meta[audio_path] = data
157
 
158
  id_ = 0
 
170
  id_ += 1
171
 
172
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
preprocess_transcriptions.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### Normalization functions ###
2
+
3
+ from sprakbanken_normalizer.inverse_text_normalizer import inv_normalize
4
+ import re
5
+
6
+
7
+ def filter_backslash(text, left=True):
8
+ """Substitute backslash notation with the word to the left or right of it."""
9
+ regx = re.compile(r"\b([\w_-]+)\\([\w_-]+)\b")
10
+ if left:
11
+ return regx.sub(r"\1", text)
12
+ else:
13
+ return regx.sub(r"\2", text)
14
+
15
+ def remove_repeats(text):
16
+ """Remove repeated words."""
17
+ return re.sub(r"\b(\w+\s+)(\1){1,10}", "\1", text)
18
+
19
+ def bracket_metatags(text):
20
+ """Enclose unintelligible, foreign, overlapping and unknown words in angle brackets."""
21
+ regx = re.compile(r"%(unint|foreign|unk|overlapping)")
22
+ return regx.sub(r"<\1>", text)
23
+
24
+ def remove_metatags(text):
25
+ """Remove metatags for hesitations, laughter, paralinguistic sounds etc."""
26
+ return re.sub(r"%\w\s", "", text)
27
+
28
+ def remove_percentage_sign(text):
29
+ """Remove percentage sign."""
30
+ return re.sub(r"%", "", text)
31
+
32
+ def remove_false_starts(text):
33
+ """Remove annotations of false starts and interruptions."""
34
+ return re.sub(r"\s\w+£", "", text)
35
+
36
+ def remove_pound_sign(text):
37
+ """Remove pound sign."""
38
+ return re.sub(r"£", "", text)
39
+
40
+ def replace_underscore(text):
41
+ """Replace underscore with a single whitespace."""
42
+ return re.sub(r"_", " ", text)
43
+
44
+ def remove_punctuation(text):
45
+ """Remove punctuation."""
46
+ return re.sub(r"[,\.\!\'-]", "", text)
47
+
48
+ def normalize_number_words(text):
49
+ """Normalize number words to integers."""
50
+ # TODO: convert hyphenated year-words to integers
51
+ # TODO: deal with punctuation at the end
52
+ inv_norm = inv_normalize(text)
53
+ return inv_norm
54
+
55
+
56
+
57
+ def normalize_transcription(transcription: str, config="annotations"):
58
+ """Normalize transcriptions according to orthographic standards, or verbatim."""
59
+ t = transcription
60
+ if config == "annotations":
61
+ # Nothing do, return as is
62
+ return t
63
+ if config == "orthographic":
64
+ t = remove_metatags(t)
65
+ t = remove_false_starts(t)
66
+ t = re.sub(r"CO-to", "CO2", t)
67
+ t = filter_backslash(t, left=False)
68
+ t = normalize_number_words(t)
69
+ elif config == "verbatim":
70
+ t = bracket_metatags(t)
71
+ t = remove_percentage_sign(t)
72
+ t = remove_pound_sign(t)
73
+ t = re.sub(r"C_O-to", "C O to", t)
74
+ t = filter_backslash(t, left=True)
75
+ t = remove_punctuation(t)
76
+ # For both, at the end:
77
+ t = replace_underscore(t)
78
+ return t