Upload 2 files
Browse files- convert_kh-noviny.py +83 -0
- test.jsonl +0 -0
convert_kh-noviny.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
TARGET = ".data/kh-noviny.vert"
|
2 |
+
|
3 |
+
import os
|
4 |
+
import re
|
5 |
+
from typing import Dict
|
6 |
+
|
7 |
+
import jsonlines
|
8 |
+
from tqdm import tqdm
|
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 r="([^"]*)"\s+m="([^"]*)"\s+d="([^"]*)"\s+t="([^"]*)"\s+c="([^"]*)"\s+h="([^"]*)"\s+a="([^"]*)">'
|
16 |
+
)
|
17 |
+
|
18 |
+
# Pattern to remove whitespace before punctuation
|
19 |
+
ws_before_punct = re.compile(r'\s+([.,!?:;])')
|
20 |
+
|
21 |
+
# Find all documents
|
22 |
+
documents = re.findall(doc_pattern, vert_content)
|
23 |
+
processed_documents = {}
|
24 |
+
|
25 |
+
for doc_id, doc in tqdm(enumerate(documents)):
|
26 |
+
# Extract metadata
|
27 |
+
metadata_match = re.search(metadata_pattern, doc)
|
28 |
+
if metadata_match:
|
29 |
+
r = metadata_match.group(1)
|
30 |
+
m = metadata_match.group(2)
|
31 |
+
d = metadata_match.group(3)
|
32 |
+
t = metadata_match.group(4)
|
33 |
+
c = metadata_match.group(5)
|
34 |
+
h = metadata_match.group(6)
|
35 |
+
a = metadata_match.group(7)
|
36 |
+
|
37 |
+
metadata_str = (f"{d} {m} {r}, "
|
38 |
+
f"Zdroj: {t}, "
|
39 |
+
f"Část: {c}, "
|
40 |
+
f"Titulek: {h}, "
|
41 |
+
f"Autor: {a}")
|
42 |
+
|
43 |
+
else:
|
44 |
+
raise ValueError("Metadata not found in document")
|
45 |
+
|
46 |
+
# Initialize an empty list to hold processed document text
|
47 |
+
|
48 |
+
# Find all speaker turns within the document
|
49 |
+
# remove tags from each line, and join text
|
50 |
+
tokens = [line.split("\t")[0].strip() for line in doc.split("\n") if line.strip() != ""]
|
51 |
+
doc_text = " ".join(tokens)
|
52 |
+
|
53 |
+
# remove any text with <...> tag
|
54 |
+
doc_text = re.sub(r'<[^>]*>', '', doc_text)
|
55 |
+
|
56 |
+
# replace more than one space with one space
|
57 |
+
doc_text = re.sub(r'\s+', ' ', doc_text).strip()
|
58 |
+
|
59 |
+
# remove whitespace before ., !, ?
|
60 |
+
doc_text = re.sub(ws_before_punct, r'\1', doc_text)
|
61 |
+
|
62 |
+
# - sometimes lines in oral are empty? e.g. 08A009N // REMOVE THESE LINES
|
63 |
+
if doc_text.strip() == "":
|
64 |
+
continue
|
65 |
+
|
66 |
+
processed_documents[doc_id] = metadata_str + "\n" + doc_text
|
67 |
+
|
68 |
+
return processed_documents
|
69 |
+
|
70 |
+
|
71 |
+
# Read the content from the file
|
72 |
+
with open(TARGET, "r") as f:
|
73 |
+
vert_content = f.read()
|
74 |
+
|
75 |
+
# Process the content
|
76 |
+
processed_documents = process_vert_format(vert_content)
|
77 |
+
|
78 |
+
# write all splits into same json file in .data/hf_dataset/cnc_fictree/test.jsonl
|
79 |
+
OF = ".data/hf_dataset/cnc_khnews/test.jsonl"
|
80 |
+
os.makedirs(os.path.dirname(OF), exist_ok=True)
|
81 |
+
with jsonlines.open(OF, "w") as writer:
|
82 |
+
for doc_id, doc in list(processed_documents.items()):
|
83 |
+
writer.write({"text": doc, "id": doc_id})
|
test.jsonl
ADDED
The diff for this file is too large to render.
See raw diff
|
|