我使用这个空格代码稍后将其应用于我的文本,但我需要否定词留在文本中,如"Not".

nlp = spacy.load("en_core_web_sm") 

def my_tokenizer(sentence): 
    return [token.lemma_ for token in tqdm(nlp(sentence.lower()), leave = False) if token.is_stop == False and token.is_alpha == True and  token.lemma_ ] 

当我申请时,我得到的结果是:

[hello, earphone, work]

然而,最初的句子是

hello,my earphones are still not working.

所以,我想看到下面这句话:[earphone, still, not, work] 谢谢

推荐答案

"Not"实际上是一个停止词,在代码中,如果一个令牌被移除,那么它就是一个停止词.您也可以通过查看Spacy停用词列表
来查看这一点

"not" in spacy.lang.en.stop_words.STOP_WORDS

或通过循环您的文档对象的令牌

for tok in nlp(text.lower()):
  print(tok.text, tok.is_stop, tok.lemma_)

#hello False hello
#, False ,
#my True my
#earphones False earphone
#are True be
#still True still
#not True not
#working False work
#. False .

要解决这个问题,您应该从STOP_WORD列表中删除诸如"NOT"之类的目标单词.您可以这样做:

spacy.lang.en.stop_words.STOP_WORDS.remove("not")

然后,您可以重新运行代码,您将获得预期的结果:

import spacy
spacy.lang.en.stop_words.STOP_WORDS.remove("not")
nlp = spacy.load("en_core_web_sm") 
def my_tokenizer(sentence): 
    return [token.lemma_ for token in tqdm(nlp(sentence.lower()), leave = False) if token.is_stop == False and token.is_alpha == True and  token.lemma_ ] 

sentence = "hello,my earphones are still not working."
results = my_tokenizer(sentence)
print(results)

#['hello', 'earphone', 'not', 'work']

Python相关问答推荐

比较两个数据帧并并排附加结果(获取性能警告)

ModuleNotFound错误:没有名为flags.State的模块; flags不是包

未删除映射表的行

2D空间中的反旋算法

如果值不存在,列表理解返回列表

如何使用它?

如何获取numpy数组的特定索引值?

pyscript中的压痕问题

如何在UserSerializer中添加显式字段?

如何使用scipy的curve_fit与约束,其中拟合的曲线总是在观测值之下?

try 检索blob名称列表时出现错误填充错误""

* 动态地 * 修饰Python中的递归函数

解决Geopandas和Altair中的正图和投影问题

利用SCIPY沿第一轴对数组进行内插

将相应的值从第2列合并到第1列(Pandas )

使用pythonminidom过滤XML文件

将时间序列附加到数据帧

是否将列表分割为2?

在Pandas 中,有没有办法让元组作为索引运行得很好?

为什么任何一个HTML页面在保存到文件后都会变大6个字节?