利用python进行自然语言处理学习笔记——chap2

第二章.获得文本语料与词汇资源

  1. 语料库和相关资源
  2. 条件概率分布

3. WordNet

import nltk
nltk.corpus.gutenberg
# including fileids/words/
# .raw() 返回所有文件内容包括空格
# sents()划分句子
  • 网络聊天文本
from nltk.corpus import webtext
from nltk.corpus import nps_chat
  • 布朗语料库
from nltk.corpus import brown

可以用来研究文体之间的系统性
比如对比不同类型文体之间相同意思单词的选择等

  • 路透社语料库
from nltk.corpus import reuters
  • 就职演说资料库
from nltk.corpus import inaugural
  • 标注文本语料库
    其他信息详见NLTK链接

  •  文本语料库的结构
    pic1

  • 基本语料库链接

  • 载入本地语料库

# .txt
from nltk.corpus import PlaintextCorpusReader
corpus_root = '/address/'
wordlists = PlaintextCorpusReader(corpus_root, '.*')
# 语料库
from nltk.corpus import BracketParseCorpusReader
corpus_root = '/address/'
file_pattern = '[正则表达式]'
ptb = BracketParseCorpusReader(corpus_root, file_pattern)

  • 条件和事件
    我们一般观察不同文本类别下的词汇频率分布nltk.ConditionalFreqDist
    e.g. pairs = [(‘news’: ‘word1’), (‘Romance’: ‘word2’), (‘news’: ‘word2’)…]
from nltk.corpus import brown
cfd = nltk.ConditionalFreqDist((genre, word)
                        for genre in brown.categories()
                        for word in brown.words(categories=genre))
  • 绘制分布图和分布表
  • 使用双连词生成随机样本
#此程序获得创世纪文本的所有双连词,并根据频率和种子词随机生成样本
text = nltk.corpus.genesis.words('english-kjv.txt')
bigrams = nltk.bigrams(text)
cfd = nltk.ConditionalFreqDist(bigrams)
def generate_word( cfd, word, num=15):
    for i in range(num):
        print word,
        word = cfd[word].max()
  • 词汇列表语料库
    词汇语料库(nltk.corpus.words)
    停用词语料库(nltk.corpus.stopwords)
    名字语料库(…….names)

使用Word Net
1.寻找同义词

from nltk.corpus import wordnet as wn
wn.synsets('motocar') # output [Synset('car.n.01')]
wn.synsets('car.n.01').lemma_names # output 'car','machine',tec.
#including wn.synsets('car.n.01').definition[examples]

WordNet 基于根同义词集
这里写图片描述
包括上位词/下位词,部分/整体,蕴涵,反义词等


练习题答案随后传上github

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值