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

Class Instance

code/NNModel/data.py:28–88  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

26
27
28class Instance(object):
29 def __init__(self, sentence_pack, word2index, args):
30 self.id = sentence_pack['id']
31 self.sentence = sentence_pack['sentence']
32 self.sentence_tokens = torch.zeros(args.max_sequence_len).long()
33
34 '''generate sentence tokens'''
35 words = self.sentence.split()
36 self.length = len(words)
37 for i, w in enumerate(words):
38 # word = w.lower()
39 word = w
40 if word in word2index:
41 self.sentence_tokens[i] = word2index[word]
42 else:
43 self.sentence_tokens[i] = word2index['<unk>']
44
45 self.aspect_tags = torch.zeros(args.max_sequence_len).long()
46 self.opinion_tags = torch.zeros(args.max_sequence_len).long()
47 self.aspect_tags[self.length:] = -1
48 self.opinion_tags[self.length:] = -1
49 self.tags = torch.zeros(args.max_sequence_len, args.max_sequence_len).long()
50 self.tags[:, :] = -1
51
52 for i in range(self.length):
53 for j in range(i, self.length):
54 self.tags[i][j] = 0
55 for pair in sentence_pack['triples']:
56 aspect = pair['target_tags']
57 opinion = pair['opinion_tags']
58 aspect_span = get_spans(aspect)
59 opinion_span = get_spans(opinion)
60
61 for l, r in aspect_span:
62 for i in range(l, r+1):
63 self.aspect_tags[i] = 1 if i == l else 2
64 self.tags[i][i] = 1
65 if i > l: self.tags[i-1][i] = 1
66 for j in range(i, r+1):
67 self.tags[i][j] = 1
68 for l, r in opinion_span:
69 for i in range(l, r+1):
70 self.opinion_tags[i] = 1 if i == l else 2
71 self.tags[i][i] = 2
72 if i > l: self.tags[i-1][i] = 2
73 for j in range(i, r+1):
74 self.tags[i][j] = 2
75 for al, ar in aspect_span:
76 for pl, pr in opinion_span:
77 for i in range(al, ar+1):
78 for j in range(pl, pr+1):
79 if args.task == 'pair':
80 if i > j: self.tags[j][i] = 3
81 else: self.tags[i][j] = 3
82 elif args.task == 'triplet':
83 if i > j: self.tags[j][i] = sentiment2id[pair['sentiment']]
84 else: self.tags[i][j] = sentiment2id[pair['sentiment']]
85

Callers 1

load_data_instancesFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected