文档生成字典

自然语言处理任务中,经常会对文本进行预处理。这种操作中 
有一部分十分重要,即建立词典。下面将给出一段讲解的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 = START_VOCABULART + sorted(vocabulary, key=vocabulary.get, reverse=True)
        #print vocabulary
        # 取前5000个常用汉字, 应该差不多够用了
        if len(vocabulary_list) > 5000:

           vocabulary_list = vocabulary_list[:5000]#5000大小的词汇表

        print(input_file , " 词汇表大小:", len(vocabulary_list))
        with open(output_file, "w") as ff:
            for word in vocabulary_list:
                ff.write(word+'\n')


 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

在这段代码中,函数有两个参数,一个为输入文件,一个是输出文件(词汇表)。 
(1)打开文档,并统计汉字词频;

    with open(input_file) as f:
        counter = 0
        for line in f:
           counter += 1
           tokens = [word for word in line.strip().decode('utf-8')]#必须加上decode(),否则建立的词汇表会出现乱码,tokens为列表。
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

统计词频字典:

           for word in tokens:
               if word in vocabulary:#已在词汇表中,则词频加1
                  vocabulary[word] += 1
               else:#不在则为1
                  vocabulary[word] = 1
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

统计新的词频字典,以词频逆排

vocabulary_list = START_VOCABULART + sorted(vocabulary, key=vocabulary.get, reverse=True)
 
 
  • 1
  • 1

取前5000个汉字:

        if len(vocabulary_list) > 5000:
           vocabulary_list = vocabulary_list[:5000]#5000大小的词汇表
 
 
  • 1
  • 2
  • 1
  • 2

词汇表大小,并写入文件

print(input_file , " 词汇表大小:", len(vocabulary_list))
with open(output_file, "w") as ff:
    for word in vocabulary_list:
        ff.write(word+'\n')
 
 
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

如果出现编码错误,请在python文件头部加上:

import sys
reload(sys)
sys.setdefaultencoding('utf-8')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值