在自然语言处理任务中,经常会对文本进行预处理。这种操作中
有一部分十分重要,即建立词典。下面将给出一段讲解的python代码。
# 生成词汇表文件
def gen_vocabulary_file(input_file, output_file):
vocabulary = {}
with open(input_file) as f:
counter = 0
for line in f:
counter += 1
#print line
tokens = [word for word in line.strip().decode('utf-8')]#这一步有问题,输出的不是汉字