我是 python 的新手并使用 nltk,所以实际上我有一个非常基本的问题,但在任何地方都找不到答案。
我想知道什么时候在 nltk 模块的函数之前使用 nltk.
。我正在处理一些任务,在某些情况下它可以在不使用它的情况下工作,而在某些情况下则没有。我正在使用 Jupyter 笔记本。 (在示例中,我只写了导入的内容和最终命令)。
示例 1:
import nltk
print (FreqDist(words).most_common(10))
这工作得很好。
示例 2:
import nltk
import math
print (FreqDist(words).most_common(10))
给出错误信息
NameError Traceback (most recent call last)
<ipython-input-21-e68f77c40897> in <module>()
----> 1 print (FreqDist(file_words).most_common(10))
NameError: name 'FreqDist' is not defined
如果我在 FreqDist 之前插入 nltk 就可以了
print (nltk.FreqDist(words).most_common(10))
如果没有 nltk.
第二个例子是不是因为我导入了两个模块?
谢谢!
最佳答案
引用自 import this
:
Namespaces are one honking great idea -- let's do more of those!
来自Wikipedia :
In general when a module is imported then the names defined in the module are defined via that module's name space, and are accessed in from the calling modules by using the fully qualified name.
例如,在 Python 中,而不是:
import nltk
nltk.pos_tag(nltk.word_tokenize('This is a sentence.'))
你可以导入这样做:
from nltk import word_tokenize, pos_tag
pos_tag(word_tokenize('This is a sentence.'))
回到问题上来,上面的某个地方应该有 FreqDist
像这样导入:
from nltk import FreqDist
这样您就可以看到没有“完全限定的”nltk.FreqDist
的FreqDist
。
关于python - 使用 nltk 时。在 nltk 函数之前,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53197625/