Beispiel #1
0
    def process_output(self, guess, truth, sentence_lengths, ids, handle=None, txts=None):

        correct_labels = 0
        total_labels = 0
        truth_n = truth
        # For fscore
        gold_count = 0
        guess_count = 0
        overlap_count = 0
        # For each sentence
        for b in range(len(guess)):

            sentence_length = sentence_lengths[b]
            gold = truth_n[b, :sentence_length]
            sentence = guess[b]
            sentence = sentence[:sentence_length]
            correct_labels += np.sum(np.equal(sentence, gold))
            total_labels += sentence_length
            gold_chunks = to_spans(gold, self.idx2label, self.span_type)
            gold_count += len(gold_chunks)
            guess_chunks = to_spans(sentence, self.idx2label, self.span_type)
            guess_count += len(guess_chunks)

            overlap_chunks = gold_chunks & guess_chunks
            overlap_count += len(overlap_chunks)

            # Should we write a file out?  If so, we have to have txts
            if handle is not None:
                id = ids[b]
                txt = txts[id]
                write_sentence_conll(handle, sentence, gold, txt, self.idx2label)

        return correct_labels, total_labels, overlap_count, gold_count, guess_count
Beispiel #2
0
    def process_output(self,
                       guess,
                       truth,
                       sentence_lengths,
                       ids,
                       handle=None,
                       txts=None):

        correct_labels = 0
        total_labels = 0
        truth_n = truth
        # For fscore
        gold_chunks = []
        pred_chunks = []
        # For each sentence
        for b in range(len(guess)):

            sentence_length = sentence_lengths[b]
            gold = truth_n[b, :sentence_length]
            sentence = guess[b]
            sentence = sentence[:sentence_length]
            correct_labels += np.sum(np.equal(sentence, gold))
            total_labels += sentence_length
            gold_chunks.append(
                set(to_spans(gold, self.idx2label, self.span_type)))
            pred_chunks.append(
                set(to_spans(sentence, self.idx2label, self.span_type)))
            # Should we write a file out?  If so, we have to have txts
            if handle is not None:
                id = ids[b]
                txt = txts[id]
                write_sentence_conll(handle, sentence, gold, txt,
                                     self.idx2label)

        return correct_labels, total_labels, gold_chunks, pred_chunks
Beispiel #3
0
    def process_output(self,
                       guess,
                       truth,
                       sentence_lengths,
                       ids,
                       handle=None,
                       txts=None):

        correct_labels = 0
        total_labels = 0
        truth_n = truth.cpu().numpy()
        # For fscore
        gold_count = 0
        guess_count = 0
        overlap_count = 0

        # For each sentence
        for b in range(len(guess)):

            sentence = guess[b].cpu().numpy()

            sentence_length = sentence_lengths[b]
            gold = truth_n[b, :sentence_length]
            correct_labels += np.sum(np.equal(sentence, gold))
            total_labels += sentence_length
            gold_chunks = to_spans(gold, self.idx2label, self.span_type,
                                   self.verbose)
            gold_count += len(gold_chunks)
            guess_chunks = to_spans(sentence, self.idx2label, self.span_type,
                                    self.verbose)
            guess_count += len(guess_chunks)

            overlap_chunks = gold_chunks & guess_chunks
            overlap_count += len(overlap_chunks)

            # Should we write a file out?  If so, we have to have txts
            if handle is not None:
                id = ids[b]
                txt = txts[id]
                write_sentence_conll(handle, sentence, gold, txt,
                                     self.idx2label)

        return correct_labels, total_labels, overlap_count, gold_count, guess_count
Beispiel #4
0
    def process_batch(self, batch_dict, handle=None, txts=None):

        guess = self.model.predict(batch_dict)
        sentence_lengths = batch_dict[self.model.lengths_key]
        ids = batch_dict['ids']
        truth = batch_dict['y']
        correct_labels = 0
        total_labels = 0

        # For fscore
        gold_count = 0
        guess_count = 0
        overlap_count = 0

        # For each sentence
        for b in range(len(guess)):
            length = sentence_lengths[b]
            assert (length == len(guess[b]))
            sentence = guess[b]
            # truth[b] is padded, cutting at :length gives us back true length
            gold = truth[b][:length]
            correct_labels += np.sum(np.equal(sentence, gold))
            total_labels += length

            gold_chunks = to_spans(gold, self.idx2label, self.span_type,
                                   self.verbose)
            gold_count += len(gold_chunks)

            guess_chunks = to_spans(sentence, self.idx2label, self.span_type,
                                    self.verbose)
            guess_count += len(guess_chunks)

            overlap_chunks = gold_chunks & guess_chunks
            overlap_count += len(overlap_chunks)

            # Should we write a file out?  If so, we have to have txts
            if handle is not None:
                id = ids[b]
                txt = txts[id]
                write_sentence_conll(handle, sentence, gold, txt,
                                     self.idx2label)

        return correct_labels, total_labels, overlap_count, gold_count, guess_count
Beispiel #5
0
    def process_batch(self, batch_dict, handle=None, txts=None):

        guess = self.model.predict(batch_dict)
        sentence_lengths = batch_dict[self.model.lengths_key]
        ids = batch_dict['ids']
        truth = batch_dict['y']
        correct_labels = 0
        total_labels = 0

        # For fscore
        gold_count = 0
        guess_count = 0
        overlap_count = 0

        # For each sentence
        for b in range(len(guess)):
            length = sentence_lengths[b]
            assert(length == len(guess[b]))
            sentence = guess[b]
            # truth[b] is padded, cutting at :length gives us back true length
            gold = truth[b][:length]
            correct_labels += np.sum(np.equal(sentence, gold))
            total_labels += length

            gold_chunks = to_spans(gold, self.idx2label, self.span_type, self.verbose)
            gold_count += len(gold_chunks)

            guess_chunks = to_spans(sentence, self.idx2label, self.span_type, self.verbose)
            guess_count += len(guess_chunks)

            overlap_chunks = gold_chunks & guess_chunks
            overlap_count += len(overlap_chunks)

            # Should we write a file out?  If so, we have to have txts
            if handle is not None:
                id = ids[b]
                txt = txts[id]
                write_sentence_conll(handle, sentence, gold, txt, self.idx2label)

        return correct_labels, total_labels, overlap_count, gold_count, guess_count