[英]Removing non-English words from text using Python
我正在 python 上进行数据清理练习,我正在清理的文本包含我想删除的意大利语单词。 我一直在网上搜索是否可以使用像 nltk 这样的工具包在 Python 上执行此操作。
例如给定一些文本:
"Io andiamo to the beach with my amico."
我想留下:
"to the beach with my"
有谁知道如何做到这一点? 任何帮助将非常感激。
您可以使用words
,从NLTK语料库:
import nltk
words = set(nltk.corpus.words.words())
sent = "Io andiamo to the beach with my amico."
" ".join(w for w in nltk.wordpunct_tokenize(sent) \
if w.lower() in words or not w.isalpha())
# 'Io to the beach with my'
不幸的是, Io恰好是一个英文单词。 一般来说,可能很难确定一个词是否是英语。
在 MAC OSX 中,如果您尝试此代码,它仍然可以显示异常。 因此,请确保您手动下载单词语料库。 import
nltk
库后,您可能会像在 mac os 中一样,它不会自动下载单词语料库。 因此,您必须潜在地下载它,否则您将面临异常。
import nltk
nltk.download('words')
words = set(nltk.corpus.words.words())
现在,您可以按照前一个人的指示执行相同的执行。
sent = "Io andiamo to the beach with my amico."
sent = " ".join(w for w in nltk.wordpunct_tokenize(sent) if w.lower() in words or not w.isalpha())
根据NLTK文档,它没有这么说。 但是我在 github 上遇到了一个问题,并以这种方式解决了,它确实有效。 如果你不把word
参数放在那里,你的 OSX 可以注销并一次又一次地发生。
from nltk.stem.snowball import SnowballStemmer
snow_stemmer = SnowballStemmer(language='english')
#list of words
words = ['cared', 'caring', 'careful']
#stem of each word
stem_words = []
for w in words:
x = snow_stemmer.stem(w)
stem_words.append(x)
#stemming results
for w1,s1 in zip(words,stem_words):
print(w1+' ----> '+s1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.