Upload 2 files
Browse files- .gitattributes +1 -0
- convert_dialekt.py +123 -0
- test.jsonl +3 -0
.gitattributes
CHANGED
@@ -53,3 +53,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
53 |
*.jpg filter=lfs diff=lfs merge=lfs -text
|
54 |
*.jpeg filter=lfs diff=lfs merge=lfs -text
|
55 |
*.webp filter=lfs diff=lfs merge=lfs -text
|
|
|
|
53 |
*.jpg filter=lfs diff=lfs merge=lfs -text
|
54 |
*.jpeg filter=lfs diff=lfs merge=lfs -text
|
55 |
*.webp filter=lfs diff=lfs merge=lfs -text
|
56 |
+
test.jsonl filter=lfs diff=lfs merge=lfs -text
|
convert_dialekt.py
ADDED
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import re
|
3 |
+
from typing import Dict
|
4 |
+
|
5 |
+
import jsonlines
|
6 |
+
from tqdm import tqdm
|
7 |
+
|
8 |
+
TARGET = ".data/dialekt_v2_ort.vert"
|
9 |
+
|
10 |
+
|
11 |
+
def process_vert_format(vert_content: str) -> Dict[str, str]:
|
12 |
+
# Pattern to match document boundaries and extract metadata
|
13 |
+
doc_pattern = re.compile(r'<doc[^>]*>.*?</doc>', re.DOTALL)
|
14 |
+
metadata_pattern = re.compile(
|
15 |
+
r'<doc id="([^"]*)".+?rok="([^"]*)".+?misto="([^"]*)" sidlotyp="([^"]*)".+?tema="([^"]*)" pocetml="([^"]*)"')
|
16 |
+
|
17 |
+
# Pattern to match speaker turns
|
18 |
+
sp_pattern = re.compile(r'<sp[^>]*id="([^"]*)"[^>]*prezdivka="([^"]*)"\s*[^>]*>(.*?)</sp>', re.DOTALL)
|
19 |
+
|
20 |
+
# Pattern to remove whitespace before punctuation
|
21 |
+
ws_before_punct = re.compile(r'\s+([.,!?])')
|
22 |
+
|
23 |
+
# Find all documents
|
24 |
+
documents = re.findall(doc_pattern, vert_content)
|
25 |
+
processed_documents = {}
|
26 |
+
|
27 |
+
for doc in tqdm(documents):
|
28 |
+
# Extract metadata
|
29 |
+
metadata_match = re.search(metadata_pattern, doc)
|
30 |
+
if metadata_match:
|
31 |
+
# r'<doc id="([^"]*)".+?rok="([^"]*)".+?misto="([^"]*)" sidlotyp="([^"]*)".+?tema="([^"]*)" pocetml="([^"]*)"')
|
32 |
+
|
33 |
+
doc_id = metadata_match.group(1)
|
34 |
+
year = metadata_match.group(2)
|
35 |
+
place = metadata_match.group(3)
|
36 |
+
settlement_type = metadata_match.group(4)
|
37 |
+
topic = metadata_match.group(5)
|
38 |
+
speakers = metadata_match.group(6)
|
39 |
+
|
40 |
+
metadata_str = (f"Rok: {year}, "
|
41 |
+
f"Typ sídla: {settlement_type}, "
|
42 |
+
f"Místo: {place}, "
|
43 |
+
f"Téma: {topic}, "
|
44 |
+
f"Počet mluvčích: {speakers}")
|
45 |
+
else:
|
46 |
+
raise ValueError("Metadata not found in document")
|
47 |
+
|
48 |
+
# Initialize an empty list to hold processed document text
|
49 |
+
processed_document = [metadata_str]
|
50 |
+
|
51 |
+
# Find all speaker turns within the document
|
52 |
+
for sp_match in re.findall(sp_pattern, doc):
|
53 |
+
speaker_id = sp_match[0]
|
54 |
+
speaker_nickname = sp_match[1]
|
55 |
+
sp_content = sp_match[2]
|
56 |
+
|
57 |
+
# if speaker is Y, rename him as Jiný zvuk
|
58 |
+
if speaker_id == "Y":
|
59 |
+
speaker_id = "Zvuk"
|
60 |
+
|
61 |
+
# remove symbols ---, ...:,
|
62 |
+
sp_content = sp_content.replace("---", "")
|
63 |
+
sp_content = sp_content.replace("...:", "")
|
64 |
+
sp_content = sp_content.replace("...", "")
|
65 |
+
sp_content = sp_content.replace("..", "")
|
66 |
+
sp_content = sp_content.replace("?.", "?")
|
67 |
+
|
68 |
+
# remove tags from each line, and join text
|
69 |
+
tokens = [line.split("\t")[0].strip() for line in sp_content.split("\n") if line.strip() != ""]
|
70 |
+
speaker_text = " ".join(tokens)
|
71 |
+
|
72 |
+
# replace more than one space with one space
|
73 |
+
speaker_text = re.sub(r'\s+', ' ', speaker_text).strip()
|
74 |
+
|
75 |
+
# remove whitespace before ., !, ?
|
76 |
+
speaker_text = re.sub(ws_before_punct, r'\1', speaker_text)
|
77 |
+
|
78 |
+
# - sometimes lines in oral are empty? e.g. 08A009N // REMOVE THESE LINES
|
79 |
+
if speaker_text.strip() == "":
|
80 |
+
continue
|
81 |
+
# If the turn is not finished by ., ?, !, add .
|
82 |
+
if speaker_text[-1] not in [".", "!", "?"]:
|
83 |
+
speaker_text += "."
|
84 |
+
# Capitalize the first letter of the speaker turn
|
85 |
+
speaker_text = speaker_text[0].upper() + speaker_text[1:]
|
86 |
+
|
87 |
+
# sometimes there is @ in the text, remove it
|
88 |
+
speaker_text = speaker_text.replace("@", "")
|
89 |
+
|
90 |
+
# capitalize first (non whitespace) letter after ., !, ?
|
91 |
+
speaker_text = re.sub(r'([.!?]\s*)(\w)', lambda m: m.group(1) + m.group(2).upper(), speaker_text)
|
92 |
+
speaker_text = speaker_text.replace("overlap", "překryv mluvčích")
|
93 |
+
|
94 |
+
# Format the speaker turn and add to the processed document list
|
95 |
+
if speakers == "1":
|
96 |
+
processed_document.append(speaker_text)
|
97 |
+
else:
|
98 |
+
processed_document.append(f"[mluvčí: {speaker_nickname}] {speaker_text}")
|
99 |
+
|
100 |
+
# Join all speaker turns into a single string for the document
|
101 |
+
if speakers == "1":
|
102 |
+
final_text = processed_document[0] + "\n" + " ".join(processed_document[1:])
|
103 |
+
else:
|
104 |
+
final_text = '\n'.join(processed_document)
|
105 |
+
processed_documents[doc_id] = final_text
|
106 |
+
|
107 |
+
return processed_documents
|
108 |
+
|
109 |
+
|
110 |
+
# Read the content from the file
|
111 |
+
with open(TARGET, "r") as f:
|
112 |
+
vert_content = f.read()
|
113 |
+
|
114 |
+
# Process the content
|
115 |
+
processed_documents = process_vert_format(vert_content)
|
116 |
+
|
117 |
+
# write all splits into same json file in .data/hf_dataset/cnc_fictree/test.jsonl
|
118 |
+
OF = ".data/hf_dataset/cnc_dialekt/test.jsonl"
|
119 |
+
os.makedirs(os.path.dirname(OF), exist_ok=True)
|
120 |
+
with jsonlines.open(OF, "w") as writer:
|
121 |
+
for split, docs in list(processed_documents.items()):
|
122 |
+
for doc in docs:
|
123 |
+
writer.write({"text": doc})
|
test.jsonl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:7a8830468997bc5a62e0a890383cf201dad01b8f5ec6aa7fd74ca42fee4d34a4
|
3 |
+
size 39884457
|