我正在执行一个数据提取用例。为了对我的数据进行预处理和标记,我同时使用了spacy英语和德语标记器,因为句子都是用这两种语言的。这是我的密码:
import spacy
from spacy.lang.de import German
from spacy.lang.en import English
from spacy.lang.de import STOP_WORDS as stp_wrds_de
from spacy.lang.en.stop_words import STOP_WORDS as stp_wrds_en
import string
punctuations = string.punctuation
# German Parser
parser_de = German()
# English Parser
parser_en = English()
def spacy_tokenizer_de(document):
# Token object for splitting text into 'units'
tokens = parser_de(document)
# Lemmatization: Grammatical conversion of words
tokens = [word.lemma_.strip() if word.lemma_ != '-PRON-' else word for word in tokens]
# Remove punctuations
tokens = [word for word in tokens if word not in punctuations]
tokens_de_str = converttostr(tokens,' ')
tokens_en = spacy_tokenizer_en(tokens_de_str)
print("Tokens EN: {}".format(tokens_en))
tokens_en = converttostr(tokens_en,' ')
return tokens_en
def converttostr(input_seq, separator):
# Join all the strings in list
final_str = separator.join(input_seq)
return final_str
def spacy_tokenizer_en(document):
tokens = parser_en(document)
tokens = [word.lemma_.strip() if word.lemma_ != '-PRON-' else word for word in tokens]
return tokens以下是对上述代码的进一步说明:
1. spacy_tokenizer_de():用德语解析和标记文档的方法
2. spacy_tokenizer_en():用英文解析和标记文档的方法
3. converttostr():将令牌列表转换为字符串,以便英语标记器可以读取输入(只接受文档/字符串格式)并对数据进行标记化。
但是,在分析某些句子时,会导致以下错误:

为什么在这样的场景中会出现一个spacy令牌对象,而有些句子正在被成功地处理呢?有人能帮忙吗?
发布于 2020-04-17 13:57:24
token.lemma_.strip() if token.lemma_ != '-PRON-' else token.text for token in tokens
你应该在这里找到一张单词列表,对吧?相反,有时返回一个字符串(当引理不等于'-PRON-'),但其他时候只返回令牌,而不返回字符串。
您可以从token.text获得一个字符串。
https://stackoverflow.com/questions/61273071
复制相似问题