python-2.7 - spacy 是否将 token 列表作为输入?

标签 python-2.7 tokenize spacy dependency-parsing

我想使用 spacy 的 POS 标记、NER 和依存解析,而不使用单词标记化。事实上,我的输入是代表一个句子的标记列表,我想尊重用户的标记化。 无论是使用 spacy 还是任何其他 NLP 包,这是否可能?

现在,我正在使用这个基于 spacy 的函数以 Conll 格式放置一个句子(一个 unicode 字符串):

import spacy
nlp = spacy.load('en')
def toConll(string_doc, nlp):
   doc = nlp(string_doc)
   block = []
   for i, word in enumerate(doc):
          if word.head == word:
                  head_idx = 0
          else:
                  head_idx = word.head.i - doc[0].i + 1
          head_idx = str(head_idx)
          line = [str(i+1), str(word), word.lemma_, word.tag_,
                      word.ent_type_, head_idx, word.dep_]
          block.append(line)
   return block
conll_format = toConll(u"Donald Trump is the new president of the United States of America")

Output:
[['1', 'Donald', u'donald', u'NNP', u'PERSON', '2', u'compound'],
 ['2', 'Trump', u'trump', u'NNP', u'PERSON', '3', u'nsubj'],
 ['3', 'is', u'be', u'VBZ', u'', '0', u'ROOT'],
 ['4', 'the', u'the', u'DT', u'', '6', u'det'],
 ['5', 'new', u'new', u'JJ', u'', '6', u'amod'],
 ['6', 'president', u'president', u'NN', u'', '3', u'attr'],
 ['7', 'of', u'of', u'IN', u'', '6', u'prep'],
 ['8', 'the', u'the', u'DT', u'GPE', '10', u'det'],
 ['9', 'United', u'united', u'NNP', u'GPE', '10', u'compound'],
 ['10', 'States', u'states', u'NNP', u'GPE', '7', u'pobj'],
 ['11', 'of', u'of', u'IN', u'GPE', '10', u'prep'],
 ['12', 'America', u'america', u'NNP', u'GPE', '11', u'pobj']]

我想在输入 token 列表时执行相同的操作...

最佳答案

您可以针对已标记化的文本运行 Spacy 的处理管道。不过,您需要了解,底层统计模型是在引用语料库上进行训练的,而引用语料库已使用某种策略进行了标记化,如果您的标记化策略显着不同,您可能会遇到一些性能下降。

以下是如何使用 Spacy 2.0.5 和 Python 3 进行操作。如果使用 Python 2,您可能需要使用 unicode 文字。

import spacy; nlp = spacy.load('en_core_web_sm')
# spaces is a list of boolean values indicating if subsequent tokens
# are followed by any whitespace
# so, create a Spacy document with your tokenisation
doc = spacy.tokens.doc.Doc(
    nlp.vocab, words=['nuts', 'itch'], spaces=[True, False])
# run the standard pipeline against it
for name, proc in nlp.pipeline:
    doc = proc(doc)

关于python-2.7 - spacy 是否将 token 列表作为输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48169545/

相关文章:

python - 统一码编码错误 : 'ascii' codec can't encode character u'\u201c' when converting series object to unicode in pandas with utf-16

python - “模块”对象没有属性 'feature_column'

java - 使用StreamTokenizer读取结构化文件

c - 在c中解析一个char数组

python - spaCy 2.0 : Loading Training Data from excel file Custom NER Model issues

python - 用户输入的数字为 "Counting digits"(Python 2.x)

python-2.7 - 如何使用带有 I2C 的 Raspberry Pi 从 Arduino 读取数据

regex - 如何从用户输入的句子中检测谁、什么、何时、何地

python - Spacy - 找不到模型

nltk - 在文本中添加标点符号