NLTK基础

Python上著名的?然语?处理库

  • ?带语料库,词性分类库
  • ?带分类,分词,等等功能
  • 强?的社区?持
  • 还有N多的简单版wrapper

安装语料库

# 方式一
import nltk
nltk.download()

showing info https://raw.githubusercontent.com/nltk/nltk_data/gh-pages/index.xml

若下载速度慢或因其他原因下载失败

官方下载地址 http://www.nltk.org/nltk_data/

githup下载地址 https://github.com/nltk/nltk_data

  • 下载packages文件,重命名为nltk_data
from nltk import data
data.path.append(‘D:/python3.6/nltk_data‘)

功能一览表

下载语料库

# 请下载
nltk.download(‘brown‘)

[nltk_data] Downloading package brown to
[nltk_data]     C:\Users\fei\AppData\Roaming\nltk_data...
[nltk_data]   Unzipping corpora\brown.zip.

nltk自带语料库

# nltk自带语料库
from nltk.corpus import brown
brown.categories()
[‘adventure‘,
 ‘belles_lettres‘,
 ‘editorial‘,
 ‘fiction‘,
 ‘government‘,
 ‘hobbies‘,
 ‘humor‘,
 ‘learned‘,
 ‘lore‘,
 ‘mystery‘,
 ‘news‘,
 ‘religion‘,
 ‘reviews‘,
 ‘romance‘,
 ‘science_fiction‘]
brown.readme()   # 语料信息描述

print(brown.words()[:10])    # 单词
print(len(brown.words()))

[‘The‘, ‘Fulton‘, ‘County‘, ‘Grand‘, ‘Jury‘, ‘said‘, ‘Friday‘, ‘an‘, ‘investigation‘, ‘of‘]
1161192

