PTB数据集是目前语言模型学习中使用最为广泛的文本数据集,下载地址如下:
在解压上述链接的压缩包,找到文件中的data文件,会发现有三个已经预处理国的三分数据文件
ptb.test.txt
ptb.train.txt
ptb.valid.txt
import codecs
import collections
from operator import itemgetter
#训练集的数据文件
RAW_DATA = "D:/chdownload/simple-examples/simple-examples/data/ptb.train.txt"
# 定义一个将为文本文件转化为词汇表文件名
VOCAB_OUTPUT = "ptb.vocab"
# 统计单词的出现频率
counter = collections.Counter() # statistic frequence of vocab
with codecs.open(RAW_DATA, "r", "utf-8") as f:
for line in f:
for word in line.strip().split():
counter[word] += 1
#按词频顺序对单词进行排序
sorted_word_to_cnt = sorted(counter.items(),
key=itemgetter(1),