【问题标题】:Modify Stopword-Removal-Code to remove numbers as well修改 Stopword-Removal-Code 以删除数字
【发布时间】:2023-01-17 23:08:55
【问题描述】:

我在 df 列中有一个标记化文本。 从中删除停用词的代码有效,但我也喜欢删除标点符号、数字和特殊字符,而不是将它们拼写出来。 就像我想确保它也会删除更大/标记为一个标记的数字。

我当前的代码是:

eng_stopwords = stopwords.words('english')
punctuation = ['.', ',', ';', ':', '!' #and so on] 
complete_stopwords = punctuation + eng_stopwords
df['removed'] = df['tokenized_text'].apply(lambda words: [word for word in words if word not in complete_stopwords])

【问题讨论】:

    标签: python pandas stop-words


    【解决方案1】:

    您可以从字符串模块中获取标点符号:

    import string
    print(string.punctuation)
    
    '!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~'
    
    eng_stopwords = stopwords.words('english')
    
    punctuation = list(string.punctuation) 
    
    complete_stopwords = punctuation + eng_stopwords
    
    df['removed'] = df['tokenized_text'].apply(lambda words: [word for word in words if word not in complete_stopwords])
    

    【讨论】:

      猜你喜欢
      • 2021-06-11
      • 1970-01-01
      • 2018-12-25
      • 2022-01-14
      • 1970-01-01
      • 2023-01-19
      • 1970-01-01
      • 2021-08-23
      • 2017-02-12
      相关资源
      最近更新 更多