在预先标记的文本上使用空格

问题描述 投票:0回答:1

我想使用spacy处理已经预先加标记的文本。将令牌列表解析为spacy无效。

import spacy
nlp = spacy.load("en_core_web_sm")
nlp(["This", "is", "a", "sentence"])

这给出TypeError(这很有意义):TypeError: Argument 'string' has incorrect type (expected str, got list)

我可以用自定义标记替换令牌生成器,但是我觉得那样会使事情复杂化,不是首选方法。

谢谢您的帮助:D

python tokenize spacy
1个回答
0
投票

您可以使用此方法:

tokens = ["This", "is", "a", "sentence"]
sentence = nlp.tokenizer.tokens_from_list(tokens)
print(sentence)
This is a sentence 
© www.soinside.com 2019 - 2024. All rights reserved.