class RelationClassifier: """ 关系分类,使用bert进行文本分类 """ def __init__(self): self.predictor = Predictor(Path.relation_classifier_model) def predict(self, text): label, confidence = self.predictor.predict_text(text_a=text) logger.debug('{} {} {}'.format(label, confidence, confidence >= 0.6)) if confidence < 0.6: label = '' return label, confidence
import os import sys import pandas as pd from sklearn import metrics base_path = os.path.join(os.path.dirname(__file__), '..') sys.path.append(base_path) from model.text_classification import Predictor predictor = Predictor(os.path.join(base_path, 'carbot_data/model/kbqapc')) examples = predictor.processor.get_dev_examples(os.path.join(base_path, 'data/train/kbqa/predicate_classification/')) data_df = [] for example in examples: pred, confidence = predictor.predict_text(example.text_a, example.text_b) text = example.text_a true = example.label data_df.append({ 'text': text, 'true': true, 'pred': pred, '是否正确': '是' if true == pred else '否' }) df = pd.DataFrame(data_df, columns=['text', 'true', 'pred', '是否正确']) trues = df['true'] preds = df['pred'] labels = sorted(set(trues)) precision, recall, f1, num = metrics.precision_recall_fscore_support(y_true=trues, y_pred=preds, labels=labels, average=None) precision_total, recall_total, f1_total, num_total = metrics.precision_recall_fscore_support(y_true=trues, y_pred=preds,