Ejemplo n.º 1
0
 def evaluate_line(self, sess, inputs, id_to_tag):
     trans = self.trans.eval()
     lengths, scores = self.run_step(sess, False, inputs[1:])
     lengths = [lengths]
     batch_paths = self.decode(scores, lengths, trans)
     tags = [id_to_tag[idx] for idx in batch_paths[0]]
     return result_to_json(inputs[0], tags)
Ejemplo n.º 2
0
    def evaluate_line(self):
        with tf.Session(config=tf_config) as sess:
            ### 1. create model and load parameters
            self.model = Model(self.config)
            ckpt = tf.train.get_checkpoint_state(FLAGS.ckpt_path)
            if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path):
                logger.info("Reading model parameters from %s" %
                            ckpt.model_checkpoint_path)
                self.model.saver.restore(sess, ckpt.model_checkpoint_path)
            else:
                logger.info("Created model with fresh parameters.")
                sess.run(tf.global_variables_initializer())

            ### 2. Evaluating
            while True:
                try:
                    line = input("请输入测试句子:")
                    inputs = utils.input_from_line(line, self.ch_2_id)

                    trans = self.model.trans.eval(session=sess)
                    lengths, scores = self.model.run_step(sess, False, inputs)
                    batch_paths = self.model.decode(scores, lengths, trans)
                    tags = [self.id_2_tag[idx] for idx in batch_paths[0]]

                    result = utils.result_to_json(inputs[0][0], tags)
                    print(result)
                except Exception as e:
                    logger.info(e)
Ejemplo n.º 3
0
def predict_line():
    config = load_config(FLAGS.config_file)
    logger = get_logger(FLAGS.log_file)
    tf_config = tf.ConfigProto()
    # 读取词典
    with open(FLAGS.map_file, "rb") as f:
        char_to_id, id_to_char, tag_to_id, id_to_tag = pickle.load(f)
    with tf.Session(config=tf_config) as sess:
        # 根据保存的模型读取模型
        model = create_model(sess, Model, FLAGS.ckpt_path, load_word2vec,
                             config, id_to_char, logger)
        while True:
            # 反复输入句子进行预测
            line = input("请输入测试句子:")
            inputs, tag = model.evaluate_line(
                sess, input_from_line(line, char_to_id), id_to_tag)
            if FLAGS.server:
                # 如果是命令行测试就打印json数据
                result = result_to_json(inputs, tag)
                result = js.dumps(result,
                                  ensure_ascii=False,
                                  indent=4,
                                  separators=(',', ': '))
                with open('./result/result.json', 'w', encoding='utf-8') as f:
                    f.write(result)
                print("预测结果为:{}".format(result))
Ejemplo n.º 4
0
 def evaluate_line(self, sess, inputs, id_to_tag):
     trans = self.trans.eval()
     lengths, scores = self.run_step(sess, False, inputs)
     batch_paths = self.decode(scores, lengths, trans)
     print(id_to_tag)
     print(batch_paths)
     tags = [id_to_tag[batch_paths[0][idx]] for idx in range(len(batch_paths[0])-1)]
     return result_to_json(inputs[0][0], tags)
Ejemplo n.º 5
0
 def evaluate_line(self, sess, inputs, id_to_tag):
     #trans条件随机场分类得出的矩阵
     trans = self.trans.eval(session=sess)
     #score是[句数,字数,label数]
     lengths, scores = self.run_step(sess, False, inputs)
     #viterbi_decode 由显式序列scores和状态转移阵trans,求隐藏序列的最大概率路径,也即最短路径
     batch_paths = self.decode(scores, lengths, trans)
     tags = [id_to_tag[idx] for idx in batch_paths[0]]
     return result_to_json(inputs[0][0], tags)
Ejemplo n.º 6
0
 def evaluate_line(self, sess, inputs, id_to_tag):
     """
     :param sess: session to run the model
     :param inputs:
     :param id_to_tag:  index to tag name
     :return:
     """
     trans = self.trans.eval()
     lengths, scores = self.run_step(sess, False, inputs)
     batch_paths = self.decode(scores, lengths, trans)
     tags = [id_to_tag[idx] for idx in batch_paths[0]]
     return result_to_json(inputs[0][0], tags)
Ejemplo n.º 7
0
 def predict_line(self, sess, sentence, id_to_tag):
     """
     一个语句实例进行实体识别测试
     :param sess:
     :param sentence:
     :param id_to_tag:
     :return:
     """
     trans = self.trans.eval()
     lengths, logits = self.run_step(sess, False, sentence)
     batch_paths = self.decode(logits, lengths, trans)
     tags = [id_to_tag[idx] for idx in batch_paths[0]]
     return result_to_json(sentence[0][0], tags)
