导入 NLTK 后帮助程序命令不起作用

Helper-command not working after importing NLTK

我发现总是输入命令的现象

from nltk import *

帮助命令不再起作用。然后我收到以下错误消息:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable

有人对这种现象有解释吗?

我正在 Visual Studio 中编写代码 Python 3.

感谢并祝愿, 马库斯

没有直接关系,但如果您计划导入所有 nltk,只需使用 import nltk。不需要 *

查看 nltk.help 模块,您需要使用其中定义的函数之一。因为 nltk.help 本身不是函数而是库位置。

参见:https://www.nltk.org/api/nltk.html?highlight=help#module-nltk.help

因此,如果这是您要使用的模块,请尝试: import nltk nltk.help.upenn_tagset()

首先,从 nltk 导入 * 是个坏主意。你用许多对你来说 unknown/unclear 的变量污染了你的命名空间。

$ python

# Native Python variables.
>>> vars()
{'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', '__doc__': None, '__package__': None}
>>> len(vars())
4

# After importing from *
>>> from nltk import *
>>> len(vars())
510

接下来 Python,模块不可调用,但函数可以。

来自https://docs.python.org/3/tutorial/modules.html

If you quit from the Python interpreter and enter it again, the definitions you have made (functions and variables) are lost. Therefore, if you want to write a somewhat longer program, you are better off using a text editor to prepare the input for the interpreter and running it with that file as input instead. This is known as creating a script. As your program gets longer, you may want to split it into several files for easier maintenance. You may also want to use a handy function that you’ve written in several programs without copying its definition into each program.

To support this, Python has a way to put definitions in a file and use them in a script or in an interactive instance of the interpreter. Such a file is called a module; definitions from a module can be imported into other modules or into the main module (the collection of variables that you have access to in a script executed at the top level and in calculator mode).

A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. Within a module, the module’s name (as a string) is available as the value of the global variable name.

查看 nltk.help 模块:

>>> from nltk import help
>>> type(help) 
<type 'module'>

# A module is not callable. 
>>> help() 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable

# A module contains definitions and statements.
>>> dir(help) 
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '_format_tagset', '_print_entries', 'brown_tagset', 'claws5_tagset', 'load', 'print_function', 're', 'upenn_tagset', 'wrap']


# A function is callable. 
>>> type(help.brown_tagset)
<type 'function'>
>>> help.brown_tagset()
(: opening parenthesis
    (
): closing parenthesis
    )
*: negator
    not n't
,: comma
    ,
--: dash
yada yada