【发布时间】:2017-11-26 10:09:43
【问题描述】:
我需要使用谷歌 ngram 语料库 (http://storage.googleapis.com/books/ngrams/books/datasetsv2.html),其中有 n-gram 的频率数据逐年出现在一本书中。
文件格式:以下每个文件都是压缩的制表符分隔数据,行格式如下:
ngram TAB year TAB match_count TAB volume_count NEWLINE.
我写了一个代码来检索我的输入 ngram 的频率
代码写成:
file = 'D:\Chrome Downloads\googlebooks-eng-all-4gram-20120701-aj\googlebooks-eng-all-4gram-20120701-aj'
z = []
counter = 0
freq = 0
with open(file, 'rt', encoding='UTF8') as input:
for line in input:
if(counter == 150):
break
if('Ajax and Achilles ?' == (line.strip().split('\t')[0])):
#else:
print(line.strip().split('\t'))
freq += int((line.strip().split('\t')[2]))
print('Frequency :', freq)
这很好用只是因为Ajax and Achilles 出现在语料库的顶部(计数器停止它)。当我尝试搜索稍后出现的 ngram 时,它需要很长时间。
使用这个语料库来获取 n-gram 的频率的问题是我必须查看整个语料库。
所以,我正在考虑合并行忽略年份并总结频率。
这是一个有效的想法吗?如果是这样,我该如何以编程方式执行此操作?
不是,有什么更好的方法?
【问题讨论】: