This code is mainly used to test the interface of the trained NER model

Use process:

Curl -f ‘query= {sen}’ 192.168.1.106:5662/ner

2, prepare test data: example.dev.txt

NER tag has 4 <name>, <position>, <organization> and <adress>, annotated in BMEO mode, and corresponding parts of the code can be modified according to their own data annotation format

3. Test code test.py

#! /usr/bin/env python # coding: Utf-8 import re import OS import json from TQDM import TQDM def read_data(file_path): sentence = [] label = [] sen = '' tag = '' with open(file_path, 'r') as f: for line in f.readlines(): if(line ! = '\n'): line = line.split(' ') sen += line[0] tag += line[1] else: Sentence.append (sen) label.append(tag) sen = "" tag =" "return sentence.append(sen) label.append(tag) sen =" " Return the tag def predict(sent): sen = sent.replace("'", Popen ("curl -f 'query= {sen}' 192.168.1.106:5662/ner").read() res = json. Loads (res) Predict_label = ['O']*len(sen) for key in res['label'].keys(): continue if(res['label'][key]): #print(res['label']) for v in res['label'][key].values(): start = v[0][0] end = v[0][1] for i in range(start, end+1): if(i == start): predict_label[i] = 'B-'+str(key) elif(i ! = start and i ! = end): predict_label[i] = 'M-'+str(key) elif(i == end): [I] = 'E-'+ STR (key) def split_entity(label_sequence): entity_mark = dict() entity_pointer = None for index, label in enumerate(label_sequence): If label. Startswith ('B'): # if(label. Split ('-')[1]! = 'position'): # continue category = label.split('-')[1] entity_pointer = (index, category) entity_mark.setdefault(entity_pointer, [label]) elif label.startswith('M'): # if(label.split('-')[1] ! = 'position'): # continue if entity_pointer is None: continue if entity_pointer[1] ! = label.split('-')[1]: continue entity_mark[entity_pointer].append(label) elif label.startswith('E'): # if(label.split('-')[1] ! = 'position'): # continue if entity_pointer is None: continue if entity_pointer[1] ! = label.split('-')[1]: continue entity_mark[entity_pointer].append(label) else: Entity_pointer = None return entity_mark # Def evaluate_one(real_label, predict_label) real_entity_mark = split_entity(real_label) predict_entity_mark = split_entity(predict_label) true_entity_mark = dict() key_set = real_entity_mark.keys() and predict_entity_mark.keys() for key in key_set: real_entity = real_entity_mark.get(key) predict_entity = predict_entity_mark.get(key) if real_entity ! = None and predict_entity ! = None: if tuple(real_entity) == tuple(predict_entity): true_entity_mark.setdefault(key, real_entity) real_entity_num = len(real_entity_mark) predict_entity_num = len(predict_entity_mark) true_entity_num = Len (true_entity_mark) return real_entity_num, predict_entity_num, true_entity_num sentence, label = read_data(file_path) real_entity_all = 0 predict_entity_all = 0 true_entity_all = 0 for i in tqdm(range(len(sentence))): predict_label = predict(sentence[i]) real_label = label[i].split('\n') real_entity_num, predict_entity_num, true_entity_num = evaluate_one(real_label, predict_label) real_entity_all += real_entity_num predict_entity_all += predict_entity_num true_entity_all += true_entity_num #print("{:.3f}, {:.3f}, {:.3f}".format(real_entity_all,predict_entity_all,true_entity_all)) precision = true_entity_all / predict_entity_all recall = true_entity_all / real_entity_all f1 = 2 * precision * recall / (precision + recall) return precision, recall, f1 if __name__ == '__main__': File_path = 'example.dev. TXT 'precision, recall, f1 = evaluate(file_path) print("precision, recall, and f1:") print("{:.3f}, {:.3f}, {:.3f}".format(precision,recall,f1))Copy the code

4. Put the data set and code in the same path

5, Run Python test.py, output precision, recall, f1:

Note:

1, the code can modify the path of the test data

2. In the predict() function, you can modify the interface of the test and modify the corresponding code to filter out tags that you do not want to test

The split_entity() function can modify the code to test only one label

Reference: zhuanlan.zhihu.com/p/56582082