欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

自然语言处理-nltk学习(一)

程序员文章站 2022-07-13 10:09:38
...

NLTK库安装

pip install nltk

执行python并下载书籍:

[[email protected] #] python
Python 2.7.11 (default, Jan 22 2016, 08:29:18)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import nltk
>>> nltk.download()

自然语言处理-nltk学习(一)

选择book后点Download开始下载

自然语言处理-nltk学习(一)

下载完成以后再输入:

>>> from nltk.book import *

你会看到可以正常加载书籍如下:

*** Introductory Examples for the NLTK Book ***
Loading text1, ..., text9 and sent1, ..., sent9
Type the name of the text or sentence to view it.
Type: 'texts()' or 'sents()' to list the materials.
text1: Moby Dick by Herman Melville 1851
text2: Sense and Sensibility by Jane Austen 1811
text3: The Book of Genesis
text4: Inaugural Address Corpus
text5: Chat Corpus
text6: Monty Python and the Holy Grail
text7: Wall Street Journal
text8: Personals Corpus
text9: The Man Who Was Thursday by G . K . Chesterton 1908

这里面的text*都是一个一个的书籍节点,直接输入text1会输出书籍标题:

>>> text1
<Text: Moby Dick by Herman Melville 1851>

 

搜索文本

执行

>>> text1.concordance("former")

会显示20个包含former的语句上下文

我们还可以搜索相关词,比如:

>>> text1.similar("ship")
whale boat sea captain world way head time crew man other pequod line
deck body fishery air boats side voyage

输入了ship,查找了boat,都是近义词

 

我们还可以查看某个词在文章里出现的位置:

>>> text4.dispersion_plot(["citizens", "democracy", "freedom", "duties", "America"])

自然语言处理-nltk学习(一)

 

词统计

len(text1):返回总字数

set(text1):返回文本的所有词集合

len(set(text4)):返回文本总词数

text4.count("is"):返回“is”这个词出现的总次数

FreqDist(text1):统计文章的词频并按从大到小排序存到一个列表里

fdist1 = FreqDist(text1);fdist1.plot(50, cumulative=True):统计词频,并输出累计图像

自然语言处理-nltk学习(一)

纵轴表示累加了横轴里的词之后总词数是多少,这样看来,这些词加起来几乎达到了文章的总词数

fdist1.hapaxes():返回只出现一次的词

text4.collocations():频繁的双联词