NLTK库

作者: 一半浮沉 | 来源:发表于2019-09-27 11:15 被阅读0次

1,安装

pip install nltk

2, 下载书籍

$ python
>>> import nltk
>>> nltk.download()

3,选择book后点Download开始下载,下载完成以后再输入:

>>> 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

4,基本操作

>>> text1  //这里面的text*都是一个一个的书籍节点,直接输入text1会输出书籍标题
>>> text1.concordance("former")  //会显示20个包含former的语句上下文
>>> text1.similar("ship") //搜索相关词
>>> text4.dispersion_plot(["citizens", "democracy", "freedom", "duties", "America"])   //查看某个词在文章里出现的位置

5,基本统计

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():频繁的双联词

6,语料库,以下各种语料库都是分别建立的,因此会稍有一些区别,但是不外乎以下几种组织结构:散养式(孤立的多篇文章)、分类式(按照类别组织,相互之间没有交集)、交叉式(一篇文章可能属于多个类)、渐变式(语法随着时间发生变化)

nltk.corpus.gutenberg.fileids() //Gutenberg语料库
nltk.corpus.gutenberg就是gutenberg语料库的阅读器,它有很多实用的方法,比如:

nltk.corpus.gutenberg.raw('chesterton-brown.txt'):输出chesterton-brown.txt文章的原始内容

nltk.corpus.gutenberg.words('chesterton-brown.txt'):输出chesterton-brown.txt文章的单词列表

nltk.corpus.gutenberg.sents('chesterton-brown.txt'):输出chesterton-brown.txt文章的句子列表

类似的语料库还有:

from nltk.corpus import webtext:网络文本语料库,网络和聊天文本

from nltk.corpus import brown:布朗语料库,按照文本分类好的500个不同来源的文本

from nltk.corpus import reuters:路透社语料库,1万多个新闻文档

from nltk.corpus import inaugural:就职演说语料库,55个总统的演说

7,语料库的通用接口

fileids():返回语料库中的文件

categories():返回语料库中的分类

raw():返回语料库的原始内容

words():返回语料库中的词汇

sents():返回语料库句子

abspath():指定文件在磁盘上的位置

open():打开语料库的文件流

8,加载自己的语料库,收集自己的语料文件(文本文件)到某路径下(比如/tmp),然后执行:

>>> from nltk.corpus import PlaintextCorpusReader
>>> corpus_root = '/tmp'
>>> wordlists = PlaintextCorpusReader(corpus_root, '.*')
>>> wordlists.fileids()

就可以列出自己语料库的各个文件了,也可以使用如wordlists.sents('a.txt')和wordlists.words('a.txt')等方法来获取句子和词信息

9,

相关文章

  • NLP基础

    NLP基础 NLP涉及知识 NLTK库 分词 TF-IDF 手动操作安装NLTK库 代码小练 什么是NLP 词处理...

  • NLTK之相关库

    NLTK之 NUmpy库 NLTK之SciPy库 线性代数特征值与特征向量稀疏矩阵(DOK,LOL,COL,CRS...

  • NLTK库

  • NLTK库

    1,安装 2, 下载书籍 3,选择book后点Download开始下载,下载完成以后再输入: 4,基本操作 5,基...

  • NLP基本步骤及原理

    本文目录第一章:文本预处理(Preprocess)1.1NLTK自然语言处理库1.1.1 NLTK自带语料库第二章...

  • 初识NLTK库

    NLTK是一个比较优秀的自然语言处理工具包,是我们聊天机器人需要的比较重要的一个工具 NLTK库安装 执行pyth...

  • NLTK | 学习笔记1

    原文链接:在这里 最近在对照着nltk.book学习NLTK库,虽然网络上有中文翻译版,但是似乎并没有搭配Pyth...

  • Python自然语言处理学习笔记(二)

    1.文本语料库 1)内容 导入corpus包得到各个文本语料库:from nltk.corpus import *...

  • mac 安装nltk, 并解决nltk.download()出错

    1. mac 安装nltk pip install nltk 卸载也很容易: pip uninstall nltk...

  • python自然语言处理学习笔记(二)—— 语料库

    一、获取文本语料库 1. 古腾堡语料库 NLTK包含古腾堡项目(Project Gutenberg)电子文本档案的...

网友评论

      本文标题:NLTK库

      本文链接:https://www.haomeiwen.com/subject/mhwouctx.html