-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess.py
More file actions
135 lines (108 loc) · 4.17 KB
/
Copy pathpreprocess.py
File metadata and controls
135 lines (108 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import glob
import os
import nltk
import json
import argparse
from pathlib import Path
from tqdm import tqdm
from bs4 import BeautifulSoup
from nltk.stem import WordNetLemmatizer
from nltk.tokenize import word_tokenize
from langdetect import detect
def table_to_markdown(table):
rows = table.find_all("tr")
if not rows:
return ""
md_rows = []
for i, row in enumerate(rows):
cells = row.find_all(["th", "td"])
cell_texts = [c.get_text(strip=True) for c in cells]
md_rows.append("| " + " | ".join(cell_texts) + " |")
if i == 0:
md_rows.append("| " + " | ".join(["---"] * len(cell_texts)) + " |")
return "\n".join(md_rows)
def create_json_data(paths):
data = []
for path in tqdm(paths):
ext = path.split(".")[-1]
with open(path, "r", encoding="utf-8") as f:
content = f.read()
if ext == "html":
soup = BeautifulSoup(content, "html.parser")
title = soup.title.string if soup.title else None
if soup.head:
soup.head.decompose()
for tag in soup(["aside", "style", "footer", "script", "img", "iframe", "form", "noscript", "a", "button"]):
tag.decompose()
selectors = [
"div.skipnav",
"div.toolbar.top-nav-on",
"div.section-toc.section-toc-before",
"div.breadcrumb-container",
"div.top-pager",
"div.feedback-panel"
]
for selector in selectors:
for element in soup.select(selector):
element.decompose()
for table in soup.find_all("table"):
md = table_to_markdown(table)
table.replace_with(md)
text = soup.get_text(separator="\n", strip=True)
else:
title = None
text = content.strip()
lang = Path(path).parent.name
if len(lang) != 2 or not lang.isalpha():
lang = detect(text)
data.append({
"text": text,
"metadata": {
"title": title,
"language": lang,
"file_path": path
}
})
return data
def lemmatize_text(text):
if not text:
return ""
lemmatizer = WordNetLemmatizer()
tokens = word_tokenize(text.lower())
lemmatized = [lemmatizer.lemmatize(token) for token in tokens if token.isalnum()]
return " ".join(lemmatized)
def save_jsonl(data_list, path):
with open(path, "w", encoding="utf-8") as f:
for data in data_list:
item = data.copy()
item["lemmatized_text"] = lemmatize_text(data["text"])
f.write(json.dumps(item, ensure_ascii=False) + "\n")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--root", type=str, required=True, help="Root folder Alarms and Manuals")
parser.add_argument("--output_dir", type=str, required=True, help="Output folder JSONL files.")
args = parser.parse_args()
root = Path(args.root)
output_dir = Path(args.output_dir)
alarms = root / "Alarms"
manuals = root / "Manuals"
alarms_jsonl_path = f"{output_dir}/alarms.jsonl"
manuals_jsonl_path = f"{output_dir}/manuals.jsonl"
dataset_jsonl_path = f"{output_dir}/dataset.jsonl"
valid_ext = ["html", "txt"]
nltk.download('punkt_tab')
nltk.download('wordnet')
alarms_paths = []
manuals_paths = []
for file_path in glob.iglob(str(alarms / "**" / "*"), recursive=True):
if os.path.isfile(file_path) and file_path.split(".")[-1] in valid_ext:
alarms_paths.append(file_path)
for file_path in glob.iglob(str(manuals / "**" / "*"), recursive=True):
if os.path.isfile(file_path) and file_path.split(".")[-1] in valid_ext:
manuals_paths.append(file_path)
alarms_data = create_json_data(alarms_paths)
manuals_data = create_json_data(manuals_paths)
os.makedirs(output_dir, exist_ok=True)
save_jsonl(alarms_data, alarms_jsonl_path)
save_jsonl(manuals_data, manuals_jsonl_path)
save_jsonl(alarms_data + manuals_data, dataset_jsonl_path)