【发布时间】: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() 以避免从句子中删除像 equipped、climatisation 这样的词。
【问题讨论】:
-
climatisation不在英语词典中,据我所知,它看起来像法语单词。您可能需要在此处提供您自己的字典。 -
您好 Wiktor,非常感谢您的回答。自己的字典添加代码的任何代码示例?我尝试过但我失败了。
-
words.extend(['climatisation', 'equipped']) -
感谢您的回答。这是我尝试过的但给了我错误:“AttributeError:'set'对象没有属性'extend'”不幸的是......
-
然后使用
update。words.update(['climatisation', 'equipped'])