You might like to try more words (e.g., liberty, constitution) and different texts. Can you predict the dispersion of a word before you view it? As before, take care to get the quotes, commas, brackets, and parentheses exactly right.
>>> text4.dispersion_plot(["citizens", "democracy", "freedom", "duties", "America"])
这里书中是这样提示的:
Important: You need to have Python’s NumPy and Matplotlib packages installed in order to produce the graphical plots used in this book. Please see http://www.nltk.org/ for installation instructions.
但是呢,这本书其实是使用的Python2.x,所以按照书上提示,你如果直接使用cmd进入Scripts,然后输入 pip install numpy matplotlib,这样是不行的,至少numpy不行!
会报错:
>>>import matplotlib
Traceback (most recent call last):
File "#5>", line 1, in
import matplotlib
File "C:\Python34\lib\site-packages\matplotlib\__init__.py", line 180, in
from matplotlib.cbook import is_string_like
File "C:\Python34\lib\site-packages\matplotlib\cbook.py", line 33, in
import numpy as np
File "C:\Python34\lib\site-packages\numpy\__init__.py", line 170, in
from . import add_newdocs
File "C:\Python34\lib\site-packages\numpy\add_newdocs.py", line 13, in
from numpy.lib import add_newdoc
File "C:\Python34\lib\site-packages\numpy\lib\__init__.py", line 8, in
from .type_check import *
File "C:\Python34\lib\site-packages\numpy\lib\type_check.py", line 11, in
import numpy.core.numeric as _nx
File "C:\Python34\lib\site-packages\numpy\core\__init__.py", line 6, in
from . import multiarray
ImportError: cannot import name 'multiarray'
>>> import numpy
Traceback (most recent call last):
File "#6>", line 1, in
import numpy
File "C:\Python34\lib\site-packages\numpy\__init__.py", line 170, in
from . import add_newdocs
File "C:\Python34\lib\site-packages\numpy\add_newdocs.py", line 13, in
from numpy.lib import add_newdoc
File "C:\Python34\lib\site-packages\numpy\lib\__init__.py", line 8, in
from .type_check import *
File "C:\Python34\lib\site-packages\numpy\lib\type_check.py", line 11, in
import numpy.core.numeric as _nx
File "C:\Python34\lib\site-packages\numpy\core\__init__.py", line 6, in
from . import multiarray
ImportError: cannot import name 'multiarray'
matplotlib直接用pip install 就行了
这个numpy需要到http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy 中去下载合适版本的numpy,我下载的是numpy-1.13.1+mkl-cp36-cp36m-win32.whl
其中cp36表示 Python3.6 win32表示32位
(这个网址可以下载很多东西,只需要改变地址后面#的值就行了,只不过非常慢,这是国外的教育网)
下载好之后cmd到这个下载文件的位置,执行
pip install numpy-1.13.1+mkl-cp36-cp36m-win32.whl
前提是你的系统环境变量有 $Python_Home\Scripts
都安装好之后,再执行
>>> from nltk.book import *
>>> text4.dispersion_plot(["citizens", "democracy", "freedom", "duties", "America"])
注:遇到此问题的人一定都是在学习《Python自然语言处理》,这本书中文翻译有许多地方不太严谨(个人观点),希望有能力的人阅读原版!