一 过滤文本
去除停用词典和错词检错都可以用词典的形式完成,以停用词为例,我使用的应该是知网提供的中文停用词典。测试的数据集是小学生数学题。
print(text) # 打印未去除停用词前版本
with open(r"C:\Users\BF\Desktop\NLTK\stopwords.txt","r",encoding='utf-8') as stopfile: # 读取停用词,用utf-8的编码格式
txt = stopfile.readlines() # 一次性将所有的词按行读进来
stopword = set(word.strip('\n') for word in txt) # 去除每个词后的换行符放入tuple中
removetext = [word for word in text if word not in stopword] # 去除停用词
print(removetext)