100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 自然语言处理-nltk学习(一)

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

时间:2021-10-26 02:18:53

相关推荐

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

NLTK库安装

pip install nltk

执行python并下载书籍:

[root@centos #] pythonPython 2.7.11 (default, Jan 22 , 08:29:18)[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> import nltk>>> nltk.download()

选择book后点Download开始下载

下载完成以后再输入:

>>> from nltk.book import *

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

*** Introductory Examples for the NLTK Book ***Loading text1, ..., text9 and sent1, ..., sent9Type the name of the text or sentence to view it.Type: 'texts()' or 'sents()' to list the materials.text1: Moby Dick by Herman Melville 1851text2: Sense and Sensibility by Jane Austen 1811text3: The Book of Genesistext4: Inaugural Address Corpustext5: Chat Corpustext6: Monty Python and the Holy Grailtext7: Wall Street Journaltext8: Personals Corpustext9: 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 linedeck body fishery air boats side voyage

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

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

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

词统计

len(text1):返回总字数

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

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

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

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

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

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

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

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。