MCPcopy Create free account
hub / github.com/baidu/DDParser / parse_seg

Method parse_seg

ddparser/run.py:413–467  ·  view source on GitHub ↗

预测已切词的句子。 Args: x: list(list(str)), 已分词的句子,类型为list Returns: outputs: list, 依存分析结果 Example: >>> ddp = DDParser() >>> inputs = [['百度', '是', '一家', '高科技', '公司'], ['他', '送', '了', '一本', '书']] >>> ddp.parse_seg(inputs)

(self, inputs)

Source from the content-addressed store, hash-verified

411 return outputs
412
413 def parse_seg(self, inputs):
414 """
415 预测已切词的句子。
416
417 Args:
418 x: list(list(str)), 已分词的句子,类型为list
419
420 Returns:
421 outputs: list, 依存分析结果
422
423 Example:
424 >>> ddp = DDParser()
425 >>> inputs = [['百度', '是', '一家', '高科技', '公司'], ['他', '送', '了', '一本', '书']]
426 >>> ddp.parse_seg(inputs)
427 [{'word': ['百度', '是', '一家', '高科技', '公司'], 'head': [2, 0, 5, 5, 2], 'deprel': ['SBV', 'HED', 'ATT', 'ATT', 'VOB']},
428 {'word': ['他', '送', '了', '一本', '书'], 'head': [2, 0, 2, 5, 2], 'deprel': ['SBV', 'HED', 'MT', 'ATT', 'VOB']}]
429
430
431 >>> ddp = DDParser(prob=True)
432 >>> inputs = [['百度', '是', '一家', '高科技', '公司']]
433 >>> ddp.parse_seg(inputs)
434 [{'word': ['百度', '是', '一家', '高科技', '公司'], 'head': [2, 0, 5, 5, 2],
435 'deprel': ['SBV', 'HED', 'ATT', 'ATT', 'VOB'], 'prob': [1.0, 1.0, 1.0, 1.0, 1.0]}]
436 """
437 if not inputs:
438 return
439 if all([isinstance(i, list) and i and all(i) for i in inputs]):
440 predicts = Corpus.load_word_segments(inputs, self.env.fields)
441 else:
442 logging.warning("please check the foramt of your inputs.")
443 return
444 dataset = TextDataset(predicts, [self.env.WORD, self.env.FEAT], self.args.buckets)
445 # set the data loader
446 dataset.loader = batchify(
447 dataset,
448 self.args.batch_size,
449 use_multiprocess=False,
450 sequential_sampler=True if not self.args.buckets else False,
451 )
452 pred_arcs, pred_rels, pred_probs = epoch_predict(self.env, self.args, self.model, dataset.loader)
453
454 if self.args.buckets:
455 indices = np.argsort(np.array([i for bucket in dataset.buckets.values() for i in bucket]))
456 else:
457 indices = range(len(pred_arcs))
458 predicts.head = [pred_arcs[i] for i in indices]
459 predicts.deprel = [pred_rels[i] for i in indices]
460 if self.args.prob:
461 predicts.prob = [pred_probs[i] for i in indices]
462
463 outputs = predicts.get_result()
464 if outputs[0].get("postag", None):
465 for output in outputs:
466 del output["postag"]
467 return outputs
468
469 def _get_abs_path(self, path):
470 return os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), path))

Callers

nothing calls this directly

Calls 6

TextDatasetClass · 0.90
batchifyFunction · 0.90
epoch_predictFunction · 0.90
load_word_segmentsMethod · 0.80
valuesMethod · 0.80
get_resultMethod · 0.45

Tested by

no test coverage detected