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

1.文本语料库

1)内容

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

包含了Gutenberg语料库,网络与聊天文本,Brown语料库,Reuters语料库,就职演说语料库(inaugural),udhr(包含有超过300种语言的世界人权宣言)。许多文本语料库都包含了语言学标注,有词性标注,命名实体,句法结构,语义角色等等。


2)文本语料库的结构

*Isolated: 孤立的,没有任何结构,仅仅是一个文本集合,eg. gutenberg, webtext, udhr.

*Categorized: 按照对应的文体,来源,作者,语言等分类,eg. brown.

*Overlapping: 分类间会重叠,因为一个文本可能与多个主题相关,eg. reuters.

*Temporal: 该类语料库可以表示随时间变化,语言用法的改变,eg. inaugural.


3)NLTK中定义的基本语料库函数

这些函数由nltk.corpus.reader提供。

(1)帮助文档对reader的说明:

NLTK corpus readers.

The modules in this package provide functions that can be used to read corpus fileids in a variety of formats. (用不同格式读取语料库的文本)

These functions can be used to read both the corpus fileids that are distributed in the NLTK corpus package, and corpus fileids that are part of external corpora. (文本可来源于外部或内部语料库)

Corpus Reader Functions

Each corpus module defines one or more "corpus reader functions", which can be used to read documents from that corpus. (每个语料库模块定义了一个或多个这样的函数)

These functions take an argument, 'item', which is used to indicate which document should be read from the corpus: - If 'item' is one of the unique identifiers listed in the corpus module's 'items' variable, then the corresponding document will be loaded from the NLTK corpus package. If 'item' is a fileid, then that file will be read. Additionally, corpus reader functions can be given lists of item names; in which case, they will return a concatenation of the corresponding documents. (可提供一个item;也可提供一个item列表,这将会返回所有对应文档的一个串联。)

Corpus reader functions are named based on the type of information they return.

Some common examples, and their return types, are:

words(): list of str

sents(): list of (list of str)

paras(): list of (list of (list of str))

tagged_words(): list of (str,str) tuple

tagged_sents(): list of (list of (str,str))

tagged_paras(): list of (list of (list of (str,str)))

chunked_sents(): list of (Tree w/ (str,str) leaves)

parsed_sents(): list of (Tree with str leaves)

parsed_paras(): list of (list of (Tree with str leaves))

xml(): A single xml ElementTree

raw(): unprocessed corpus contents

For example, to read a list of the words in the Brown Corpus, use 'nltk.corpus.brown.words()':

>>> from nltk.corpus import brown

>>> print(", ".join(brown.words()))

The, Fulton, County, Grand, Jury, said, ...

(2)某些函数的说明:

fileids(): 显示语料库中的所有文件

fileids([categories]): 语料库中指定分类下的文件

categories(): 显示语料库中的所有分类

categories([fileids]): 显示文件在语料库中的分类(不是共同分类,类别按字母表顺序显示)

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

raw(fileids=[f1, f2, f3]): 指定文件的原始内容

raw(categories=[c1, c2]): 指定分类下的原始内容

words(): 整个语料库中的词汇

words(fileids=[f1, f2, f3]): 指定文件中的词汇

words(categories=[c1, c2]): 指定分类下的词汇

sents(): 整个语料库中的句子

sents(fileids=[f1, f2, f3]): 指定文件中的句子

sents(categories=[c1, c2]): 指定分类下的句子

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

encoding(fileid): 文件的编码

open(fileid): 打开指定文件的文件流

(3)载入自己的语料库

from nltk.corpus import PlaintextCorpusReader

corpus_root = '/home/annefu/'

wordlists = PlaintextCorpusReader(corpus_root, 'datafile.txt')


2. 条件频率分布

条件频率分布是频率分布的集合,每个频率分布关联一个条件。--> (条件,事件)的搭配对

from nltk.probability import *

cfd = ConditionalFreqDist([(con1, event1), (con2, event2), ......])

cfd.conditions()     #查看条件

list(cfd[condition])     #查看condition下的所有事件

cfd[condition][event]     #查看condition下event的频率

cfd.tabulate()     #为条件频率分布制表

cfd.tabulate(conditions, samples)    """在指定条件和样本下制表(指定参数condtitions和samples)"""


3. 词典资源

1)词项(Lexical entry)包括词目(Headword,也叫词条(Lemma))及其他附加信息,例如词性(Part of speech, or lexical category)和词意定义(Sense definition, or gloss)。

两个含义不同但拼写相同的词被称为同音异义词(Homonyms)。


2)词汇语料库

(1)nltk.corpus.words.words()     #获得现有的词汇列表

(2)from nltk.corpus import stopwords

          stopwords.words('english')    """获得英语中的停用词(人类语言中包含的功能词,这些功能词极其普遍,与其他词相比,功能词没有什么实际含义,比如'the'、'is'、'at'、'which'、'on'等)列表"""


3)比较词表

Swadesh wordlists, 包含几种语言的约200个常用词列表。

from nltk.corpus import swadesh

swadesh.fileids()     #查看所支持的语言

swadesh.words('en')     #查看英语中的常用词列表

fr2en = swadesh.entries(['fr', 'en'])     #法语与英语中的同源词(eg. ('je', 'I'), ('il', 'he'))

translate = dict(fr2en)     #转换为简单的法语-英语词典


4)词汇工具:Toolbox


5)名字语料库

nltk.corpus.names

包含了按性别分类的名字。男性和女性的名字存储在单独的文件中。


6)发音词典

NLTK包含美国英语的CMU发音词典。


4.WordNet

WordNet是面向语义的英语词典,由同义词集组成,并且形成一个网络。

词条:同义词集和词的配对。

上位词,下位词

from nltk.corpus import wordnet as wn

wn.synsets(word)    #获取指定词的所有同义词集

eg. wn.synsets('motorcar') => [Synset('car.n.01')],说明motorcar只有一个可能的含义,被定义为car.n.01,表示car的第一个名词意义。

wn.synset('car.n.01').lemma_names()     #指定同义词集内的所有词

wn.synset('car.n.01').definition()     #指定同义词集的一般的定义

wn.synset('car.n.01').examples()     #获得相应的例句

wn.synset('car.n.01').lemmas()     #指定同义词集的所有词条

wn.lemma('car.n.01.automobile')     #查找特定的词条

wn.lemma('car.n.01.automobile').synset()     #得到一个词条所对应的同义词集

wn.lemma('car.n.01.automobile').name()     #得到一个词条的名字

motorcar = wn.synset('car.n.01')

motorcar.hyponyms()     #得到指定同义词集的下位同义词集

motorcar.hypernyms()     #得到指定同义词集的上位同义词集

motorcar.hypernym_paths()     #得到指定同义词集的上位词路径(嵌套列表的形式)

motorcar.root_hypernyms()     #得到根上位同义词集

表示包含关系:

wn.synset('tree.n.01').part_meronyms()    

wn.synset('tree.n.01').substance_meronyms()

wn.synset('tree.n.01').member_holonyms()


wn.synset('walk.v.01').entailments()     #表示蕴涵关系


wn.lemma('supply.n.02.supply').antonyms()     #得到指定词条的反义词条


语义相似度的度量:

set1 = wn.synset(synset1)

set2 = wn.synset(synset2)

set1.lowest_common_hypernyms(set2)     #两个同义词集共同的最低上位同义词集

set1.min_depth()     #指定同义词集的深度

set1.path_similarity(set2)     #范围在0~1,两者之间没有路径就返回-1









你可能感兴趣的:(Python自然语言处理学习笔记(二))