Computes TEDS score between the prediction and the ground truth of a given sample
(self, pred, true)
| 101 | return new_node |
| 102 | |
| 103 | def evaluate(self, pred, true): |
| 104 | ''' Computes TEDS score between the prediction and the ground truth of a |
| 105 | given sample |
| 106 | ''' |
| 107 | if (not pred) or (not true): |
| 108 | return 0.0 |
| 109 | pred = "<html>" + pred + "</html>" |
| 110 | true = "<html>" + true + "</html>" |
| 111 | parser = html.HTMLParser(remove_comments=True, encoding='utf-8') |
| 112 | pred = html.fromstring(pred, parser=parser) |
| 113 | true = html.fromstring(true, parser=parser) |
| 114 | if pred.xpath('body/table') and true.xpath('body/table'): |
| 115 | pred = pred.xpath('body/table')[0] |
| 116 | true = true.xpath('body/table')[0] |
| 117 | if self.ignore_nodes: |
| 118 | etree.strip_tags(pred, *self.ignore_nodes) |
| 119 | etree.strip_tags(true, *self.ignore_nodes) |
| 120 | n_nodes_pred = len(pred.xpath(".//*")) |
| 121 | n_nodes_true = len(true.xpath(".//*")) |
| 122 | n_nodes = max(n_nodes_pred, n_nodes_true) |
| 123 | tree_pred = self.load_html_tree(pred) |
| 124 | tree_true = self.load_html_tree(true) |
| 125 | distance = APTED(tree_pred, tree_true, CustomConfig()).compute_edit_distance() |
| 126 | return 1.0 - (float(distance) / n_nodes) |
| 127 | else: |
| 128 | return 0.0 |
| 129 | |
| 130 | def batch_evaluate(self, pred_json, true_json): |
| 131 | ''' Computes TEDS score between the prediction and the ground truth of |
no test coverage detected