【发布时间】:2017-08-05 17:32:28
【问题描述】:
我正在研究 Sindhi,这是一种在 python 中使用 NLTK 工具进行的非英语语料库分析。我在 python 中导入了所有相关的库,并处理了加载压缩文件的代码。压缩文件的代码工作正常并加载数据。代码如下:
with zipfile.ZipFile('D:\Sindhicorpus.zip') as z:
print (len(z.namelist()))
for filename in z.namelist():
if not os.path.isdir(filename):
# read the file
with z.open(filename, 'rU') as rf:
line = rf.readline().decode('utf8')
# print(line)
在此之后,我处理用于生成 ngram 的代码,但该代码没有显示没有任何错误的结果。代码如下:
import nltk
from nltk.collocations import *
line = ""
for val in filename:
line += val
tokens = line.split()
bigram_measures = nltk.collocations.BigramAssocMeasures()
finder = BigramCollocationFinder.from_words(tokens)
finder.apply_freq_filter(3)
print(finder.nbest(bigram_measures.pmi, 100))
此代码仅显示 [ ] 之类的括号
在这段代码之后,我处理了另一个代码,但它也没有显示结果而不显示任何错误。代码如下:
from nltk import ngrams
n = 2
sixgrams = ngrams(filename.split(), n)
for grams in sixgrams:
print(grams)
请帮我解决我的问题
马扎尔
【问题讨论】:
-
第二个例子:如果
len(x) < n,nltk.ngrams(x, n)是一个空迭代器。由于您的文件名可能不包含空格,filename.split()会生成一个包含一个元素的列表,该元素小于 2(您分配给n)。
标签: python-3.x nltk cluster-analysis corpus