自然语言处理(NLP) 是指用算法对人类口头表达或书面提供的自然语言信息进行处理的技术,自然语言处理属于人工智能和语言学的交叉学科
中文分词工具主要是Jieba。不仅能提供分词,还提供关键词提取和词性标注等功能。以下是Jieba分词的三种模式
1:精确模式 试图将句子最精确的切开 适合文本分析
2:全模式 把句子中所有可以成词的词语都扫描出来 速度非常快 但是不能解决歧义
3:搜索引擎模式 在精确模式的基础上 对长词进行切分 提高召回率 适合用于搜索引擎分词
同时Jieba还支持繁体分词 自定义词典 MIT授权协议等等...
分词效果如下
代码如下
import jieba.posseg as pseg
import jieba.analyse
list0=jieba.cut('东北林业大学的猫科动物专家判定,这只野生东北虎属于定居虎',cut_all=True)
print('全模式',list(list0))
list1=jieba.cut('东北林业大学的猫科动物专家判定,这只野生东北虎属于定居虎',cut_all=False)
print('精确模式',list(list1))
list2=jieba.cut('东北林业大学的猫科动物专家判定,这只野生东北虎属于定居虎')
print('搜索引擎模式',list(list2))
同样可以使用停用词 对文本进行分词 停用词就是在自然语言处理时可以自动或手动选择忽略的某些字和词
代码如下
扫描二维码关注公众号,回复:
14515751 查看本文章
![](/qrcode.jpg)
import jieba.posseg as pseg
import jieba.analyse
def stopwords(filepath):
f=open(filepath,'r',encoding='utf-8')
txt=f.readlines()
stopwords=[]
for line in txt:
stopwords.append(line.strip())
return stopwords
inputs=open('zhangsan.txt','rb')
stopwords=stopwords('zhangsan.txt')
outstr=''
for line in inputs:
sentence_seged=jieba.cut(line.strip())
for word in sentence_seged:
if word not in stopwords:
if word!='\t':
outstr+=''+word
outstr+=''
print(outstr)