【问题标题】:Adding punctuations and other characters to stopwords?在停用词中添加标点符号和其他字符?
【发布时间】:2023-04-09 18:40:02
【问题描述】:

对于作业,我们手动使用停用词来返回没有它们的句子。但是,我们还必须删除句点、逗号、问号、标点符号,我不知道该怎么做,因为如果它附加到单词上,它就不会删除。这是我的代码。例如,如果我输入 prep_text('how was the game?') 它应该打印 'how was game'。没有问号或其他停用词。 (顺便说一句,停用词在代码中,我只是不知道如何将其放入此处的代码框中,哈哈:

my_stopwords =  ['is', 'it', 'the', 'if', '.', 'Is', 'It', 'The', 'If']

def prep_text(sentence):
    words = sentence.split(" ")
    words_filtered= [word for word in words if not word in my_stopwords]
    return (" ").join(words_filtered)

【问题讨论】:

  • 网上有很多 NLP 教程,如果你用 Google 搜索如何为 NLP 准备数据,你肯定会看到关于如何做到这一点的整篇文章。
  • 他想让我们用手动方法来做,但它仍然没有真正解释我如何删除问号之类的东西。
  • 这能回答你的问题吗? Best way to strip punctuation from a string
  • 我希望如此。但它并没有真正说明我需要如何修复我的代码。

标签: python list performance python-3.8 stop-words


【解决方案1】:

为了帮助您:只需将任务分开即可。在拆分之前从字符串中删除标点符号。

my_punctuation_marks = '''!"#$%&'()*+, -./:;<=>?@[\]^_`{|}~'''
my_stopwords =  ['is', 'it', 'the', 'if']

def prep_text(sentence):
    for ele in sentence:
        if ele in my_punctuation_marks:
            sentence = sentence.replace(ele, " ")
    words = sentence.split(" ")
    words_filtered= [word for word in words if not word.lower() in my_stopwords if word]
    return (" ").join(words_filtered)

【讨论】:

    猜你喜欢
    • 2011-07-29
    • 2019-10-29
    • 2019-09-16
    • 1970-01-01
    • 2018-01-14
    • 2020-10-04
    • 1970-01-01
    • 1970-01-01
    • 2010-09-26
    相关资源
    最近更新 更多