Ejemplo n.º 8
0
 def evaluate_line(self, sess, inputs, id_to_tag):
     trans = self.trans.eval()
     lengths, scores = self.evaluate_run(sess, False, inputs)
     strings = inputs[0]
     batch_paths = self.decode(scores, lengths, trans)
     result = []
     for i in range(len(strings)):
         char_input = strings[i][:lengths[i]]
         #print(char_input)
         tags = [id_to_tag[idx] for idx in batch_paths[i][:lengths[i]]]
         json_result = result_to_json(char_input, tags)
         print(json_result)
         result.append(json_result)
     return result
Ejemplo n.º 9
0
 def evaluate_line(self, sess, inputs, id_to_tag):
     # print("evaluate_line:{}".format(inputs))
     # "[['请调出汇坤园北门2016年4月9日中午11:40的录像,四倍速回放'], " \
     # "[[25, 27, 28, 134, 731, 41, 29, 5, 4, 7, 2, 16, 17, 14, 8, 24, 9, 42, 33, 2, 2, 3, 14, 7, 6, 10, 11, 1, 108, 18, 19, 34, 36]], [[0, 1, 3, 1, 2, 3, 1, 3, 1, 2, 2, 3, 0, 0, 0, 0, 0, 1, 3, 1, 3, 0, 1, 3, 0, 1, 3, 0, 1, 3, 0, 1, 3]], " \
     # "[[]]]"
     trans = self.trans.eval()
     # print("trans:{}".format(trans.shape))
     lengths, scores = self.run_step(sess, False, inputs)
     # print("lengths:{}".format(lengths.shape))
     # print("scores:{}".format(scores.shape))
     # trans: (35, 35)
     # lengths: (1,)
     # scores: (1, 33, 34)
     batch_paths = self.decode(scores, lengths, trans)
     # print("batch_paths:{}".format(batch_paths))
     'batch_paths:[[24, 32, 24, 32, 21, 7, 15, 9, 17, 25, 23, 26, 25, 6, 17, 25, 23, 26, 25, 23, 26, 8, 32, 24, 32, 24, 32, 24, 21, 19, 4, 33, 18]]'
     tags = [id_to_tag[idx] for idx in batch_paths[0]]
     # print("tags:{}".format(tags))
     "tags:['B-SPEED', 'S-ELOC', 'B-SPEED', 'S-ELOC', 'B-ETIME', 'B-SLOC', 'B-STIME', 'B-DAY', 'I-SPEED', 'E-SPEED', 'I-DAY', 'I-ROAD', 'E-SPEED', 'E-TYPE', 'I-SPEED', 'E-SPEED', 'I-DAY', 'I-ROAD', 'E-SPEED', 'I-DAY', 'I-ROAD', 'E-SLOC', 'S-ELOC', 'B-SPEED', 'S-ELOC', 'B-SPEED', 'S-ELOC', 'B-SPEED', 'B-ETIME', 'E-YEAR', 'I-ETIME', 'S-SLOC', 'B-YEAR']"
     return result_to_json(inputs[0][0], tags)
Ejemplo n.º 10
0
 def evaluate_line(self, sess, inputs, id_to_tag):
     '''
     inputs: 心脏病、腰部叩击痛阳性和肌无力
     lengths: array([15])
     scores: shape (1, 15, 51)
     trans: 解码用的转移矩阵
     batch_paths: [[13, 2, 14, 0, 6, 7, 10, 3, 3, 3, 11, 0, 0, 0, 0]]
     tags: ['B-DIS', 'I-DIS', 'E-DIS', 'O', 'B-REG', 'E-REG', 'B-SGN', 'I-SGN', 'I-SGN', 'I-SGN', 'E-SGN', 'O', 'O', 'O', 'O']
     return: 
      {
           'entities': [
               {
                   'word': '心脏病',
                   'type': 'DIS',
                   'start': 0,
                   'end': 3
               },
               {
                   'word': '腰部',
                   'type': 'REG',
                   'start': 4,
                   'end': 6
               },
               {
                   'word': '叩击痛阳性',
                   'type': 'SGN',
                   'start': 6,
                   'end': 11
               }
           ],
           'string': '心脏病、腰部叩击痛阳性和肌无力'
       }
     '''
     lengths, scores = self.run_step(sess, False, inputs)
     trans = self.trans.eval(session=sess)
     batch_paths = self.decode(scores, lengths, trans)
     tags = [id_to_tag[idx] for idx in batch_paths[0]]
     return result_to_json(inputs[0][0], tags)
Ejemplo n.º 11
0
 def evaluate_line(self, sess, inputs, label_vocab):
     trans = self.trans.eval()
     lengths, scores = self.run_step(sess, False, inputs)
     batch_paths = self.decode(scores, lengths, trans)
     tags = [label_vocab[idx] for idx in batch_paths[0]]
     return result_to_json(inputs[0][0], tags)
Ejemplo n.º 12
0
 def evaluate_line(self, sess, inputs, id_to_tag):
     trans = self.trans.eval()
     lengths, scores = self.run_step(sess, False, inputs)
     batch_paths = self.decode(scores, lengths, trans)
     tags = [id_to_tag[idx] for idx in batch_paths[0]]
     return result_to_json(inputs[0][0], tags)