Upload convert_ORTOFON_ORAL13.py
Browse files- convert_ORTOFON_ORAL13.py +199 -0
convert_ORTOFON_ORAL13.py
ADDED
@@ -0,0 +1,199 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# open .data/ORTOFONv1/ortofon_v1_vert.gz
|
2 |
+
import gzip
|
3 |
+
import os
|
4 |
+
import re
|
5 |
+
from typing import Dict
|
6 |
+
from tqdm import tqdm
|
7 |
+
|
8 |
+
FILE_PATH = ".data/ORTOFONv1/ortofon_v1_vert.gz"
|
9 |
+
with gzip.open(FILE_PATH, "rt") as f:
|
10 |
+
data = f.read()
|
11 |
+
"""
|
12 |
+
Problems:
|
13 |
+
- sometimes lines in oral are empty? e.g. 08A009N // REMOVE THESE LINES
|
14 |
+
- sometimes lines in ortofon are containing three dots only, such as [mluvčí: Miroslava] ... // REMOVE THESE LINES
|
15 |
+
- sometimes lines in ortofon contain @ only, e.g., [mluvčí: Radka] @ // REMOVE THESE LINES
|
16 |
+
"""
|
17 |
+
|
18 |
+
def process_vert_format_ortofon(vert_content: str) -> Dict[str, str]:
|
19 |
+
# Pattern to match document boundaries and extract metadata
|
20 |
+
doc_pattern = re.compile(r'<doc[^>]*>.*?</doc>', re.DOTALL)
|
21 |
+
metadata_pattern = re.compile(
|
22 |
+
r'<doc id="([^"]*)" year="([^"]*)" month="([^"]*)" location="([^"]*)" situation="([^"]*)" speakers="([^"]*)" genders="([^"]*)" generations="([^"]*)" relationship="([^"]*)"[^>]*>')
|
23 |
+
|
24 |
+
# Pattern to match speaker turns
|
25 |
+
sp_pattern = re.compile(r'<sp[^>]*nickname="([^"]*)"[^>]*>(.*?)</sp>', re.DOTALL)
|
26 |
+
|
27 |
+
# Pattern to match pw tags
|
28 |
+
pw_pattern = re.compile(r'<pw>\n(.*?)</pw>\n', re.DOTALL)
|
29 |
+
|
30 |
+
# Pattern to remove speaker suffix
|
31 |
+
remove_speaker_suffix = re.compile(r'_[0-9]+$')
|
32 |
+
|
33 |
+
# Pattern to remove whitespace before punctuation
|
34 |
+
ws_before_punct = re.compile(r'\s+([.!?])')
|
35 |
+
|
36 |
+
# Find all documents
|
37 |
+
documents = re.findall(doc_pattern, vert_content)
|
38 |
+
processed_documents = {}
|
39 |
+
|
40 |
+
for doc in tqdm(documents):
|
41 |
+
# Extract metadata
|
42 |
+
metadata_match = re.search(metadata_pattern, doc)
|
43 |
+
if metadata_match:
|
44 |
+
doc_id = metadata_match.group(1)
|
45 |
+
location = metadata_match.group(4)
|
46 |
+
situation = metadata_match.group(5)
|
47 |
+
speakers = metadata_match.group(6)
|
48 |
+
genders = metadata_match.group(7)
|
49 |
+
generations = metadata_match.group(8)
|
50 |
+
relationship = metadata_match.group(9)
|
51 |
+
metadata_str = (f"Lokalita: {location}, Situace: {situation}, "
|
52 |
+
f"Počet mluvčích: {speakers}, Pohlaví: {genders}, "
|
53 |
+
f"Generace: {generations}, Vztah: {relationship}")
|
54 |
+
else:
|
55 |
+
raise ValueError("Metadata not found in document")
|
56 |
+
|
57 |
+
# Initialize an empty list to hold processed document text
|
58 |
+
processed_document = [metadata_str]
|
59 |
+
|
60 |
+
# Find all speaker turns within the document
|
61 |
+
for sp_match in re.findall(sp_pattern, doc):
|
62 |
+
speaker_id = sp_match[0]
|
63 |
+
# sometimes speaker_id ends with _1, _2, _89, etc. Remove it
|
64 |
+
speaker_id = re.sub(remove_speaker_suffix, '', speaker_id)
|
65 |
+
|
66 |
+
# if speaker is Y, rename him as Jiný zvuk
|
67 |
+
if speaker_id == "Y":
|
68 |
+
speaker_id = "Zvuk"
|
69 |
+
|
70 |
+
|
71 |
+
sp_content = sp_match[1]
|
72 |
+
|
73 |
+
segs = re.findall(pw_pattern, sp_content)
|
74 |
+
if segs == []:
|
75 |
+
segs = [sp_content]
|
76 |
+
# remove tags from each line, and join text
|
77 |
+
tokens = [line.split("\t")[0].strip() for seg in segs for line in seg.split("\n") if line != ""]
|
78 |
+
speaker_text = " ".join(tokens)
|
79 |
+
|
80 |
+
# - sometimes lines in ortofon are containing three dots only, such as [mluvčí: Miroslava] ... // REMOVE THESE LINES
|
81 |
+
if speaker_text.strip() == "...":
|
82 |
+
continue
|
83 |
+
# - sometimes lines in ortofon contain @ only, e.g., [mluvčí: Radka] @ // REMOVE THESE LINES
|
84 |
+
if speaker_text.strip() == "@":
|
85 |
+
continue
|
86 |
+
|
87 |
+
# remove whitespace before ., !, ?
|
88 |
+
speaker_text = re.sub(ws_before_punct, r'\1', speaker_text)
|
89 |
+
|
90 |
+
# Format the speaker turn and add to the processed document list
|
91 |
+
processed_document.append(f"[mluvčí: {speaker_id}] {speaker_text}")
|
92 |
+
|
93 |
+
|
94 |
+
# Join all speaker turns into a single string for the document
|
95 |
+
final_text = '\n'.join(processed_document)
|
96 |
+
processed_documents[doc_id] = final_text
|
97 |
+
|
98 |
+
return processed_documents
|
99 |
+
|
100 |
+
|
101 |
+
ortofon_data = process_vert_format_ortofon(data)
|
102 |
+
del data
|
103 |
+
|
104 |
+
|
105 |
+
FILE_PATH = ".data/ORAL2013/oral2013_vert.gz"
|
106 |
+
with gzip.open(FILE_PATH, "rt") as f:
|
107 |
+
data = f.read()
|
108 |
+
|
109 |
+
def process_vert_format_oral(vert_content: str) -> Dict[str, str]:
|
110 |
+
# Pattern to match document boundaries and extract metadata
|
111 |
+
doc_pattern = re.compile(r'<doc[^>]*>.*?</doc>', re.DOTALL)
|
112 |
+
metadata_pattern = re.compile(
|
113 |
+
r'<doc id="([^"]*)" temp="([^"]*)" pocet="([^"]*)" vztah="([^"]*)" situace="([^"]*)" promluva="([^"]*)"[^>]*>'
|
114 |
+
)
|
115 |
+
# Pattern to match speaker turns
|
116 |
+
sp_pattern = re.compile(r'<sp[^>]*num="([^"]*)"[^>]*>(.*?)</sp>', re.DOTALL)
|
117 |
+
|
118 |
+
# Pattern to match seg tags
|
119 |
+
seg_pattern = re.compile(r'<seg start="[^"]*" end="[^"]*">(.*?)</seg>\n', re.DOTALL)
|
120 |
+
|
121 |
+
# Pattern to remove whitespace before punctuation
|
122 |
+
ws_before_punct = re.compile(r'\s+([.!?])')
|
123 |
+
|
124 |
+
# Find all documents
|
125 |
+
documents = re.findall(doc_pattern, vert_content)
|
126 |
+
processed_documents = {}
|
127 |
+
|
128 |
+
for doc in tqdm(documents):
|
129 |
+
# Extract metadata
|
130 |
+
metadata_match = re.search(metadata_pattern, doc)
|
131 |
+
if metadata_match:
|
132 |
+
doc_id = metadata_match.group(1)
|
133 |
+
situation = metadata_match.group(5)
|
134 |
+
speakers = metadata_match.group(3)
|
135 |
+
relationship = metadata_match.group(4)
|
136 |
+
|
137 |
+
metadata_str = (f"Situace: {situation}, "
|
138 |
+
f"Počet mluvčích: {speakers}, "
|
139 |
+
f"Vztah: {relationship}")
|
140 |
+
else:
|
141 |
+
raise ValueError("Metadata not found in document")
|
142 |
+
|
143 |
+
# Initialize an empty list to hold processed document text
|
144 |
+
processed_document = [metadata_str]
|
145 |
+
|
146 |
+
# Find all speaker turns within the document
|
147 |
+
for sp_match in re.findall(sp_pattern, doc):
|
148 |
+
speaker_id = sp_match[0]
|
149 |
+
|
150 |
+
# if speaker is Y, rename him as Jiný zvuk
|
151 |
+
if speaker_id == "Y":
|
152 |
+
speaker_id = "Zvuk"
|
153 |
+
|
154 |
+
sp_content = sp_match[1]
|
155 |
+
# remove symbols ---, ...:,
|
156 |
+
sp_content = sp_content.replace("---", "")
|
157 |
+
sp_content = sp_content.replace("...:", "")
|
158 |
+
sp_content = sp_content.replace("...", "")
|
159 |
+
sp_content = sp_content.replace("?.", "?")
|
160 |
+
|
161 |
+
|
162 |
+
segs = re.findall(seg_pattern, sp_content)
|
163 |
+
if segs == []:
|
164 |
+
segs = [sp_content]
|
165 |
+
# remove tags from each line, and join text
|
166 |
+
tokens = [line.split("\t")[0].strip() for seg in segs for line in seg.split("\n") if line.strip() != ""]
|
167 |
+
speaker_text = " ".join(tokens)
|
168 |
+
|
169 |
+
# remove whitespace before ., !, ?
|
170 |
+
speaker_text = re.sub(ws_before_punct, r'\1', speaker_text)
|
171 |
+
|
172 |
+
# - sometimes lines in oral are empty? e.g. 08A009N // REMOVE THESE LINES
|
173 |
+
if speaker_text.strip() == "":
|
174 |
+
continue
|
175 |
+
|
176 |
+
# Format the speaker turn and add to the processed document list
|
177 |
+
processed_document.append(f"[mluvčí: {speaker_id}] {speaker_text}")
|
178 |
+
|
179 |
+
|
180 |
+
# Join all speaker turns into a single string for the document
|
181 |
+
final_text = '\n'.join(processed_document)
|
182 |
+
processed_documents[doc_id] = final_text
|
183 |
+
|
184 |
+
return processed_documents
|
185 |
+
|
186 |
+
oral_data = process_vert_format_oral(data)
|
187 |
+
|
188 |
+
# merge ortofon and oral data
|
189 |
+
ortofon_data.update(oral_data)
|
190 |
+
|
191 |
+
# save the merged data in jsonlines as {"text": doc, "id": doc_id}
|
192 |
+
|
193 |
+
import jsonlines
|
194 |
+
FILE_PATH = ".data/hf_dataset/ortofon_oral/test.jsonl"
|
195 |
+
|
196 |
+
os.makedirs(os.path.dirname(FILE_PATH), exist_ok=True)
|
197 |
+
with jsonlines.open(FILE_PATH, 'w') as writer:
|
198 |
+
for doc_id, doc in ortofon_data.items():
|
199 |
+
writer.write({"text": doc, "id": doc_id})
|