《PYTHON自然语言处理》第2章

内容简介:各种文本语料库。

古腾堡项目 www.gutenberg.org

OLAC元数据格式档 http://www.language-archives.org/

语料库邮件列表 http://clu.uni.no/corpora/sub.html

ETHNOLOGUE 世界完整语言清单 http://www.ethnologue.com/

SIL 语言项目http://www.sil.org/

2.8 练习

1.

 >>> phrase = ['abstruse']
>>> type(phrase)
<type 'list'>
>>> phrase + ['bb']
['abstruse', 'bb']
>>> phrase + ['bb'] * 3
['abstruse', 'bb', 'bb', 'bb']
>>> (phrase + ['bb']) * 3
['abstruse', 'bb', 'abstruse', 'bb', 'abstruse', 'bb']
>>> tmp = (phrase + ['bb']) * 3
>>> tmp[-3:]
['bb', 'abstruse', 'bb']
>>> sorted(tmp)
['abstruse', 'abstruse', 'abstruse', 'bb', 'bb', 'bb']
>>> tmp[-1]
'bb'

Ex1 Code

2.

 >>> import nltk
>>> nltk.corpus.gutenberg.fileids()
['austen-emma.txt', 'austen-persuasion.txt', 'austen-sense.txt', 'bible-kjv.txt', 'blake-poems.txt', 'bryant-stories.txt', 'burgess-busterbrown.txt', 'carroll-alice.txt', 'chesterton-ball.txt', 'chesterton-brown.txt', 'chesterton-thursday.txt', 'edgeworth-parents.txt', 'melville-moby_dick.txt', 'milton-paradise.txt', 'shakespeare-caesar.txt', 'shakespeare-hamlet.txt', 'shakespeare-macbeth.txt', 'whitman-leaves.txt']
>>> per = nltk.corpus.gutenberg.words('austen-persuasion.txt')
>>> len(per)
98171
>>> len(set(per))
6132
## 区别
>>> len(set((s.lower() for s in per if s.isalpha())))
5739
##
>>> from __future__ import division
>>> len(per) / len(set(per))
16.00962165688193
>>> type(per)
<class 'nltk.corpus.reader.util.StreamBackedCorpusView'>
>>> per[20:30]
['was', 'a', 'man', 'who', ',', 'for', 'his', 'own', 'amusement', ',']

3

from nltk.corpus import brown
brown.categories()
['adventure', 'belles_lettres', 'editorial', 'fiction', 'government', 'hobbies', 'humor', 'learned', 'lore', 'mystery', 'news', 'religion', 'reviews', 'romance', 'science_fiction']
b = brown.words(categories='humor')
print ' '.join(b[:50]) w = nltk.corpus.webtext.words('pirates.txt')
print ' '.join(w[:50])

4

  modals = ['men', 'women', 'people']

  cfd = nltk.ConditionalFreqDist(
(target,fileid[:4])
for fileid in state_union.fileids()
for w in state_union.words(fileid)
for target in modals
if w.lower().startswith(target))
cfd.plot()

8

 names = nltk.corpus.names

  cfd = nltk.ConditionalFreqDist(
(fileid, name[0])
for fileid in names.fileids()
for name in names.words(fileid)) cfd.plot

图显示,首字母H和W在男名中比女名中更常用。

9.

用词典过滤文本语料的内容。

# 函数定义
# myproc.py
from __future__ import division
import nltk
from nltk.corpus import stopwords def content_fraction(text):
"""calculate words fractioin without stop words"""
stopwords = nltk.corpus.stopwords.words('english')
content = [w for w in text if w.lower() not in stopwords]
return len(content) / len(set(content)) # 命令行
from nltk.book import *
from myproc iimport *
content_fraction(text1)
8.06865891513653
content_fraction(text2)
11.399421789409617

可以看出,去掉停用词后,<<Moby Dick>>词汇比<<Sense and Sensibility>>更丰富。(注,不去停用词,统计结果同样支持这个结论。)

疑问:如何考察文体(genre)差异? 文体上分,二者都是小说,再具体点,一个是恐怖(惊悚)小说,一个是言情(浪漫)小说。

如果有分类语料库,如BROWN,直接提供了类型。

若如TEXT1,TEXT2等TEXT词链表,如何能根据词汇统计得出“文体”的结论呢?分类语料库是如何进行文本分类的,依据是什么?搞清楚这两点,大约可以仿BROWN分类方法,将这道题目做出来了。

尝试,发现Moby dick应是写whale的, S&S应是写人的。

探查一个词在两个文本中的词义,如下。

 text1.concordance('monstrous')
text2.concordance('monstrous') text1.similar('monstrous')
text2.similar('monstrous') #还可以
text1.common_contexts("'monstrous'", "abundant")
text2.common_contexts("'monstrous'", "very")

用WordNet考察monstrous词义,会发现该词意义很单一,没有小说中那么丰富的变化。定义就是"abnormally large"。

这个意义更接近现在的词义。是否因为写于1811的S&S比写于1851年的MB在语言用法方面与现代距离更远呢?

 >>> from nltk.corpus import wordnet as wn
>>> wn.synsets('monstrous')
[Synset('monstrous.s.01'), Synset('atrocious.s.01'), Synset('grotesque.s.01')]
>>> wn.synset('monstrous.s.01').lemma_names
['monstrous']
>>> wn.synset('atrocious.s.01').lemma_names
['atrocious', 'flagitious', 'grievous', 'monstrous']
>>> wn.synset('grotesque.s.01').lemma_names
['grotesque', 'monstrous']
>>> wn.synset('monstrous.s.01').definition
'abnormally large'
>>> wn.synset('monstrous.s.01').examples
[]
>>> wn.synset('monstrous.s.01').lemmas
[Lemma('monstrous.s.01.monstrous')]

=====结束分割线=====

(待续……)

NLPP-02-Exercises的更多相关文章

  1. Kotlin中变量不同于Java: var 对val(KAD 02)

    原文标题:Variables in Kotlin, differences with Java. var vs val (KAD 02) 作者:Antonio Leiva 时间:Nov 28, 201 ...

  2. Android游戏开发实践(1)之NDK与JNI开发02

    Android游戏开发实践(1)之NDK与JNI开发02 承接上篇Android游戏开发实践(1)之NDK与JNI开发01分享完JNI的基础和简要开发流程之后,再来分享下在Android环境下的JNI ...

  3. iOS系列 基础篇 02 StoryBoard 故事板文件

    iOS基础 02 StoryBoard 故事板文件 目录: 1. 故事板的导航特点 2. 故事板中的Scene和Segue 3. 本文最后 在上篇HelloWorld工程中有一个Main.storyb ...

  4. [转]Tesseract 3.02中文字库训练

    下载chi_sim.traindata字库下载tesseract-ocr-setup-3.02.02.exe 下载地址:http://code.google.com/p/tesseract-ocr/d ...

  5. Java多线程系列--“JUC锁”02之 互斥锁ReentrantLock

    本章对ReentrantLock包进行基本介绍,这一章主要对ReentrantLock进行概括性的介绍,内容包括:ReentrantLock介绍ReentrantLock函数列表ReentrantLo ...

  6. Oracle Recovery 02 - 常规恢复之不完全恢复

    背景:这里提到的常规恢复指的是数据库有完备可用的RMAN物理备份. 实验环境:RHEL6.4 + Oracle 11.2.0.4 单实例. 二.常规恢复之不完全恢复:部分数据丢失 2.1 重做日志文件 ...

  7. 异步编程系列第02章 你有什么理由使用Async异步编程

    p { display: block; margin: 3px 0 0 0; } --> 写在前面 在学异步,有位园友推荐了<async in C#5.0>,没找到中文版,恰巧也想提 ...

  8. star ccm+ 11.02安装

    STAR CCM+是CD-Adapco公司的主打软件,其安装方式较为简单,这里以图文方式详细描述STAR CCM+11.02安装过程. 1 安装准备工作2 正式安装3 软件破解4 软件测试 1 安装准 ...

  9. 导入项目时,有关[2016-04-03 20:38:02 - Dex Loader] Unable to execute dex: Multiple dex files 问题

    最近我在学习androidUI设计,在网上找了一个UI菜单界面开源代码示例,按照步骤导入项目,运行的时候控制台结果报了如下错误: [2016-04-03 20:38:02 - Dex Loader] ...

  10. java多线程系类:JUC线程池:02之线程池原理(一)

    在上一章"Java多线程系列--"JUC线程池"01之 线程池架构"中,我们了解了线程池的架构.线程池的实现类是ThreadPoolExecutor类.本章,我 ...

随机推荐

  1. POJ-3261 Milk Patterns(后缀数组)

    题目大意:找出至少出现K次的子串的最长长度. 题目分析:二分枚举长度x,判断有没有最长公共前缀不小于x的并且连续出现了至少k次的有序子串区间. 代码如下: # include<iostream& ...

  2. python基础教程之抽象

    很早知道python,但没有坚持学习.最近心血来潮,但能弥补这个遗憾. 对象几个重要概念: 多态:可以对不同类的对象使用同样的操作: 封装:对外部世界隐藏对象的工作细节: 继承:以普通的类为基础建立专 ...

  3. C++模板编程里的主版本模板类、全特化、偏特化(C++ Type Traits)

    1.  主版本模板类 首先我们来看一段初学者都能看懂,应用了模板的程序: 1 #include <iostream> 2 using namespace std; 3 4 template ...

  4. Linux系统性能和使用活动监控工具 sysstat

    Sysstat是一个非常方便的工具,它带有众多的系统资源监控工具,用于监控系统的性能和使用情况.我们在日常使用的工具中有相当一部分是来自sysstat工具包的.同时,它还提供了一种使用cron表达式来 ...

  5. 【freemaker】之判断是否为空,表达式的使用

    测试代码 @Test public void test05(){ try { freemakerUtil.fprint(root, "05.ftl",fn+"05.htm ...

  6. Spring Boot 静态资源处理

    spring Boot 默认的处理方式就已经足够了,默认情况下Spring Boot 使用WebMvcAutoConfiguration中配置的各种属性. 建议使用Spring Boot 默认处理方式 ...

  7. CPA

    CPA.CPS.CPM.CPT.CPC 是什么 网络营销之所以越来越受到重视一个主要的原因就是因为“精准”.相比较传统媒体的陈旧广告形式,网络营销能为广告主带来更为确切的效果与回报,更有传统媒体所没有 ...

  8. [ActionScript 3.0] NetConnection建立客户端与服务器的双向连接

    一个客户端与服务器之间的接口测试的工具 <?xml version="1.0" encoding="utf-8"?> <!--- - - - ...

  9. Django进阶篇(一)

    Form django中的Form一般有两种功能: 1.输入html 2.验证用户输入 最简易的form验证: <!DOCTYPE html> <html lang="en ...

  10. Python绑定方法,未绑定方法,类方法,实例方法,静态方法

    >>> class foo(): clssvar=[1,2] def __init__(self): self.instance=[1,2,3] def hehe(self): pr ...