I have found some non-English words in my dictionary (from CountVectorizer) that I would like to remove:
verified={'日本': '19 日本',
'له': 'إستعداد له',
'لسنا': 'القادم لسنا',
'غيتس': 'بيل غيتس',
'على': 'على إستعداد',
'بيل': 'بيل غيتس',
'الوباء': 'الوباء القادم',
'إستعداد': 'إستعداد له',
'és': 'koronavírus és',
'állnak': 'kik állnak',
'zu': 'könig zu',
'zero': 'agenda zero'}
My attempt was to use nltk, specifically words
:
import nltk
words = set(nltk.corpus.words.words())
not_en_list = [x for x, v in verified.items() if v!='[]' if x not in words]
But when I ran it, no changes were applied. Still non-English words there. Please note that the example I provided is a sample of data: I have thousands of English words, but a few of non-English words that I would like to delete, without copying and pasting the list.
No changes are applied since you are not modifying any existing data structure. not_en_list
will be made but verified
will not be modified. Try this instead, and if not please post a minimum working example.
raw = {'日本': '19 日本',
'له': 'إستعداد له',
'لسنا': 'القادم لسنا',
'غيتس': 'بيل غيتس',
'على': 'على إستعداد',
'بيل': 'بيل غيتس',
'الوباء': 'الوباء القادم',
'إستعداد': 'إستعداد له',
'és': 'koronavírus és',
'állnak': 'kik állnak',
'zu': 'könig zu',
'zero': 'agenda zero'}
words = set(['zero'])
verified = {k: v for k, v in raw.items() if k in words}
assert verified == {'zero': 'agenda zero'}
Maybe this can help you:
import nltk
import ast
#nltk.download('words')
'''-> Remove HashTag if the word list has not been downloaded'''
dict_ = {'日本': '19 日本',
'له': 'إستعداد له',
'لسنا': 'القادم لسنا',
'غيتس': 'بيل غيتس',
'على': 'على إستعداد',
'بيل': 'بيل غيتس',
'الوباء': 'الوباء القادم',
'إستعداد': 'إستعداد له',
'és': 'koronavírus és',
'állnak': 'kik állnak',
'zu': 'könig zu',
'zero': 'agenda zero'}
words = set(nltk.corpus.words.words())
new_string = ''.join(w for w in nltk.wordpunct_tokenize(str(dict_)) \
if w.lower() in words or not w.isalpha())
new_dic = ast.literal_eval(new_string)
new_dic = {k: v for k, v in new_dic.items() if k and v is not None}
print(new_dic)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.