未能在python3.6中同时实现NLTK和matplotlib

2024-04-25 16:47:08 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我在写语料库语言学的本科论文,需要从我收集的文本中创建一个语料库。我设法使语料库与Python3.6中的NLTK配合得很好,为了使用它,我尝试实现NLTK手册中的一个示例代码:

>>> cfd = nltk.ConditionalFreqDist(
...           (target, fileid[:4])
...           for fileid in inaugural.fileids()
...           for w in inaugural.words(fileid)
...           for target in ['america', 'citizen']
...           if w.lower().startswith(target))
>>> cfd.plot()

我用pip安装了matplotlib,但它给了我一个错误消息,我不明白:

 Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\marcell\AppData\Local\Programs\Python\Python36-32\lib\site-pack
ages\nltk\probability.py", line 1862, in plot
    pylab.legend(loc=legend_loc)
UnboundLocalError: local variable 'legend_loc' referenced before assignment

我不知道我做错了什么,我几乎没有任何python或编程方面的经验。有人能帮我吗?你知道吗

概率.py根据Terry Jan Reedy从1849线到1869线的要求:

for condition in conditions:
        if cumulative:
            freqs = list(self[condition]._cumulative_frequencies(samples))
            ylabel = "Cumulative Counts"
            legend_loc = 'lower right'
        else:
            freqs = [self[condition][sample] for sample in samples]
            ylabel = "Counts"
            legend_loc = 'upper right'
        # percents = [f * 100 for f in freqs] only in ConditionalProbDist?
        kwargs['label'] = "%s" % condition
        pylab.plot(freqs, *args, **kwargs)

    pylab.legend(loc=legend_loc)
    pylab.grid(True, color="silver")
    pylab.xticks(range(len(samples)), [text_type(s) for s in samples], rotation=90)
    if title:
        pylab.title(title)
    pylab.xlabel("Samples")
    pylab.ylabel(ylabel)
    pylab.show()

Tags: intargetforifplottitleconditionloc