机器学习19-自然语言处理包(NLTK)

本文介绍如何使用词袋法进行文本特征向量化,以及利用NLTK进行文本词汇分割、词干提取和词性标注等自然语言处理基础分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用词袋法(Bag-of-Words)对示例文本进行特征向量化

from sklearn.feature_extraction.text import CountVectorizer

sent1 = 'The cat is waking in the bedroom'
sent2 =  'A dog was running across the kitchen'

count_vec = CountVectorizer()

sentences = [sent1, sent2]

print(count_vec.fit_transform(sentences).toarray())
#out[]:
# [[0 1 1 0 1 1 0 0 2 1 0]
#  [1 0 0 1 0 0 1 1 1 0 1]]

#输出向量各个维度的特征含义
print(count_vec.get_feature_names())
#out[]:
# ['across', 'bedroom', 'cat', 'dog', 'in', 'is', 'kitchen', 'running', 'the', 'waking', 'was']

使用NLTK对示例文本进行语言分析

在cmd中安装nltk包:

  1. pip install nltk
  2. 安装NLTK数据: import nltknltk.download()
    这里写图片描述
    然后弹出NLTK Downloader,选择All packages并点击下载。
    这里写图片描述

code

import nltk

nltk.download('punkt')
sent1 = 'The cat is waking in the bedroom'
sent2 =  'A dog was running across the kitchen'
#对句子进行词汇分割和正规化,有些情况如are'nt需要分割为are和n't;
#或者I'm要分割为I和'm
tokens_1 = nltk.word_tokenize(sent1)
print(tokens_1)
#out[]:['The', 'cat', 'is', 'waking', 'in', 'the', 'bedroom']

tokens_2 = nltk.word_tokenize(sent2)
print(tokens_2)
#out[]:['A', 'dog', 'was', 'running', 'across', 'the', 'kitchen']
#整理两句的词表,并且按照ASCII的排序输出。
vocab_1 = sorted(set(tokens_1))
print(vocab_1)
#out[]:['The', 'bedroom', 'cat', 'in', 'is', 'the', 'waking']
vocab_2 = sorted(set(tokens_2))
print(vocab_2)
#out[]:['A', 'across', 'dog', 'kitchen', 'running', 'the', 'was']
#初始化stemmer寻找各个词汇最原始的词根。
stemmer = nltk.stem.PorterStemmer()
stem_1 = [stemmer.stem(t) for t in tokens_1]
print(stem_1)
#out[]:['the', 'cat', 'is', 'wake', 'in', 'the', 'bedroom']
stem_2 = [stemmer.stem(t) for t in tokens_2]
print(stem_2)
#out[]:['A', 'dog', 'wa', 'run', 'across', 'the', 'kitchen']
#初始化词性标注器,对每个词汇进行标注。
pos_tag_1 = nltk.tag.pos_tag(tokens_2)
print(pos_tag_1)
#out[]:[('A', 'DT'), ('dog', 'NN'), ('was', 'VBD'), ('running', 'VBG'), ('across', 'IN'), ('the', 'DT'), ('kitchen', 'NN')]
pos_tag_2 = nltk.tag.pos_tag(tokens_2)
print(pos_tag_2)
#out[]:[('A', 'DT'), ('dog', 'NN'), ('was', 'VBD'), ('running', 'VBG'), ('across', 'IN'), ('the', 'DT'), ('kitchen', 'NN')]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值