文本挖掘应用举例
小说数据处理、情感分析、评论数据分析等。
较详细地参考链接:
Python中文分词 jieba 十五分钟入门与进阶
前期准备
1.安装jieba
pip install jieba
2.基础
# -*- coding: GBK -*-
'''
一 分词的四种模式
'''
import jieba
sentence = '我喜欢上海东方明珠'
w1 = jieba.cut(sentence,cut_all = True)
#print(w1)generator对象
#cut_all对应着分词模式,其取值为True时,代表着全模式,分词可能会出现叠加
#针对generator object,通过循环遍历出结果
#for item in w1:
# print(item)
'''
全模式输出结果展示:
我
喜欢
上海
上海东方
海东
东方
东方明珠
方明
明珠
'''
w2 = jieba.cut(sentence,cut_all = False)#精准模式
#for item in w2:
# print(item)
'''
精准模式输出结果展示:
我
喜欢
上海
东方明珠
'''
w3 = jieba.cut_for_search(sentence)#搜索引擎模式
#for item in w3:
# print(item)
'''
搜索引擎模式输出结果展示:
(计算网页与我们语句的相关度,计算网页权重)
我
喜欢
上海
东方
方明
明珠
东方明珠
'''
w4 = jieba.cut(sentence)#默认模式
#for item in w4:
# print(item)
'''
默认使用精准模式
我
喜欢
上海
东方明珠
'''
'''
二 词性标注
'''
#既切分,同时得到对应的词性
import jieba.posseg
w5 = jieba.posseg.cut(sentence)
#.flag 词性
#.word 词语
#for item in w5:
# print(item.word + ' ' + item.flag)
'''
我 r
喜欢 v
上海 ns
东方明珠 nr
a adj
c conj
d adv
e 感叹词
f 方位词
i 成语
m 数词
n 普通名词
nr 人名
ns 地名
nt 机构团体
nz 其他常用专有名词
p prep
r 代词
t time
u 助词
v verb
vn 动名词
w 标点符号
un 未知词语
'''
'''
dict.txt的建立
注意词库的更新
词语 词频 词性(可选)
'''
'''
三 词典的加载
'''
jieba.load_userdict('F:\python35\Lib\site-packages\jieba\dict_2.txt')
sentence_2 = '天瑞智能是一个很好的机构'
w6 = jieba.posseg.cut(sentence_2)
#for item in w6:
# print(item.word + ' ' + item.flag)
'''
第一行是新添加的词典
天瑞智能 nt
是 v
一个 m
很好 a
的 uj
机构 n
'''
'''
四 词频的更改(存疑)
'''
sentence_3 = '桃运科技是做云计算的。'
w7 = jieba.cut(sentence)
#for item in w7:
# print(item)
'''
桃运
科技
是
做
云
计算
的
。
我
喜欢
上海
东方明珠
'''
jieba.add_word('上海东方')#添加到字典中
w8 = jieba.cut(sentence)
#for item in w8:
# print(item)
'''
我
喜欢
上海
东方明珠
'''
jieba.suggest_freq('上海东方',True)
w9 = jieba.cut(sentence)
#for item in w9:
# print(item)
'''
我
喜欢
上海
东方明珠
'''
'''
五 输出句子中词频较高的关键词
'''
import jieba.analyse
tag = jieba.analyse.extract_tags(sentence,3)#默认输出20个关键词
#print(tag)
'''
['东方明珠', '喜欢', '上海']
'''
'''
六 返回词语在文档中是第几个位置
'''
w9 = jieba.tokenize(sentence)
#for item in w9:
# print(item)
'''
('我', 0, 1)
('喜欢', 1, 3)
('上海', 3, 5)
('东方明珠', 5, 9)
'''
w10 = jieba.tokenize(sentence,mode = 'search')
for item in w10:
print(item)
'''
以搜索引擎的方式返回:
('我', 0, 1)
('喜欢', 1, 3)
('上海', 3, 5)
('东方', 5, 7)
('方明', 6, 8)
('明珠', 7, 9)
('东方明珠', 5, 9)
'''
版权声明:本文为Seal_Wings原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。