def predict_sentiment_file(): model_path = "/home/kuan.li/msnlp/output_models/sentiment" pretrain_type = "bert" task_name = "text_classification" data_dir = "/home/kuan.li/msnlp/data/sentiment" test_file = os.path.join(data_dir, "test.tsv") output_file = os.path.join(data_dir, "test_predict.tsv") test_set = pd.read_table(test_file, sep='\t') texts = {"text": test_set.text.tolist()} # texts = { # "text": [ # "已经被你们坑了两次了", # "草泥马逼的30次都没提现成功", # "还光了吧我", # ] # } predictor = NLPPredictor(task_name, model_path=model_path, pretrain_type=pretrain_type) pres = predictor.predict_rest(texts) test_set["predict_label"] = pres["data"] test_set.to_csv(path_or_buf=output_file, sep='\t', header=False, index=False) print("file saved.") num_correct = 0 for v in test_set.values: if v[1] == v[2]: num_correct = num_correct + 1 print("Accuracy:{}".format(float(num_correct)/len(test_set))) print("total lines:{}".format(len(test_set)))
def predict_xma(): model_path = "/home/dingnan.jin/projects/nlpsdk/testout/xma" pretrain_type = "bert" task_name = "text_classification" texts = { "text": [ "几点到几点上班", "贷20000利息多少", "我不用了,怎么解绑", "立即还款里显示我没借过钱 我怎么还",] } predictor = NLPPredictor(task_name, model_path=model_path, pretrain_type=pretrain_type) pres = predictor.predict_rest(texts) print(pres)
def predict_tnews(): model_path = "/home/kuan.li/msnlp/output_models/tnews" pretrain_type = "bert" task_name = "text_classification" texts = { "text": [ "以色列暗杀叙利亚7名“国家英雄”的行为该如何定性?", "小学生高校食堂写作业:不会的问大学生,他们也做错,只能靠自己", "芜湖有哪些上市公司?", "赵本山在东北的地位如何?最近怎么没有消息了呢?", ] } predictor = NLPPredictor(task_name, model_path=model_path, pretrain_type=pretrain_type) pres = predictor.predict_rest(texts) print(pres)
def predict_sst(): model_path = "/home/dingnan.jin/projects/nlpsdk/testout/SST-2" pretrain_type = "bert" task_name = "text_classification" texts = { "text": [ " it 's just incredibly dull", "it 's a charming and often affecting journey . ", "unflinchingly bleak and desperate", "allows us to hope that nolan is poised to embark a major career as a commercial yet inventive filmmaker .",] } predictor = NLPPredictor(task_name, model_path=model_path, pretrain_type=pretrain_type) pres = predictor.predict_rest(texts) print(pres)