【问题标题】:removing words from strings without affecting words using python spacy使用 python spacy 从字符串中删除单词而不影响单词
【发布时间】:2021-06-20 07:57:46
【问题描述】:

我正在使用 spacy,我有一个句子列表,我想从中删除停用词和标点符号。

for i in sentences_list: 
for token in docfile:
    if token.is_stop or token.is_punct and token.text in i[1]:
       i[1] = i[1].replace(token.text, '') 
print(sentences_list)

但它也会影响单词,例如单词 I 是停用词,因此单词 big 变为 bg

【问题讨论】:

  • 删除停用词和标点符号对现代 NLP 模型没有帮助,您不必在大多数时间都这样做。

标签: python string nlp spacy


【解决方案1】:

你可以使用:

" ".join([token.text for token in doc if not token.is_stop and not token.is_punct])

这是一个示例代码演示:

import spacy
nlp = spacy.load("en_core_web_sm")
sentences_list = ["I like big planes.", "No, I saw no big flames."]
new_sentence_list = []
for i in sentences_list:
    doc = nlp(i)
    new_sentence_list.append(" ".join([token.text for token in doc if not token.is_stop and not token.is_punct]))

new_sentence_list 现在是:

['like big planes', 'saw big flames']

【讨论】:

    猜你喜欢
    • 2020-01-25
    • 2017-03-27
    • 2014-11-21
    • 2022-01-26
    • 2013-08-11
    • 1970-01-01
    • 1970-01-01
    • 2021-08-26
    相关资源
    最近更新 更多