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

Method parse

ddparser/run.py:341–411  ·  view source on GitHub ↗

预测未切词的句子。 Args: x: list(str) | str, 未分词的句子,类型为str或list Returns: outputs: list, 依存分析结果 Example: >>> ddp = DDParser() >>> inputs = "百度是一家高科技公司" >>> ddp.parse(inputs) [{'word': ['百度', '是', '一家', '高科技', '公司'], 'h

(self, inputs)

Source from the content-addressed store, hash-verified

339 self.args.batch_size = 50
340
341 def parse(self, inputs):
342 """
343 预测未切词的句子。
344
345 Args:
346 x: list(str) | str, 未分词的句子,类型为str或list
347
348 Returns:
349 outputs: list, 依存分析结果
350
351 Example:
352 >>> ddp = DDParser()
353 >>> inputs = "百度是一家高科技公司"
354 >>> ddp.parse(inputs)
355 [{'word': ['百度', '是', '一家', '高科技', '公司'], 'head': [2, 0, 5, 5, 2], 'deprel': ['SBV', 'HED', 'ATT', 'ATT', 'VOB']}]
356
357 >>> inputs = ["百度是一家高科技公司", "他送了一本书"]
358 >>> ddp.parse(inputs)
359 [{'word': ['百度', '是', '一家', '高科技', '公司'], 'head': [2, 0, 5, 5, 2], 'deprel': ['SBV', 'HED', 'ATT', 'ATT', 'VOB']},
360 {'word': ['他', '送', '了', '一本', '书'], 'head': [2, 0, 2, 5, 2], 'deprel': ['SBV', 'HED', 'MT', 'ATT', 'VOB']}]
361
362 >>> ddp = DDParser(prob=True, use_pos=True)
363 >>> inputs = "百度是一家高科技公司"
364 >>> ddp.parse(inputs)
365 [{'word': ['百度', '是', '一家', '高科技', '公司'], 'postag': ['ORG', 'v', 'm', 'n', 'n'],
366 'head': [2, 0, 5, 5, 2], 'deprel': ['SBV', 'HED', 'ATT', 'ATT', 'VOB'], 'prob': [1.0, 1.0, 1.0, 1.0, 1.0]}]
367 """
368 if not self.lac:
369 self.lac = LAC.LAC(mode="lac" if self.use_pos else "seg", use_cuda=self.args.use_cuda)
370 if not inputs:
371 return
372 if isinstance(inputs, six.string_types):
373 inputs = [inputs]
374 if all([isinstance(i, six.string_types) and i for i in inputs]):
375 lac_results = []
376 position = 0
377 try:
378 inputs = [query if isinstance(query, six.text_type) else query.decode("utf-8") for query in inputs]
379 except UnicodeDecodeError:
380 logging.warning("encoding only supports UTF-8!")
381 return
382
383 while position < len(inputs):
384 lac_results += self.lac.run(inputs[position:position + self.args.batch_size])
385 position += self.args.batch_size
386 predicts = Corpus.load_lac_results(lac_results, self.env.fields)
387 else:
388 logging.warning("please check the foramt of your inputs.")
389 return
390 dataset = TextDataset(predicts, [self.env.WORD, self.env.FEAT], self.args.buckets)
391 # set the data loader
392
393 dataset.loader = batchify(
394 dataset,
395 self.args.batch_size,
396 use_multiprocess=False,
397 sequential_sampler=True if not self.args.buckets else False,
398 )

Callers

nothing calls this directly

Calls 6

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

Tested by

no test coverage detected