Advertisement

基于所给文档构建词汇表

阅读量:

在自然语言处理相关的各类任务中,文本预处理通常是一个不可或缺的步骤。其中,有一项操作具有特别重要的意义,那就是构建词典。接下来将提供一段用于讲解的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')]#这一步有问题,输出的不是汉字
           for word in tokens:
               if word in vocabulary:#已在词汇表中,则词频加1
                  vocabulary[word] += 1
               else:#不在则为1
                  vocabulary[word] = 1
        vocabulary_list = STA

全部评论 (0)

还没有任何评论哟~