MCPcopy Create free account
hub / github.com/NJUNLP/GTS / Metric

Class Metric

code/BertModel/utils.py:43–186  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

41
42
43class Metric():
44 def __init__(self, args, predictions, goldens, bert_lengths, sen_lengths, tokens_ranges, ignore_index=-1):
45 self.args = args
46 self.predictions = predictions
47 self.goldens = goldens
48 self.bert_lengths = bert_lengths
49 self.sen_lengths = sen_lengths
50 self.tokens_ranges = tokens_ranges
51 self.ignore_index = -1
52 self.data_num = len(self.predictions)
53
54 def get_spans(self, tags, length, token_range, type):
55 spans = []
56 start = -1
57 for i in range(length):
58 l, r = token_range[i]
59 if tags[l][l] == self.ignore_index:
60 continue
61 elif tags[l][l] == type:
62 if start == -1:
63 start = i
64 elif tags[l][l] != type:
65 if start != -1:
66 spans.append([start, i - 1])
67 start = -1
68 if start != -1:
69 spans.append([start, length - 1])
70 return spans
71
72 def find_pair(self, tags, aspect_spans, opinion_spans, token_ranges):
73 pairs = []
74 for al, ar in aspect_spans:
75 for pl, pr in opinion_spans:
76 tag_num = [0] * 4
77 for i in range(al, ar + 1):
78 for j in range(pl, pr + 1):
79 a_start = token_ranges[i][0]
80 o_start = token_ranges[j][0]
81 if al < pl:
82 tag_num[int(tags[a_start][o_start])] += 1
83 else:
84 tag_num[int(tags[o_start][a_start])] += 1
85 if tag_num[3] == 0: continue
86 sentiment = -1
87 pairs.append([al, ar, pl, pr, sentiment])
88 return pairs
89
90 def find_triplet(self, tags, aspect_spans, opinion_spans, token_ranges):
91 triplets = []
92 for al, ar in aspect_spans:
93 for pl, pr in opinion_spans:
94 tag_num = [0] * 6
95 for i in range(al, ar + 1):
96 for j in range(pl, pr + 1):
97 a_start = token_ranges[i][0]
98 o_start = token_ranges[j][0]
99 if al < pl:
100 tag_num[int(tags[a_start][o_start])] += 1

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected