spacy是否将令牌列表作为输入?

dad*_*ada 7 tokenize python-2.7 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']]
Run Code Online (Sandbox Code Playgroud)

我想在输入令牌列表的同时做同样的事情...

ada*_*.ra 8

您可以针对已标记的文本运行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)
Run Code Online (Sandbox Code Playgroud)