-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_object_reference.py
More file actions
220 lines (188 loc) · 7.27 KB
/
Copy pathparse_object_reference.py
File metadata and controls
220 lines (188 loc) · 7.27 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# parse_object_reference.py
import json
from pathlib import Path
from bs4 import BeautifulSoup
OBJ_REF_DIR = Path("object_reference/pd.iem.sh/objects")
BASE_URL = "https://pd.iem.sh/objects"
def get_source_url(filepath):
parts = filepath.parts
obj_idx = parts.index("objects")
rel = parts[obj_idx + 1:]
if len(rel) == 1:
return f"{BASE_URL}/{rel[0].replace('.html', '')}"
else:
return f"{BASE_URL}/{rel[0]}/"
def parse_iolet_list(div):
"""Parse the nested ul/li iolet structure into 'slot | type | type' rows."""
rows = []
ul = div.find("ul")
if not ul:
return rows
current_slot = None
slot_types = []
for child in ul.children:
if not hasattr(child, "name") or child.name is None:
continue
if child.name == "li":
if current_slot is not None:
suffix = " | ".join(slot_types)
rows.append(f"{current_slot} | {suffix}" if suffix else current_slot)
current_slot = child.get_text(strip=True)
slot_types = []
elif child.name == "ul":
for li in child.find_all("li", recursive=False):
t = li.get_text(" ", strip=True)
if t:
slot_types.append(t)
if current_slot is not None:
suffix = " | ".join(slot_types)
rows.append(f"{current_slot} | {suffix}" if suffix else current_slot)
return rows
def parse_list(element):
items = []
for li in element.find_all("li", recursive=False):
items.append("- " + li.get_text(" ", strip=True))
return "\n".join(items)
def parse_table(element):
rows = []
for row in element.find_all("tr"):
cells = [cell.get_text(strip=True) for cell in row.find_all(["td", "th"])]
rows.append(" | ".join(cells))
return "\n".join(rows)
def parse_file(filepath):
with open(filepath, encoding="utf-8") as f:
soup = BeautifulSoup(f.read(), "html.parser")
url = get_source_url(filepath)
for tag in soup.select("header, footer"):
tag.decompose()
main = soup.select_one("main")
if not main:
return []
# Object name — skip non-object pages (e.g. index listing)
obj_name_tag = main.find("h3", class_="pdobj")
if not obj_name_tag:
return []
object_name = obj_name_tag.get_text(strip=True)
# Short description
description = ""
for p in main.find_all("p", recursive=False):
if "105%" in p.get("style", ""):
description = p.get_text(strip=True)
break
# Abbreviation line, e.g. "Abbreviation: t"
abbreviation = ""
for p in main.find_all("p", recursive=False):
if p.get_text(" ", strip=True).startswith("Abbreviation:"):
abbreviation = p.get_text(" ", strip=True)
break
# Inlets: try table rows first (some pages), fall back to ul/li structure
inlet_rows = []
for row in soup.select(".inlets tr, #inlets tr"):
cells = [td.get_text().strip() for td in row.select("td")]
if cells:
inlet_rows.append(" | ".join(cells))
if not inlet_rows:
inlets_div = main.find("div", id="inlets")
if inlets_div:
inlet_rows = parse_iolet_list(inlets_div)
# Outlets
outlet_rows = []
for row in soup.select(".outlets tr, #outlets tr"):
cells = [td.get_text().strip() for td in row.select("td")]
if cells:
outlet_rows.append(" | ".join(cells))
if not outlet_rows:
outlets_div = main.find("div", id="outlets")
if outlets_div:
outlet_rows = parse_iolet_list(outlets_div)
# Arguments / creation arguments
arg_rows = []
for row in soup.select(".arguments tr, #arguments tr"):
cells = [td.get_text().strip() for td in row.select("td")]
if cells:
arg_rows.append(" | ".join(cells))
if not arg_rows:
args_div = main.find("div", id="arguments")
if args_div:
for li in args_div.find_all("li"):
t = li.get_text(" ", strip=True)
if t:
arg_rows.append(t)
# See also
seealso_div = main.find("div", id="reference-seealso")
seealso = []
if seealso_div:
seealso = [a.get_text(strip=True) for a in seealso_div.find_all("a", class_="pdobj")]
# Build structured text optimized for embedding
text_parts = [f"Object: {object_name}", f"Description: {description}"]
if abbreviation:
text_parts.append(abbreviation)
if inlet_rows:
text_parts.append("Inlets:\n" + "\n".join(inlet_rows))
if outlet_rows:
text_parts.append("Outlets:\n" + "\n".join(outlet_rows))
if arg_rows:
text_parts.append("Arguments:\n" + "\n".join(arg_rows))
if seealso:
text_parts.append("See also: " + ", ".join(seealso))
# Mark pre-processed elements to avoid repeating them below
processed = {id(obj_name_tag)}
for p in main.find_all("p", recursive=False):
if "105%" in p.get("style", ""):
processed.add(id(p))
break
for p in main.find_all("p", recursive=False):
if p.get_text(" ", strip=True).startswith("Abbreviation:"):
processed.add(id(p))
break
ref_div = main.find("div", class_="object-reference")
if ref_div:
processed.add(id(ref_div))
# Additional narrative paragraphs, tables, and sub-section content
extra_parts = []
for element in main.children:
if not hasattr(element, "name") or element.name is None:
continue
if id(element) in processed:
continue
if element.name == "h3":
extra_parts.append("\n" + element.get_text(" ", strip=True))
elif element.name == "h4":
extra_parts.append("#### " + element.get_text(" ", strip=True))
elif element.name == "p":
t = element.get_text(" ", strip=True)
if t:
extra_parts.append(t)
elif element.name == "pre":
extra_parts.append("[CODE]\n" + element.get_text() + "\n[/CODE]")
elif element.name in ("ul", "ol"):
extra_parts.append(parse_list(element))
elif element.name == "table":
extra_parts.append(parse_table(element))
if extra_parts:
text_parts.append("\n".join(extra_parts))
return [{
"heading_path": object_name,
"text": "\n\n".join(text_parts),
"url": url,
"source": "iem_reference",
"content_type": "object_reference",
"object_name": object_name,
}]
all_records = []
# Direct .html files in objects/ (e.g. expr.html); skip index.html and any
# that also have a same-named subdirectory (the subdir version is canonical)
for filepath in sorted(OBJ_REF_DIR.glob("*.html")):
if filepath.name == "index.html":
continue
if (OBJ_REF_DIR / filepath.stem / "index.html").exists():
continue
records = parse_file(filepath)
all_records.extend(records)
# Subdirectory pages: objects/{name}/index.html
for filepath in sorted(OBJ_REF_DIR.glob("*/index.html")):
records = parse_file(filepath)
all_records.extend(records)
with open("parsed_object_reference.json", "w") as f:
json.dump(all_records, f, indent=2)
print(f"Total: {len(all_records)} records from {len(list(OBJ_REF_DIR.glob('*/index.html')))} object pages")