import nltk
sentence = 'I love natural language processing!'
tokens = nltk.word_tokenize(sentence)
print(tokens)
['I', 'love', 'natural', 'language', 'processing', '!']
tagged = nltk.pos_tag(tokens)
print(tagged)
[('I', 'PRP'), ('love', 'VBP'), ('natural', 'JJ'), ('language', 'NN'), ('processing', 'NN'), ('!', '.')]
nltk.download('maxent_ne_chunker')
[nltk_data] Downloading package maxent_ne_chunker to
[nltk_data] C:\Users\yuquanle\AppData\Roaming\nltk_data...
[nltk_data] Unzipping chunkers\maxent_ne_chunker.zip.
True
nltk.download('words')
[nltk_data] Downloading package words to
[nltk_data] C:\Users\yuquanle\AppData\Roaming\nltk_data...
[nltk_data] Unzipping corpora\words.zip.
True
entities = nltk.chunk.ne_chunk(tagged)
print(entities)
(S I/PRP love/VBP natural/JJ language/NN processing/NN !/.)
nltk.download('brown')
[nltk_data] Downloading package brown to
[nltk_data] C:\Users\yuquanle\AppData\Roaming\nltk_data...
[nltk_data] Package brown is already up-to-date!
True
from nltk.corpus import brown
brown.words()
['The', 'Fulton', 'County', 'Grand', 'Jury', 'said', ...]
from nltk.metrics import precision, recall, f_measure
reference = 'DET NN VB DET JJ NN NN IN DET NN'.split()
test = 'DET VB VB DET NN NN NN IN DET NN'.split()
reference_set = set(reference)
test_set = set(test)
print("precision:" + str(precision(reference_set, test_set)))
print("recall:" + str(recall(reference_set, test_set)))
print("f_measure:" + str(f_measure(reference_set, test_set)))
precision:1.0
recall:0.8
f_measure:0.8888888888888888
from nltk.stem.porter import *
# 创建词干提取器
stemmer = PorterStemmer()
plurals = ['caresses', 'flies', 'dies', 'mules', 'denied']
singles = [stemmer.stem(plural) for plural in plurals]
print(' '.join(singles))
caress fli die mule deni
from nltk.stem.snowball import SnowballStemmer
print(" ".join(SnowballStemmer.languages))
arabic danish dutch english finnish french german hungarian italian norwegian porter portuguese romanian russian spanish swedish
# 指定语言
stemmer = SnowballStemmer("english")
print(stemmer.stem("running"))
run
import nltk
nltk.download('sentiwordnet')
[nltk_data] Downloading package sentiwordnet to
[nltk_data] C:\Users\yuquanle\AppData\Roaming\nltk_data...
[nltk_data] Unzipping corpora\sentiwordnet.zip.
True
from nltk.corpus import sentiwordnet as swn
breakdown = swn.senti_synset('breakdown.n.03')
print(breakdown)
print(breakdown.pos_score())
print(breakdown.neg_score())
print(breakdown.obj_score())
0.0
0.25
0.75
print(list(swn.senti_synsets('slow')))
[SentiSynset('decelerate.v.01'), SentiSynset('slow.v.02'), SentiSynset('slow.v.03'), SentiSynset('slow.a.01'), SentiSynset('slow.a.02'), SentiSynset('dense.s.04'), SentiSynset('slow.a.04'), SentiSynset('boring.s.01'), SentiSynset('dull.s.08'), SentiSynset('slowly.r.01'), SentiSynset('behind.r.03')]
happy = swn.senti_synsets('happy', 'a')
print(list(happy))
[SentiSynset('happy.a.01'), SentiSynset('felicitous.s.02'), SentiSynset('glad.s.02'), SentiSynset('happy.s.04')]
另外,代码我已经上传github:https://github.com/yuquanle/StudyForNLP/blob/master/NLPtools/NLTKDemo.ipynb
公众号:StudyForAI(小白人工智能入门学习)