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

Method prune

webarena/browser_env/html_tools/html_parser.py:366–396  ·  view source on GitHub ↗
(self, tree: html.HtmlElement, nodes_to_keep: list[str])

Source from the content-addressed store, hash-verified

364 return list(nodes_to_keep)
365
366 def prune(self, tree: html.HtmlElement, nodes_to_keep: list[str]) -> html.HtmlElement:
367 # remove nodes not in nodes_to_keep
368 for node in tree.xpath('//*')[::-1]:
369 if node.tag != 'text':
370 is_keep = node.attrib.get(self.id_attr, '') in nodes_to_keep
371 is_candidate = node.attrib.get(self.id_attr, '') in self.keep
372 else:
373 is_keep = (node.getparent().attrib.get(self.id_attr, '') in nodes_to_keep)
374 is_candidate = (node.getparent().attrib.get(self.id_attr, '') in self.keep)
375
376 if not is_keep and node.getparent() is not None:
377 # insert all children into parent
378 for child in node.getchildren():
379 node.addprevious(child)
380 node.getparent().remove(node)
381 else:
382 # if not is_candidate or node.tag == 'text':
383 # node.attrib.pop(self.id_attr, None)
384 if (
385 len(node.attrib) == 0
386 and not any([x.tag == 'text' for x in node.getchildren()])
387 and node.getparent() is not None
388 and node.tag != "text"
389 and len(node.getchildren()) <= 1
390 ):
391 # insert all children into parent
392 for child in node.getchildren():
393 node.addprevious(child)
394 node.getparent().remove(node)
395
396 return tree
397
398 def prune_tree(self, dfs_count: int=1, max_depth: int=3, max_children: int=30,
399 max_sibling: int=3, keep_parent: bool=False) -> None:

Callers 2

prune_treeMethod · 0.95
get_segmentMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected