MCPcopy Create free account
hub / github.com/baidu/lac / parse_result

Method parse_result

python/LAC/models.py:272–303  ·  view source on GitHub ↗

将SEG模型输出的Tensor转为明文

(self, lines, crf_decode, dataset, words_length)

Source from the content-addressed store, hash-verified

270 return tensor, words_length
271
272 def parse_result(self, lines, crf_decode, dataset, words_length):
273 """将SEG模型输出的Tensor转为明文"""
274 offset_list = crf_decode.lod[0]
275 crf_decode = crf_decode.data.int64_data()
276 batch_size = len(offset_list) - 1
277
278 batch_out = []
279 for sent_index in range(batch_size):
280 begin, end = offset_list[sent_index], offset_list[sent_index + 1]
281
282 sent = lines[sent_index]
283 tags = [dataset.id2label_dict[str(id)]
284 for id in crf_decode[begin:end]]
285 tags_for_rank = []
286
287 if self.custom:
288 self.custom.parse_customization(sent, tags)
289
290 sent_out, tags_out = [], []
291 for ind, tag in enumerate(tags):
292 # for the first char
293 if len(sent_out) == 0 or tag.endswith("B") or tag.endswith("S"):
294 sent_out.append(sent[ind])
295 tags_out.append(tag[:-2])
296 continue
297 sent_out[-1] += sent[ind]
298 # 取最后一个tag作为标签
299 tags_out[-1] = tag[:-2]
300
301 sent_out = [''] if len(sent_out) == 0 else sent_out
302 batch_out.append([sent_out, tags_out, tags_for_rank])
303 return batch_out
304
305class RankModel(Model):
306 """Docstring for Rank Model"""

Callers

nothing calls this directly

Calls 1

parse_customizationMethod · 0.45

Tested by

no test coverage detected