简体   繁体   English

从语料库中删除非ASCII

[英]Removing non ASCII from corpus

I'm using NLTK for my project. 我正在为我的项目使用NLTK。 However, if a non-ascii word like '•' exist. 但是,如果存在像'•'这样的非ascii词。 NLTK cannot tokenize it. NLTK无法对其进行标记。 I'm using nltk.word_tokenize as the tokenizer. 我正在使用nltk.word_tokenize作为标记器。 How do I remove such words from entire corpus or make the tokenizer aware of such words? 如何从整个语料库中删除此类单词或使标记生成器识别出这些单词?

Use the below code to remove nonascii from your corpus: 使用以下代码从您的语料库中删除nonascii

ip=open(nonascii.txt,'r')
#Edit should be in w mode
op=open(ascii.txt,'w')
for line in ip:
        line=line.strip().decode("ascii","ignore").encode("ascii")
        if line=="":continue
        op.write(line)
ip.close()
op.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM