MCPcopy Create free account
hub / github.com/THUDM/AutoWebGLM / get_keep_elements

Method get_keep_elements

miniwob++/html_tools/html_parser.py:283–349  ·  view source on GitHub ↗
(self, tree: html.HtmlElement, keep: list[str], max_depth: int, max_children: int, 
                            max_sibling: int, dfs_count: int=1, keep_parent: bool=False)

Source from the content-addressed store, hash-verified

281
282 # From mind2web, https://github.com/OSU-NLP-Group/Mind2Web/blob/main/src/data_utils/dom_utils.py
283 def get_keep_elements(self, tree: html.HtmlElement, keep: list[str], max_depth: int, max_children: int,
284 max_sibling: int, dfs_count: int=1, keep_parent: bool=False) -> list[str]:
285 def get_anscendants(node: html.HtmlElement, max_depth: int, current_depth: int=0) -> list[str]:
286 if current_depth > max_depth:
287 return []
288
289 anscendants = []
290 parent = node.getparent()
291 if parent is not None:
292 anscendants.append(parent)
293 anscendants.extend(get_anscendants(parent, max_depth, current_depth + 1))
294
295 return anscendants
296
297 def get_descendants(node: html.HtmlElement, max_depth: int, current_depth: int=0) -> list[str]:
298 if current_depth > max_depth:
299 return []
300
301 descendants = []
302 for child in node:
303 descendants.append(child)
304 descendants.extend(get_descendants(child, max_depth, current_depth + 1))
305
306 return descendants
307
308 to_keep = set(copy.deepcopy(keep))
309 nodes_to_keep = set()
310
311 for _ in range(max(1, dfs_count)):
312 for bid in to_keep:
313 candidate_node = self.get_node_by_bid(tree, bid)
314 if candidate_node is None:
315 continue
316
317 nodes_to_keep.add(candidate_node.attrib[self.id_attr])
318 # get all ancestors or with max depth
319 nodes_to_keep.update([x.attrib.get(self.id_attr, '') for x in get_anscendants(candidate_node, max_depth)])
320
321 # get descendants with max depth
322 nodes_to_keep.update([x.attrib.get(self.id_attr, '') for x in get_descendants(candidate_node, max_depth)][:max_children])
323 # get siblings within range
324 parent = candidate_node.getparent()
325 if parent is None:
326 continue
327
328 siblings = [x for x in parent.getchildren() if x.tag != 'text']
329 if candidate_node not in siblings:
330 continue
331
332 idx_in_sibling = siblings.index(candidate_node)
333 nodes_to_keep.update([x.attrib.get(self.id_attr, '')
334 for x in siblings[max(0, idx_in_sibling - max_sibling) : idx_in_sibling + max_sibling + 1]])
335
336 max_children = int(max_children * 0.5)
337 max_depth = int(max_depth * 0.5)
338 max_sibling = int(max_sibling * 0.7)
339
340 to_keep = copy.deepcopy(nodes_to_keep)

Callers 2

prune_treeMethod · 0.95
get_segmentMethod · 0.95

Calls 1

get_node_by_bidMethod · 0.95

Tested by

no test coverage detected