问题描述
我正在尝试使用spacy获取文档中所有令牌的引号(即token.lemma _)。
代码:
sentence = 'I'm looking for all of the lemmas. Please help me find them!'
nlp = spacy.load('en',disable=['parser','NER])
doc = nlp(sentence)
tokens = [tokens.lemma_ for token in doc]
预期结果:
['look','lemma','help','find']
实际结果:
[-PRON-,'be','look','all','of','the','.','please','-PRON-','find','-PRON','!']
我是否在spacy中缺少某种预处理功能,还是必须单独进行预处理?我希望在词形还原之前删除所有标点和停用词。
解决方法
您可以使用
>>> [token.lemma_ for token in doc if not token.is_stop and not token.is_punct]
['look','lemma','help','find']
已添加以下部分:
-
if not token.is_stop
-如果令牌是停用词 -
and
-和 -
not token.is_punct
-如果令牌是标点符号,请忽略它们。