【问题标题】:Python NLTK ngram code does not give resultsPython NLTK ngram 代码不给出结果
【发布时间】: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) < nnltk.ngrams(x, n) 是一个空迭代器。由于您的文件名可能不包含空格,filename.split() 会生成一个包含一个元素的列表,该元素小于 2(您分配给 n)。

标签: python-3.x nltk cluster-analysis corpus


【解决方案1】:

您没有在阅读文件。

相反,您将文件名连接起来,并尝试对其进行解析。

for val in filename:
    line += val

你需要解决这个问题。

【讨论】:

  • 我需要解决方案
  • 您拥有所需的一切,您只需要正确设置变量即可。
猜你喜欢
  • 2021-08-23
  • 2014-10-25
  • 1970-01-01
  • 2013-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-24
  • 2013-07-19
相关资源
最近更新 更多