print(brown.sents()[:10])    # 句子
print(brown.sents().__len__())
[[‘The‘, ‘Fulton‘, ‘County‘, ‘Grand‘, ‘Jury‘, ‘said‘, ‘Friday‘, ‘an‘, ‘investigation‘, ‘of‘, "Atlanta‘s", ‘recent‘, ‘primary‘, ‘election‘, ‘produced‘, ‘``‘, ‘no‘, ‘evidence‘, "‘‘", ‘that‘, ‘any‘, ‘irregularities‘, ‘took‘, ‘place‘, ‘.‘], [‘The‘, ‘jury‘, ‘further‘, ‘said‘, ‘in‘, ‘term-end‘, ‘presentments‘, ‘that‘, ‘the‘, ‘City‘, ‘Executive‘, ‘Committee‘, ‘,‘, ‘which‘, ‘had‘, ‘over-all‘, ‘charge‘, ‘of‘, ‘the‘, ‘election‘, ‘,‘, ‘``‘, ‘deserves‘, ‘the‘, ‘praise‘, ‘and‘, ‘thanks‘, ‘of‘, ‘the‘, ‘City‘, ‘of‘, ‘Atlanta‘, "‘‘", ‘for‘, ‘the‘, ‘manner‘, ‘in‘, ‘which‘, ‘the‘, ‘election‘, ‘was‘, ‘conducted‘, ‘.‘], [‘The‘, ‘September-October‘, ‘term‘, ‘jury‘, ‘had‘, ‘been‘, ‘charged‘, ‘by‘, ‘Fulton‘, ‘Superior‘, ‘Court‘, ‘Judge‘, ‘Durwood‘, ‘Pye‘, ‘to‘, ‘investigate‘, ‘reports‘, ‘of‘, ‘possible‘, ‘``‘, ‘irregularities‘, "‘‘", ‘in‘, ‘the‘, ‘hard-fought‘, ‘primary‘, ‘which‘, ‘was‘, ‘won‘, ‘by‘, ‘Mayor-nominate‘, ‘Ivan‘, ‘Allen‘, ‘Jr.‘, ‘.‘], [‘``‘, ‘Only‘, ‘a‘, ‘relative‘, ‘handful‘, ‘of‘, ‘such‘, ‘reports‘, ‘was‘, ‘received‘, "‘‘", ‘,‘, ‘the‘, ‘jury‘, ‘said‘, ‘,‘, ‘``‘, ‘considering‘, ‘the‘, ‘widespread‘, ‘interest‘, ‘in‘, ‘the‘, ‘election‘, ‘,‘, ‘the‘, ‘number‘, ‘of‘, ‘voters‘, ‘and‘, ‘the‘, ‘size‘, ‘of‘, ‘this‘, ‘city‘, "‘‘", ‘.‘], [‘The‘, ‘jury‘, ‘said‘, ‘it‘, ‘did‘, ‘find‘, ‘that‘, ‘many‘, ‘of‘, "Georgia‘s", ‘registration‘, ‘and‘, ‘election‘, ‘laws‘, ‘``‘, ‘are‘, ‘outmoded‘, ‘or‘, ‘inadequate‘, ‘and‘, ‘often‘, ‘ambiguous‘, "‘‘", ‘.‘], [‘It‘, ‘recommended‘, ‘that‘, ‘Fulton‘, ‘legislators‘, ‘act‘, ‘``‘, ‘to‘, ‘have‘, ‘these‘, ‘laws‘, ‘studied‘, ‘and‘, ‘revised‘, ‘to‘, ‘the‘, ‘end‘, ‘of‘, ‘modernizing‘, ‘and‘, ‘improving‘, ‘them‘, "‘‘", ‘.‘], [‘The‘, ‘grand‘, ‘jury‘, ‘commented‘, ‘on‘, ‘a‘, ‘number‘, ‘of‘, ‘other‘, ‘topics‘, ‘,‘, ‘among‘, ‘them‘, ‘the‘, ‘Atlanta‘, ‘and‘, ‘Fulton‘, ‘County‘, ‘purchasing‘, ‘departments‘, ‘which‘, ‘it‘, ‘said‘, ‘``‘, ‘are‘, ‘well‘, ‘operated‘, ‘and‘, ‘follow‘, ‘generally‘, ‘accepted‘, ‘practices‘, ‘which‘, ‘inure‘, ‘to‘, ‘the‘, ‘best‘, ‘interest‘, ‘of‘, ‘both‘, ‘governments‘, "‘‘", ‘.‘], [‘Merger‘, ‘proposed‘], [‘However‘, ‘,‘, ‘the‘, ‘jury‘, ‘said‘, ‘it‘, ‘believes‘, ‘``‘, ‘these‘, ‘two‘, ‘offices‘, ‘should‘, ‘be‘, ‘combined‘, ‘to‘, ‘achieve‘, ‘greater‘, ‘efficiency‘, ‘and‘, ‘reduce‘, ‘the‘, ‘cost‘, ‘of‘, ‘administration‘, "‘‘", ‘.‘], [‘The‘, ‘City‘, ‘Purchasing‘, ‘Department‘, ‘,‘, ‘the‘, ‘jury‘, ‘said‘, ‘,‘, ‘``‘, ‘is‘, ‘lacking‘, ‘in‘, ‘experienced‘, ‘clerical‘, ‘personnel‘, ‘as‘, ‘a‘, ‘result‘, ‘of‘, ‘city‘, ‘personnel‘, ‘policies‘, "‘‘", ‘.‘]]
57340

print(brown.tagged_words()[:10])    # 词性标注
print(brown.tagged_words().__len__())
[(‘The‘, ‘AT‘), (‘Fulton‘, ‘NP-TL‘), (‘County‘, ‘NN-TL‘), (‘Grand‘, ‘JJ-TL‘), (‘Jury‘, ‘NN-TL‘), (‘said‘, ‘VBD‘), (‘Friday‘, ‘NR‘), (‘an‘, ‘AT‘), (‘investigation‘, ‘NN‘), (‘of‘, ‘IN‘)]
1161192

二、文本处理流程

  • 1.preprocess
  • 2.tokenize
  • 3.stopwords
  • 4....
  • 5.make features
  • 6.machine learning

 

一、Tokenize

  把句子拆成有意义的小部件

import nltk
sentence = ‘Never underestimate the heart of a champion ‘
tokens = nltk.word_tokenize(sentence)
tokens
[‘Never‘, ‘underestimate‘, ‘the‘, ‘heart‘, ‘of‘, ‘a‘, ‘champion‘]

  中文分词

import jieba
seg_list = jieba.cut("我来到北京清华?学", cut_all=True)
print("全模式:", "/ ".join(seg_list))  # 全模式
seg_list = jieba.cut("我来到北京清华?学", cut_all=False)
print("精确模式:", "/ ".join(seg_list))  # 精确模式
seg_list = jieba.cut("他来到了?易杭研?厦") # 默认是精确模式
print(‘新词识别:‘,", ".join(seg_list))
seg_list = jieba.cut_for_search("?明硕?毕业于中国科学院计算所,后在?本京都?学深造")
print(‘搜索引擎模式:‘,‘,‘.join(seg_list))

