Exemplo n.º 1
0
    def eval_instance(self, best_path, gold):
        """
        update statics for one instance

        args: 
            best_path (seq_len): predicted
            gold (seq_len): ground-truth
        """
        total_labels = len(best_path)
        correct_labels = np.sum(np.equal(best_path, gold))
        gold_chunks = utils.iobes_to_spans(gold, self.r_l_map)
        gold_count = len(gold_chunks)

        guess_chunks = utils.iobes_to_spans(best_path, self.r_l_map)
        guess_count = len(guess_chunks)

        # tests code
        self.gold_cnter += Counter([self.r_l_map[idx] for idx in gold])
        self.pred_cnter += Counter([self.r_l_map[idx] for idx in best_path])
        ############

        overlap_chunks = gold_chunks & guess_chunks
        overlap_count = len(overlap_chunks)

        return correct_labels, total_labels, gold_count, guess_count, overlap_count
Exemplo n.º 2
0
    def eval_sent(self, pred, gold):
        """
        update statics for one instance

        args:
            pred (seq_len): predicted
            gold (seq_len): ground-truth
        """
        total_labels = len(pred)
        correct_labels = sum(p == g for p, g in zip(pred, gold))
        gold_chunks = utils.iobes_to_spans(gold)
        gold_count = len(gold_chunks)

        guess_chunks = utils.iobes_to_spans(pred)
        guess_count = len(guess_chunks)

        overlap_chunks = gold_chunks & guess_chunks
        overlap_count = len(overlap_chunks)

        self.correct_labels += correct_labels
        self.total_labels += total_labels
        self.gold_count += gold_count
        self.guess_count += guess_count
        self.overlap_count += overlap_count

        return correct_labels, total_labels, gold_count, guess_count, overlap_count
Exemplo n.º 3
0
    def eval_instance(pred, ref):
        """
        update statics for one instance
        """
        total_labels = len(pred)
        correct_labels = np.sum(np.equal(pred, ref))
        gold_chunks = iobes_to_spans(ref, r_l_map)
        gold_count = len(gold_chunks)

        guess_chunks = iobes_to_spans(pred, r_l_map)
        guess_count = len(guess_chunks)

        overlap_chunks = gold_chunks & guess_chunks
        overlap_count = len(overlap_chunks)

        return correct_labels, total_labels, gold_count, guess_count, overlap_count
Exemplo n.º 4
0
    def eval_instance(self, best_path, gold):
        """
        update statics for one instance

        args:
            best_path (seq_len): predicted
            gold (seq_len): ground-truth
        """

        total_labels = len(best_path)
        correct_labels = np.sum(np.equal(best_path, gold))

        for i in range(total_labels):
            gold_label = self.r_l_map[gold[i]]
            guessed_label = self.r_l_map[best_path[i]]
            self.totalp_counts[gold_label] = 1 + self.totalp_counts.get(
                gold_label, 0)
            if gold_label == guessed_label:
                self.truep_counts[gold_label] = 1 + self.truep_counts.get(
                    gold_label, 0)
            else:
                val = self.fn_counts.get(gold_label, {})
                val[guessed_label] = 1 + val.get(guessed_label, 0)
                self.fn_counts[gold_label] = val

                val2 = self.fp_counts.get(guessed_label, {})
                val2[gold_label] = 1 + val2.get(gold_label, 0)
                self.fp_counts[guessed_label] = val2

        gold_chunks = utils.iobes_to_spans(gold, self.r_l_map)
        # gold_chunks=set([i for i in gold_chunks if i[0:i.find('@')]!='other'])
        gold_count = len(gold_chunks)

        guess_chunks = utils.iobes_to_spans(best_path, self.r_l_map)
        # guess_chunks=set([i for i in guess_chunks if i[0:i.find('@')]!='other'])
        guess_count = len(guess_chunks)

        overlap_chunks = gold_chunks & guess_chunks
        overlap_count = len(overlap_chunks)

        return correct_labels, total_labels, gold_count, guess_count, overlap_count
Exemplo n.º 5
0
    def eval_instance(self, best_path, gold):
        """
        update statics for one instance

        args:
            best_path (seq_len): predicted
            gold (seq_len): ground-truth
        """

        total_labels = len(best_path)
        correct_labels = np.sum(np.equal(best_path, gold))
        for i in range(total_labels):
            gold_label = self.r_l_map[gold[i]]
            guessed_label = self.r_l_map[best_path[i]]
            self.totalp_counts[gold_label] = 1 + self.totalp_counts.get(gold_label,0)
            if gold_label == guessed_label:
                self.truep_counts[gold_label] = 1 + self.truep_counts.get(gold_label,0)
            else:
                val = self.fn_counts.get(gold_label,{})
                val[guessed_label] = 1+ val.get(guessed_label,0)
                self.fn_counts[gold_label]=val

                val2 = self.fp_counts.get(guessed_label,{})
                val2[gold_label] = 1+ val2.get(gold_label,0)
                self.fp_counts[guessed_label] = val2

        gold_chunks = utils.iobes_to_spans(gold, self.r_l_map)
        gold_count = len(gold_chunks)

        guess_chunks = utils.iobes_to_spans(best_path, self.r_l_map)
        guess_count = len(guess_chunks)

        overlap_chunks = gold_chunks & guess_chunks
        overlap_count = len(overlap_chunks)

        return correct_labels, total_labels, gold_count, guess_count, overlap_count