【问题标题】:How to solve missing words in nltk.corpus.words.words()?如何解决 nltk.corpus.words.words() 中缺失的单词?
【发布时间】:2022-12-17 08:18:40
【问题描述】:

我试图从文本中删除非英语单词。 NLTK 语料库中缺少许多其他词的问题。

我的代码:

import pandas as pd
    
lst = ['I have equipped my house with a new [xxx] HP203X climatisation unit']
df = pd.DataFrame(lst, columns=['Sentences'])
    
import nltk 
nltk.download('words')
words = set(nltk.corpus.words.words())
    
df['Sentences'] = df['Sentences'].apply(lambda x: " ".join(w for w in nltk.wordpunct_tokenize(x) if w.lower() in (words)))
df

输入:I have equipped my house with a new [xxx] HP203X climatisation unit
结果:I have my house with a new unit

应该是:I have equipped my house with a new climatisation unit

我不知道如何完成 nltk.corpus.words.words() 以避免从句子中删除像 equippedclimatisation 这样的词。

【问题讨论】:

  • climatisation 不在英语词典中,据我所知,它看起来像法语单词。您可能需要在此处提供您自己的字典。
  • 您好 Wiktor,非常感谢您的回答。自己的字典添加代码的任何代码示例?我尝试过但我失败了。
  • words.extend(['climatisation', 'equipped'])
  • 感谢您的回答。这是我尝试过的但给了我错误:“AttributeError:'set'对象没有属性'extend'”不幸的是......
  • 然后使用updatewords.update(['climatisation', 'equipped'])

标签: nlp nltk tokenize corpus


【解决方案1】:

您可以使用

words.update(['climatisation', 'equipped'])

在这里,words 是一个集合,这就是为什么 .extend(word_list) 不起作用的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多