Datasets:
CZLC
/

Modalities:
Text
Formats:
json
Languages:
Czech
Libraries:
Datasets
pandas
License:
mfajcik commited on
Commit
8d80dc0
·
verified ·
1 Parent(s): f96ffea

Upload 2 files

Browse files
Files changed (2) hide show
  1. convert_AKCES.py +72 -0
  2. test.jsonl +0 -0
convert_AKCES.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import re
4
+ import subprocess
5
+ from docx import Document
6
+
7
+ FILE_PATH = ".data/AKCES/AKCES 1/kontroly-RTF/"
8
+ OUTPUT_FILE = ".data/hf_dataset/cnc_skript2012/test.jsonl"
9
+
10
+
11
+ def convert_to_docx(file_path):
12
+ """Convert a given file to DOCX using LibreOffice."""
13
+ new_file_path = file_path + 'x'
14
+ command = ['libreoffice', '--headless', '--convert-to', 'docx', '--outdir', os.path.dirname(file_path), file_path]
15
+ subprocess.run(command, check=True)
16
+ return new_file_path
17
+
18
+
19
+ def read_docx_file(file_path):
20
+ """Extract text from a DOCX file."""
21
+ doc = Document(file_path)
22
+ text = "\n".join([para.text for para in doc.paragraphs])
23
+ return text
24
+
25
+
26
+ def main():
27
+ # Step 1: Convert all RTF and DOC files to DOCX
28
+ for root, dirs, files in os.walk(FILE_PATH):
29
+ for file in files:
30
+ file_path = os.path.join(root, file)
31
+ if file.endswith('.rtf') or file.endswith('.doc'):
32
+ # if the docx file already exists, skip the conversion
33
+ docx_file_path = file_path[:-3] + 'docx'
34
+ if os.path.exists(docx_file_path):
35
+ continue
36
+ try:
37
+ convert_to_docx(file_path)
38
+ except subprocess.CalledProcessError as e:
39
+ raise ValueError(f"Error converting {file_path}: {e}")
40
+
41
+ # Step 2: Process all DOCX files to extract text
42
+ output_data = []
43
+ for root, dirs, files in os.walk(FILE_PATH):
44
+ for file in files:
45
+ file_path = os.path.join(root, file)
46
+ if file.endswith('.docx'):
47
+ try:
48
+ text = read_docx_file(file_path)
49
+
50
+ # postprocessing
51
+ text = text.replace("\xa0", " ")
52
+ text = text.replace("\n \n", "\n\n")
53
+ text = re.sub(r' +\n', '\n', text)
54
+ text = re.sub(r' +', ' ', text)
55
+ text = text.strip()
56
+
57
+ output_data.append({
58
+ "text": text,
59
+ "original_file": file[:-5], # remove the extension
60
+ })
61
+ except Exception as e:
62
+ print(f"Error reading {file_path}: {e}")
63
+
64
+ os.makedirs(os.path.dirname(OUTPUT_FILE), exist_ok=True)
65
+ # Write the output data to a JSONL file
66
+ with open(OUTPUT_FILE, 'w', encoding='utf-8') as jsonl_file:
67
+ for entry in output_data:
68
+ jsonl_file.write(json.dumps(entry, ensure_ascii=False) + '\n')
69
+
70
+
71
+ if __name__ == "__main__":
72
+ main()
test.jsonl ADDED
The diff for this file is too large to render. See raw diff