Download data/parser/to_mrp/sequential_parser.py from ltg/ssa-perin: direct link, hf CLI and curl.
- Browser
- Download file 1.66 kB
-
https://huggingface.co/ltg/ssa-perin/resolve/main/data/parser/to_mrp/sequential_parser.py
- Command line
-
hf download hf://ltg/ssa-perin/data/parser/to_mrp/sequential_parser.py
-
curl -L -o sequential_parser.py https://huggingface.co/ltg/ssa-perin/resolve/main/data/parser/to_mrp/sequential_parser.py
1.66 kB
| #!/usr/bin/env python3 | |
| # coding=utf-8 | |
| from data.parser.to_mrp.abstract_parser import AbstractParser | |
| class SequentialParser(AbstractParser): | |
| def parse(self, prediction): | |
| output = {} | |
| output["id"] = self.dataset.id_field.vocab.itos[prediction["id"].item()] | |
| output["nodes"] = self.create_nodes(prediction) | |
| output["nodes"] = self.create_anchors(prediction, output["nodes"], join_contiguous=True, at_least_one=True, mode="anchors") | |
| output["nodes"] = self.create_anchors(prediction, output["nodes"], join_contiguous=True, at_least_one=False, mode="source anchors") | |
| output["nodes"] = self.create_anchors(prediction, output["nodes"], join_contiguous=True, at_least_one=False, mode="target anchors") | |
| output["edges"], output["nodes"] = self.create_targets_sources(output["nodes"]) | |
| return output | |
| def create_targets_sources(self, nodes): | |
| edges, new_nodes = [], [] | |
| for i, node in enumerate(nodes): | |
| new_node_id = len(nodes) + len(new_nodes) | |
| if len(node["source anchors"]) > 0: | |
| new_nodes.append({"id": new_node_id, "label": "Source", "anchors": node["source anchors"]}) | |
| edges.append({"source": i, "target": new_node_id, "label": ""}) | |
| new_node_id += 1 | |
| del node["source anchors"] | |
| if len(node["target anchors"]) > 0: | |
| new_nodes.append({"id": new_node_id, "label": "Target", "anchors": node["target anchors"]}) | |
| edges.append({"source": i, "target": new_node_id, "label": ""}) | |
| del node["target anchors"] | |
| return edges, nodes + new_nodes | |