【问题标题】:Remove Stopwords in French AND English删除法语和英语中的停用词
【发布时间】:2019-12-07 21:45:37
【问题描述】:

我正在尝试删除法语和英语中的停用词。到目前为止,我一次只能从一种语言中删除停用词。我有一个包含 700 行法文和英文混合文本的文本文档。

我正在使用 Python 对这 700 行进行聚类项目。但是,我的集群出现了问题。我得到了一个充满法语停用词的集群,这破坏了我的集群的效率。

这是我的停用词代码:

stopwords = nltk.corpus.stopwords.words('english')

如前所述,我也尝试在其中包含“法语”停用词,但无法在一行代码或同一变量中这样做。

这是包含我的文件的代码,其中包含我的 700 行混合的法语和英语描述:

Description2 = df['Description'].str.lower().apply(lambda x: ' 
'.join([word for word in str(x).split() if word not in (stopwords)]))

我尝试在上面的代码行中添加 2 个停用词变量,但它只删除了第一个变量的停用词。

以下是我因未删除法语停用词而得到的集群示例:

Cluster 5:
 la
 et
 dans
 les
 des
 est
 du
 le
 une
 en

如果我能够从我的文档中删除法语停用词,我将能够拥有代表在我的文档中重复出现的实际单词的集群。

任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: python nltk stop-words


    【解决方案1】:

    您是否尝试将法语停用词简单地添加到英语停用词中?例如这种方式(并使用set() 来提高效率,如nltk tutorial 中所述):

    stopwords = set(nltk.corpus.stopwords.words('english')) | set(nltk.corpus.stopwords.words('french'))
    # This way, you've got the english and french stop words in the stopwords variable
    
    Description2 = df['Description'].str.lower().apply(lambda x: ' '.join([word for word in str(x).split() if word not in stopwords]))
    

    【讨论】:

    • @OnThaRise 很高兴为您提供帮助:D。当你的问题得到解决时,不要犹豫接受答案,当其他人面临同样的问题时,它会帮助他们,这会标志着你的感激之情:)(一个好的评论对回答问题的人很好,接受的答案对网站的整体可用性更好)
    • 完美!我不知道!刚刚检查过,以后会这样做!
    • 我尝试将相同的停用词代码添加到我的 tf-idf 代码中的停用词中。但是,它不起作用。有没有办法在我的 tf-idf 代码中添加法语和英语停用词?我写了 stop_words = 'english' 但我也无法添加法语。它说 tf-idf 中没有内置法语停用词。
    • @OnThaRise 如果您还有其他问题,请提出一个新问题,cmets 不是为此设计的 :) (如果没有代码和/或更多上下文,将很难为您提供帮助 :))
    【解决方案2】:

    怎么样:

    import nltk
    import pandas as pd
    from functools import reduce
    
    
    df = pd.DataFrame(data={'Description': ['hello', 'dupa']})
    
    def apply_filtering(val, df):
        df['Description'] = df['Description'].str.lower()
        df['Description'] = df['Description'].apply(lambda x: str(x).split())
        df['Description'] = (df['Description']
                             .apply(lambda x: [word for word in x if word not in (nltk.corpus.stopwords.words(val))])
                             )
        df['Description'] = df['Description'].apply(lambda x: ''.join(x))
        return df
    
    
    elo = lambda l: reduce(lambda y,x: apply_filtering(x,y), l, df)
    elo(['english', 'french'])
    

    【讨论】:

      猜你喜欢
      • 2019-12-13
      • 2017-11-28
      • 1970-01-01
      • 2019-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-19
      • 1970-01-01
      相关资源
      最近更新 更多