#author: Martin Fajčík import gzip import os import re from typing import Dict from tqdm import tqdm FILE_PATH = ".data/ORTOFONv1/ortofon_v1_vert.gz" with gzip.open(FILE_PATH, "rt") as f: data = f.read() def process_vert_format_ortofon(vert_content: str) -> Dict[str, str]: # Pattern to match document boundaries and extract metadata doc_pattern = re.compile(r']*>.*?', re.DOTALL) metadata_pattern = re.compile( r']*>') # Pattern to match speaker turns sp_pattern = re.compile(r']*nickname="([^"]*)"[^>]*>(.*?)', re.DOTALL) # Pattern to match pw tags pw_pattern = re.compile(r'\n(.*?)\n', re.DOTALL) # Pattern to remove speaker suffix remove_speaker_suffix = re.compile(r'_[0-9]+$') # Pattern to remove whitespace before punctuation ws_before_punct = re.compile(r'\s+([.!?])') # Find all documents documents = re.findall(doc_pattern, vert_content) processed_documents = {} for doc in tqdm(documents): # Extract metadata metadata_match = re.search(metadata_pattern, doc) if metadata_match: doc_id = metadata_match.group(1) location = metadata_match.group(4) situation = metadata_match.group(5) speakers = metadata_match.group(6) genders = metadata_match.group(7) generations = metadata_match.group(8) relationship = metadata_match.group(9) metadata_str = (f"Lokalita: {location}, Situace: {situation}, " f"Počet mluvčích: {speakers}, Pohlaví: {genders}, " f"Generace: {generations}, Vztah: {relationship}") else: raise ValueError("Metadata not found in document") # Initialize an empty list to hold processed document text processed_document = [metadata_str] # Find all speaker turns within the document for sp_match in re.findall(sp_pattern, doc): speaker_id = sp_match[0] # sometimes speaker_id ends with _1, _2, _89, etc. Remove it speaker_id = re.sub(remove_speaker_suffix, '', speaker_id) # if speaker is Y, rename him as Jiný zvuk if speaker_id == "Y": speaker_id = "Zvuk" sp_content = sp_match[1] segs = re.findall(pw_pattern, sp_content) if segs == []: segs = [sp_content] # remove tags from each line, and join text tokens = [line.split("\t")[0].strip() for seg in segs for line in seg.split("\n") if line != ""] speaker_text = " ".join(tokens) # - sometimes lines in ortofon are containing three dots only, such as [mluvčí: Miroslava] ... // REMOVE THESE LINES if speaker_text.strip() == "...": continue # - sometimes lines in ortofon contain @ only, e.g., [mluvčí: Radka] @ // REMOVE THESE LINES if speaker_text.strip() == "@": continue # remove whitespace before ., !, ? speaker_text = re.sub(ws_before_punct, r'\1', speaker_text) # Format the speaker turn and add to the processed document list processed_document.append(f"[mluvčí: {speaker_id}] {speaker_text}") # Join all speaker turns into a single string for the document final_text = '\n'.join(processed_document) processed_documents[doc_id] = final_text return processed_documents ortofon_data = process_vert_format_ortofon(data) del data FILE_PATH = ".data/ORAL2013/oral2013_vert.gz" with gzip.open(FILE_PATH, "rt") as f: data = f.read() def process_vert_format_oral(vert_content: str) -> Dict[str, str]: # Pattern to match document boundaries and extract metadata doc_pattern = re.compile(r']*>.*?', re.DOTALL) metadata_pattern = re.compile( r']*>' ) # Pattern to match speaker turns sp_pattern = re.compile(r']*num="([^"]*)"[^>]*>(.*?)', re.DOTALL) # Pattern to match seg tags seg_pattern = re.compile(r'(.*?)\n', re.DOTALL) # Pattern to remove whitespace before punctuation ws_before_punct = re.compile(r'\s+([.!?])') # Find all documents documents = re.findall(doc_pattern, vert_content) processed_documents = {} for doc in tqdm(documents): # Extract metadata metadata_match = re.search(metadata_pattern, doc) if metadata_match: doc_id = metadata_match.group(1) situation = metadata_match.group(5) speakers = metadata_match.group(3) relationship = metadata_match.group(4) metadata_str = (f"Situace: {situation}, " f"Počet mluvčích: {speakers}, " f"Vztah: {relationship}") else: raise ValueError("Metadata not found in document") # Initialize an empty list to hold processed document text processed_document = [metadata_str] # Find all speaker turns within the document for sp_match in re.findall(sp_pattern, doc): speaker_id = sp_match[0] # if speaker is Y, rename him as Jiný zvuk if speaker_id == "Y": speaker_id = "Zvuk" sp_content = sp_match[1] # remove symbols ---, ...:, sp_content = sp_content.replace("---", "") sp_content = sp_content.replace("...:", "") sp_content = sp_content.replace("...", "") sp_content = sp_content.replace("?.", "?") segs = re.findall(seg_pattern, sp_content) if segs == []: segs = [sp_content] # remove tags from each line, and join text tokens = [line.split("\t")[0].strip() for seg in segs for line in seg.split("\n") if line.strip() != ""] speaker_text = " ".join(tokens) # remove whitespace before ., !, ? speaker_text = re.sub(ws_before_punct, r'\1', speaker_text) # - sometimes lines in oral are empty? e.g. 08A009N // REMOVE THESE LINES if speaker_text.strip() == "": continue # Format the speaker turn and add to the processed document list processed_document.append(f"[mluvčí: {speaker_id}] {speaker_text}") # Join all speaker turns into a single string for the document final_text = '\n'.join(processed_document) processed_documents[doc_id] = final_text return processed_documents oral_data = process_vert_format_oral(data) # merge ortofon and oral data ortofon_data.update(oral_data) # save the merged data in jsonlines as {"text": doc, "id": doc_id} import jsonlines FILE_PATH = ".data/hf_dataset/ortofon_oral/test.jsonl" os.makedirs(os.path.dirname(FILE_PATH), exist_ok=True) with jsonlines.open(FILE_PATH, 'w') as writer: for doc_id, doc in ortofon_data.items(): writer.write({"text": doc, "id": doc_id})