删除 nlp 任务的自定义停用词列表

扎眼的阳光 nlp 377

原文标题Removing a custom list of stopwords for an nlp task

我写了一个函数来清理我的文本语料库,它的形式如下:

["wild things is a suspenseful .. twists .  ",
 "i know it already.. film goers .  ",
.....,
"touchstone pictures..about it .  okay ?  "]

这是一个用逗号分隔的句子列表。

我的功能是:

def clean_sentences(sentences):  
   
    sentences = (re.sub(r'\d+','£', s) for s in sentences
 
    stopwords = ['a', 'and', 'any', 'he', 'her', 'here', 'hers', 'herself', 'him', 'himself', 'is' , 'it']
       
    sentences = ' '.join(w for w in sentences if w not in stopwords)

    return sentences 

它将数字替换为“£”,但不会删除停用词。

输出:

'wild things is a suspenseful thriller...

and a £ . £ rating , it\'s still watchable , just don\'t think about it .  okay ?  '

我不明白为什么。谢谢。

原文链接:https://stackoverflow.com//questions/71678789/removing-a-custom-list-of-stopwords-for-an-nlp-task

回复

我来回复
  • ewz93的头像
    ewz93 评论

    当您实际上想要将句子中的单词与停用词进行比较时,您会将整个句子与停用词进行比较。

    import re
    
    sentences = ["wild things is a suspenseful .. twists .  ",
                 "i know it already.. film goers .  ",
                 "touchstone pictures..about it .  okay ?  "]
    
    stopwords = ['a', 'and', 'any', 'he', 'her', 'here', 'hers', 'herself', 'him', 'himself', 'is', 'it']
    

    作为一个循环:

    def clean_sentences(sentences):
        new_sentences = []
        for sentence in sentences:
            new_sentence = sentence.split()
            new_sentence = [re.sub(r'\d+', '£', word) for word in new_sentence]
            new_sentence = [word for word in new_sentence if word not in stopwords]
            new_sentence = " ".join(new_sentence)
            new_sentences.append(new_sentence)
        return new_sentences
    

    或者,更紧凑,作为列表理解:

    def clean_sentences(sentences):
        return [" ".join([re.sub(r'\d+', '£', word) for word in sentence.split() if word not in stopwords]) for sentence in sentences]
    

    两者都返回:

    print(clean_sentences(sentences))
    > ['wild things suspenseful .. twists .', 'i know already.. film goers .', 'touchstone pictures..about . okay ?']
    
    2年前 0条评论
  • Alan Shiah的头像
    Alan Shiah 评论

    我相信这是因为您使用正则表达式替换代码中的符号 £。澄清:sentences = (re.sub(r’\d+’,’£’, s) for s in sentence

    这是一段用该符号替换任何数字的代码。我看到您定义了停用词列表,然后制作了一个没有这些停用词的新列表。但是,您替换数字的符号£不在停用词列表中,因此不会被排除在新列表中。您可以尝试将其添加到您的停用词列表中,如下所示:

    def clean_sentences(sentences):  
       
        sentences = (re.sub(r'\d+','£', s) for s in sentences)
     
        stopwords = ['a', 'and', 'any', 'he', 'her', 'here', 'hers', 'herself', 'him', 'himself', 'is' , 'it', '£']
           
        sentences = ' '.join(w for w in sentences if w not in stopwords)
    
        return sentences 
    

    希望这可以帮助!

    编辑:我也相信您的原始代码可能有问题。看来您正在尝试使用sentences = ' '.join(w for w in sentences if w not in stopwords)将您的句子连接在一起并取出任何停用词。但是,这是对not in运算符如何工作的无效使用。 not in运算符仅检查列表中的特定单词,而不是整个句子。基本上,它不会使用您的停用词删除任何内容,因为它无法检测整个句子中是否有停用词。你想要做的是先把每个句子分成一堆单词,然后用你已经做的相同.join方法制作一个新列表。这将使not in操作员可以检查每个单词并在它是停用词时将其删除。

    2年前 0条评论