【问题标题】:Remove Stopwords in a RDD, Pyspark删除 RDD、Pyspark 中的停用词
【发布时间】:2021-09-08 08:53:38
【问题描述】:

我有一个 RDD,其中包含从文本文件中读取的文本。我想删除文本文件中的所有停用词。有一个 pyspark.ml.feature.StopWordsRemover 可以在 Dataframe 上执行相同的功能,但我想在 RDD 上执行此操作。有办法吗?

步骤:

txt = sc.textFile('/Path') 
txt.collect()  

哪个输出:

["23890098\tShlykov, a hard-working taxi driver and Lyosha"]

我想删除 txt RDD 中存在的所有停用词。 期望的输出:

["23890098\tShlykov, hard-working taxi driver Lyosha"]

【问题讨论】:

    标签: apache-spark pyspark apache-spark-sql rdd apache-spark-dataset


    【解决方案1】:

    您可以列出停用词,然后使用 lambda 函数来映射和过滤输出。

    stop_words = ['a','and','the','is']
    
    txt = sc.textFile('/Path')
    
    filtered_txt = txt.flatMap(lambda x: x.split()).filter(lambda x: x not in stop_words)
    
    filtered_txt.first()
    

    【讨论】:

      猜你喜欢
      • 2022-12-29
      • 2017-05-14
      • 2020-09-03
      • 2020-01-18
      • 2021-10-30
      • 1970-01-01
      • 2011-03-12
      • 2018-02-25
      • 2021-02-02
      相关资源
      最近更新 更多