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 | class LayoutKGConstruction(KGConstructionBase):
"""
Constructs a layout knowledge graph from markdown documents.
The output is a JSON file for each document containing layout elements.
"""
def __init__(self, project_id: str):
super().__init__(project_id)
self.md = markdown.Markdown(extensions=["tables", "fenced_code"])
def _parse_html_element(self, element: BeautifulSoup) -> List[Dict[str, str]]:
"""
Parse an HTML element and extract layout information.
Args:
element: BeautifulSoup element from parsed markdown
Returns:
list: List of element information including id, text, and label
"""
elements = []
# Skip empty elements
if not element.text.strip():
return elements
# Generate element ID
element_id = f"p_{str(uuid.uuid4())}"
# Map HTML tags to layout labels
tag_to_label = {
"h1": "H1",
"h2": "H2",
"h3": "H3",
"h4": "H4",
"h5": "H5",
"h6": "H6",
"p": "P",
"li": "LI",
"ol": "OL",
"ul": "UL",
"blockquote": "QUOTE",
"pre": "CODE",
"code": "CODE",
"table": "TABLE",
"tr": "TR",
"td": "TD",
"th": "TH",
}
# Get the element's tag name
tag = element.name
# If it's a recognized tag, create an element entry
if tag in tag_to_label:
elements.append(
{
"id": element_id,
"text": element.get_text().strip(),
"label": tag_to_label[tag],
"entities": [],
"relations": [],
}
)
# Recursively process child elements
for child in element.children:
if hasattr(child, "name") and child.name is not None:
elements.extend(self._parse_html_element(child))
return elements
def _process_document(self, content: str, filename: str) -> Dict[str, Any]:
"""
Process a single markdown document and extract its layout elements.
Args:
content: Content of the markdown file
filename: Name of the document
Returns:
dict: Structured document information with layout elements
"""
# Convert markdown to HTML
html = self.md.convert(content)
# Parse HTML
soup = BeautifulSoup(html, "html.parser")
# Extract elements
elements = []
for element in soup.find_all(recursive=False):
elements.extend(self._parse_html_element(element))
return {
"filename": filename,
"data": elements,
"metadata": {
"title": filename,
},
}
def construct(self, docs: List[Dict[str, str]]) -> Dict[str, Any]:
"""
Construct the layout knowledge graph from a list of documents.
Args:
docs: List of documents, where each document is a dict containing
'content' and 'filename' keys
Returns:
dict: Layout knowledge graph containing all processed documents
"""
layout_kg = {}
# output a layout schema json
layout_schema = {
"H1": ["H2", "P", "LI", "OL", "UL", "QUOTE", "CODE", "TABLE"],
"H2": ["H3", "P", "LI", "OL", "UL", "QUOTE", "CODE", "TABLE"],
"H3": ["H4", "P", "LI", "OL", "UL", "QUOTE", "CODE", "TABLE"],
"H4": ["H5", "P", "LI", "OL", "UL", "QUOTE", "CODE", "TABLE"],
"H5": ["H6", "P", "LI", "OL", "UL", "QUOTE", "CODE", "TABLE"],
"H6": ["P", "LI", "OL", "UL", "QUOTE", "CODE", "TABLE"],
"P": ["P", "LI", "OL", "UL", "QUOTE", "CODE", "TABLE"],
"LI": ["LI", "OL", "UL", "P"],
"OL": ["LI", "OL", "UL", "P"],
"UL": ["LI", "OL", "UL", "P"],
"QUOTE": ["P", "LI", "OL", "UL", "CODE"],
"CODE": ["CODE"],
"TABLE": ["TR"],
"TR": ["TD", "TH"],
"TD": ["P"],
"TH": ["P"],
}
output_path = self.layout_folder / "schema.json"
with open(output_path, "w", encoding="utf-8") as f:
json.dump(layout_schema, f, indent=2, ensure_ascii=False)
for doc in docs:
content = doc["content"]
filename = doc["filename"]
# Process the document
doc_kg = self._process_document(content, filename)
# Save individual document KG
output_path = self.layout_folder / f"{filename}.json"
with open(output_path, "w", encoding="utf-8") as f:
json.dump(doc_kg, f, indent=2, ensure_ascii=False)
# Add to the complete KG
layout_kg[filename] = doc_kg
return layout_kg
|