全模式: 我/ 来到/ 北京/ 清华/ / / 学
精确模式: 我/ 来到/ 北京/ 清华/ ?/ 学
新词识别: 他, 来到, 了, ?, 易, 杭研, ?, 厦
搜索引擎模式: ?,明硕,?,毕业,于,中国,科学,学院,科学院,中国科学院,计算,计算所,,,后,在,?,本,京都,?,学,深造  

社交网络语言的分词

例子

# 社交网络语言的tokenize
from nltk.tokenize import word_tokenize

tweet = ‘RT @angelababy: love you baby! :D http://ah.love #168cm‘
print(word_tokenize(tweet))
[‘RT‘, ‘@‘, ‘angelababy‘, ‘:‘, ‘love‘, ‘you‘, ‘baby‘, ‘!‘, ‘:‘, ‘D‘, ‘http‘, ‘:‘, ‘//ah.love‘, ‘#‘, ‘168cm‘]

 解决方法:正则表达式过滤

import re
emoticons_str = r"""
    (?:
        [:=;] # 眼睛
        [oO\-]? # ??
        [D\)\]\(\]/\\OpP] # 嘴
    )"""
regex_str = [
    emoticons_str,
    r‘<[^>]+>‘, # HTML tags
    r‘(?:@[\w_]+)‘, # @某?
    r"(?:\#+[\w_]+[\w\‘_\-]*[\w_]+)", # 话题标签
    r‘http[s]?://(?:[a-z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-f][0-9a-f]))+‘,
                   # URLs
    r‘(?:(?:\d+,?)+(?:\.?\d+)?)‘, # 数字
    r"(?:[a-z][a-z‘\-_]+[a-z])", # 含有 - 和 ‘ 的单词
    r‘(?:[\w_]+)‘, # 其他
    r‘(?:\S)‘ # 其他
]

正则表达式对照表

http://www.regexlab.com/zh/regref.htm

tokens_re = re.compile(r‘(‘+‘|‘.join(regex_str)+‘)‘, re.VERBOSE | re.IGNORECASE)
emoticon_re = re.compile(r‘^‘+emoticons_str+‘$‘, re.VERBOSE | re.IGNORECASE)

def tokenize(s):
    return tokens_re.findall(s)

def preprocess(s, lowercase=False):
    tokens = tokenize(s)
    if lowercase:
        tokens = [token if emoticon_re.search(token) else token.lower() for token in
tokens]
    return tokens

tweet = ‘RT @angelababy: love you baby! :D http://ah.love #168cm‘
print(preprocess(tweet))

[‘RT‘, ‘@angelababy‘, ‘:‘, ‘love‘, ‘you‘, ‘baby‘, ‘!‘, ‘:D‘, ‘http://ah.love‘, ‘#168cm‘]

二、词型归一化

纷繁复杂的词形

  • Inflection变化: walk => walking => walked
  • 影响词性
  • derivation 引申: nation (noun) => national (adjective) => nationalize (verb)
  • 不影响词性

Stemming 词?提取:?般来说,就是把不影响词性的inflection的?尾巴砍掉

  • walking 砍ing = walk
  • walked 砍ed = walk
  • Lemmatization 词形归?:把各种类型的词的变形,都归为?个形式
  • went 归? = go
  • are 归? = be

1. 词干提取

from nltk.stem.porter import PorterStemmer
porter_stemmer = PorterStemmer()
print(porter_stemmer.stem(‘maximum‘))
print(porter_stemmer.stem(‘presumably‘))
print(porter_stemmer.stem(‘multiply‘))
print(porter_stemmer.stem(‘provision‘))

maximum
presum
multipli
provis

2. 词型归一  

from nltk.stem import WordNetLemmatizer
wordnet_lemmatizer = WordNetLemmatizer()
print(wordnet_lemmatizer.lemmatize(‘dogs‘))
print(wordnet_lemmatizer.lemmatize(‘churches‘))
print(wordnet_lemmatizer.lemmatize(‘aardwolves‘))
print(wordnet_lemmatizer.lemmatize(‘abaci‘))
print(wordnet_lemmatizer.lemmatize(‘hardrock‘))

dog
church
aardwolf
abacus
hardrock

 没有pos tag,,默认是nn

# ?有POS Tag,默认是NN 名词
wordnet_lemmatizer.lemmatize(‘are‘)
‘are‘
wordnet_lemmatizer.lemmatize(‘is‘)
‘is‘

 词性标注

  方式一:手动标注

# 加上POS Tag
print(wordnet_lemmatizer.lemmatize(‘is‘, pos=‘v‘))
print(wordnet_lemmatizer.lemmatize(‘are‘, pos=‘v‘))
‘be‘
‘be‘

  方式二:

import nltk
text = nltk.word_tokenize(‘what does the fox say‘)
print(text)
print(nltk.pos_tag(text))
[‘what‘, ‘does‘, ‘the‘, ‘fox‘, ‘say‘]
[(‘what‘, ‘WDT‘), (‘does‘, ‘VBZ‘), (‘the‘, ‘DT‘), (‘fox‘, ‘NNS‘), (‘say‘, ‘VBP‘)]

 词性关系表

三、Stopwords

?千个HE有?千种指代

?千个THE有?千种指事

对于注重理解?本『意思』的应?场景来说

歧义太多

全体stopwords列表   http://www.ranks.nl/stopwords

nltk去除stopwords

?先记得在console??下载?下词库
或者nltk.download(‘stopwords’)

from nltk.corpusimportstopwords
#先token?把,得到?个word_list
# ...
#然后filter?把
filtered_words =
[wordforwordinword_listifwordnot instopwords.words(‘english‘)]

四、nltk频率统计 

import nltk
from nltk import FreqDist
# 做个词库先
corpus = ‘this is my sentence ‘            ‘this is my life ‘            ‘this is the day‘
# 随便tokenize?下
# 显然, 正如上?提到,
# 这?可以根据需要做任何的preprocessing:
# stopwords, lemma, stemming, etc.
tokens = nltk.word_tokenize(corpus)
print(tokens)
[‘this‘, ‘is‘, ‘my‘, ‘sentence‘, ‘this‘, ‘is‘, ‘my‘, ‘life‘, ‘this‘, ‘is‘, ‘the‘, ‘day‘]

 

# 借?NLTK的FreqDist统计?下?字出现的频率
fdist = FreqDist(tokens)
# 它就类似于?个Dict
# 带上某个单词, 可以看到它在整个?章中出现的次数
print(fdist.most_common(50))
for k,v in fdist.items():
    print(k,v)

[(‘this‘, 3), (‘is‘, 3), (‘my‘, 2), (‘sentence‘, 1), (‘life‘, 1), (‘the‘, 1), (‘day‘, 1)]
this 3
is 3
my 2
sentence 1
life 1
the 1
day 1

# 好, 此刻, 我们可以把最常?的50个单词拿出来
standard_freq_vector = fdist.most_common(50)
size = len(standard_freq_vector)
print(standard_freq_vector)

[(‘this‘, 3), (‘is‘, 3), (‘my‘, 2), (‘sentence‘, 1), (‘life‘, 1), (‘the‘, 1), (‘day‘, 1)]

Func: 按照出现频率??, 记录下每?个单词的位置  

def position_lookup(v):
    res = {}
    counter = 0
    for word in v:
        res[word[0]] = counter
        counter += 1
    return res
# 把标准的单词位置记录下来
standard_position_dict = position_lookup(standard_freq_vector)
print(standard_position_dict)
# 得到?个位置对照表
{‘this‘: 0, ‘is‘: 1, ‘my‘: 2, ‘sentence‘: 3, ‘life‘: 4, ‘the‘: 5, ‘day‘: 6}

  这时,我们有个新句子

[1, 1, 0, 0, 0, 0, 0]sentence = ‘this is cool‘
# 先新建?个跟我们的标准vector同样??的向量
freq_vector = [0] * size
# 简单的Preprocessing
tokens = nltk.word_tokenize(sentence)
# 对于这个新句??的每?个单词
for word in tokens:
    try:
        # 如果在我们的词库?出现过
        # 那么就在"标准位置"上+1
        freq_vector[standard_position_dict[word]] += 1
    except KeyError:
        # 如果是个新词
        # 就pass掉
        continue
print(freq_vector)
# 第?个位置代表 is, 出现了?次
# 第?个位置代表 this, 出现了?次
# 后?都?有
[1, 1, 0, 0, 0, 0, 0]

 五、nltk实现tf-idf

import nltk
from nltk.text import TextCollection
sents = [‘this is sentence one‘, ‘this is sentence two‘, ‘this is sentence three‘]
sents = [nltk.word_tokenize(sent) for sent in sents]
corpus = TextCollection(sents)

# 直接就能算出tfidf
# (term: ?句话中的某个term, text: 这句话)
print(corpus.idf(‘three‘))
print(corpus.tf(‘four‘,nltk.word_tokenize(‘this is a sentence four‘)))
print(corpus.tf_idf(‘four‘,nltk.word_tokenize(‘this is a sentence four‘)))

1.0986122886681098
0.2
0.0

 六、svd降维

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
la = np.linalg
words = [‘I‘,‘like‘,‘enjoy‘,‘deep‘,‘learning‘,‘NLP‘,‘flying‘]
X = np.array([[0,2,1,0,0,0,0,0],
              [2,0,0,1,0,1,0,0],
              [1,0,0,0,0,0,1,0],
              [0,1,0,0,1,0,0,0],
              [0,0,0,1,0,0,0,1],
              [0,1,0,0,0,0,0,1],
              [0,0,1,0,0,0,0,1],
              [0,0,0,0,1,1,1,0],
             ])
U,s,Vh = la.svd(X,full_matrices=False)
# print(U,s,Vh)
for i in range(len(words)):
    plt.text(U[i,0],U[i,1],words[i])

plt.xlim(-1,1)
plt.ylim(-1,1)
plt.show()

  

七、nltk经典应用-情感分析

简单的情感分析

import nltk
words = nltk.word_tokenize(‘I am very happy,i love you‘)
sentiment_dictionary = {}
for line in open(‘data/AFINN/AFINN-111.txt‘):
    word, score = line.split(‘\t‘)
    sentiment_dictionary[word] = int(score)
# 把这个打分表记录在?个Dict上以后
# 跑?遍整个句?,把对应的值相加
total_score = sum(sentiment_dictionary.get(word, 0) for word in words)
# 有值就是Dict中的值,没有就是0
# 于是你就得到了?个 sentiment score
print(total_score)
6

  配上ML的情感分析

from nltk.classify import NaiveBayesClassifier
# 随?造点训练集
s1 = ‘this is a good book‘
s2 = ‘this is a awesome book‘
s3 = ‘this is a bad book‘
s4 = ‘this is a terrible book‘
def preprocess(s):
    # Func: 句?处理
    # 这?简单的?了split(), 把句?中每个单词分开
    # 显然 还有更多的processing method可以?
    return {word: True for word in s.lower().split()}
    # return?这样:
    # {‘this‘: True, ‘is‘:True, ‘a‘:True, ‘good‘:True, ‘book‘:True}
    # 其中, 前?个叫fname, 对应每个出现的?本单词;
    # 后?个叫fval, 指的是每个?本单词对应的值。
        # 这?我们?最简单的True,来表示,这个词『出现在当前的句?中』的意义。
    # 当然啦, 我们以后可以升级这个?程, 让它带有更加?逼的fval, ?如 word2vec

# 把训练集给做成标准形式
training_data = [[preprocess(s1), ‘pos‘],
                 [preprocess(s2), ‘pos‘],
                 [preprocess(s3), ‘neg‘],
                 [preprocess(s4), ‘neg‘]]
# 喂给model吃
model = NaiveBayesClassifier.train(training_data)
# 打出结果
print(model.classify(preprocess(‘this is a good book‘)))
pos

八、nltk应用-文本相似度

原文地址:https://www.cnblogs.com/zhangyafei/p/10618585.html

时间: 2024-08-27 03:58:32

NLTK基础的相关文章

AI 经典书单 | 人工智能学习该读哪些书

转载 2018年01月16日 00:00:00 人工智能相关岗位中,涉及到的内容包含: 算法.深度学习.机器学习.自然语言处理.数据结构.Tensorflow.Python .数据挖掘.搜索开发.神经网络.视觉度量.图像识别.语音识别.推荐系统.系统算法.图像算法.数据分析.概率编程.计算机数学.数据仓库.建模等关键词,基本涵盖了现阶段人工智能细分领域的人才结构. 将上面的岗位涉及到的知识和技术划类,就形成了今天的五份书单: 1人工智能科普类:人工智能科普.人工智能哲学 <智能的本质>斯坦福.

学习笔记CB014:TensorFlow seq2seq模型步步进阶

神经网络.<Make Your Own Neural Network>,用非常通俗易懂描述讲解人工神经网络原理用代码实现,试验效果非常好. 循环神经网络和LSTM.Christopher Olah http://colah.github.io/posts/2015-08-Understanding-LSTMs/ . seq2seq模型基于循环神经网络序列到序列模型,语言翻译.自动问答等序列到序列场景,都可用seq2seq模型,用seq2seq实现聊天机器人的原理 http://suriyade

学习要趁早,专栏上新,早鸟订阅送图书

51CTO博客专栏又有新内容了<负载均衡高手炼成记>从入门到实操,Linux老鸟带你走上高并发架构之路. 为了贯彻学习要趁早的理念,我们特意为前100名订阅专栏的小伙伴免费赠送纸质图书一本,特别强调!!是!纸!质!图!书!! 每位订阅专栏的小伙伴可在以下书单中任选一本,因每类图书数量有限,先选先得.目前已订阅过<负载均衡高手炼成记>的小伙伴可直接链接小助手(微信:cto51shequ)进行领取. 好了,废话不再多说,上书单: 书名 定价 书名 文本上的算法 深入浅出自然语言处理 6

原 iBooker ML 群的资料打包分享

很多人问我们的 ML 群为啥加不进去,很不幸的是,这个群挂了. 在群挂了之前,我们把所有群文件备份到了百度云. 目录 自动驾驶 无人驾驶汽车技术及其发展探究.caj 第一本无人驾驶技术书.pdf ROS机器人程序设计 原书第2版.pdf 中文文档 计算广告.epub <谁说菜鸟不会数据分析>入门篇-简版电子书.pdf xgboost.docx Unix入门经典.pdf The Linux Command Line(中文版)好奇猫团队翻译.pdf Python机器学习基础教程.pdf Panda

java web 开发三剑客 -------电子书

Internet,人们通常称为因特网,是当今世界上覆盖面最大和应用最广泛的网络.根据英语构词法,Internet是Inter + net,Inter-作为前缀在英语中表示“在一起,交互”,由此可知Internet的目的是让各个net交互.所以,Internet实质上是将世界上各个国家.各个网络运营商的多个网络相互连接构成的一个全球范围内的统一网,使各个网络之间能够相互到达.各个国家和运营商构建网络采用的底层技术和实现可能各不相同,但只要采用统一的上层协议(TCP/IP)就可以通过Internet

NLTK与NLP原理及基础

参考https://blog.csdn.net/zxm1306192988/article/details/78896319 以NLTK为基础配合讲解自然语言处理的原理  http://www.nltk.org/ Python上著名的自然语?处理库 自带语料库,词性分类库 自带分类,分词,等功能 强?的社区?持 还有N多的简单版wrapper,如 TextBlob NLTK安装(可能需要预先安装numpy) pip install nltk 安装语料库 import nltk nltk.down

Python自然语言工具包(NLTK)入门

在本期文章中,小生向您介绍了自然语言工具包(Natural Language Toolkit),它是一个将学术语言技术应用于文本数据集的 Python 库.称为“文本处理”的程序设计是其基本功能:更深入的是专门用于研究自然语言的语法以及语义分析的能力. 鄙人并非见多识广, 语言处理(linguistic processing) 是一个相对新奇的领域.如果在对意义非凡的自然语言工具包(NLTK)的说明中出现了错误,请您谅解.NLTK 是使用 Python 教学以及实践计算语言学的极好工具.此外,计

【NLP】干货!Python NLTK结合stanford NLP工具包进行文本处理

干货!详述Python NLTK下如何使用stanford NLP工具包 作者:白宁超 2016年11月6日19:28:43 摘要:NLTK是由宾夕法尼亚大学计算机和信息科学使用python语言实现的一种自然语言工具包,其收集的大量公开数据集.模型上提供了全面.易用的接口,涵盖了分词.词性标注(Part-Of-Speech tag, POS-tag).命名实体识别(Named Entity Recognition, NER).句法分析(Syntactic Parse)等各项 NLP 领域的功能.

NLTK学习笔记(四):自然语言处理的一些算法研究

自然语言处理中算法设计有两大部分:分而治之 和 转化 思想.一个是将大问题简化为小问题,另一个是将问题抽象化,向向已知转化.前者的例子:归并排序:后者的例子:判断相邻元素是否相同(与排序). 这次总结的自然语言中常用的一些基本算法,算是入个门了. 递归 使用递归速度上会受影响,但是便于理解算法深层嵌套对象.而一些函数式编程语言会将尾递归优化为迭代. 如果要计算n个词有多少种组合方式?按照阶乘定义:n! = n*(n-1)*...*1 def func(wordlist): length = le