jieba分词的简单使用

原文链接:http://chenhao.space/post/20300dce.html

jieba的三种分词模式

Jieba中文分词包含三种模式,下面来介绍一下这三种模式的不同:

  1. 精确模式:试图将句子最精确地切开,适合文本分析;
  2. 全模式:把句子中所有的可以成词的词语都扫描出来, 速度非常快,但是不能解决歧义问题;
  3. 搜索引擎模式:在精确模式的基础上,对长词再次切分,提高召回率,适合用于搜索引擎分词。

同时结巴分词支持繁体分词和自定义字典方法。

全模式

import jieba
seg_list = jieba.cut("我来到南京东南大学", cut_all=True)
print("Full Mode: " + "/ ".join(seg_list))  # 全模式

# Output
Full Mode:/ 来到/ 南京/ 京东/ 东南/ 东南大学/ 南大/ 大学

精确模式

精确模式也是默认模式

seg_list = jieba.cut("我来到南京东南大学", cut_all=False)
print("Default Mode: " + "/ ".join(seg_list))  # 精确模式

# Output
Default Mode:/ 来到/ 南京/ 东南大学

搜索引擎模式

seg_list = jieba.cut_for_search("我来到南京东南大学")  # 搜索引擎模式
print(", ".join(seg_list))

# Output
Search Mode:/ 来到/ 南京/ 东南/ 南大/ 大学/ 东南大学

jieba.cut()与jieba.lcut()的区别

jieba.cut生成的是一个生成器,generator,也就是可以通过for循环来取里面的每一个词。
jieba.lcut 直接生成的就是一个list。

jieba.cut()

import jieba

seg_list = jieba.cut("贪心学院专注于人工智能教育", cut_all=False)
for i in seg_list:
    print(i)
    
# Output
贪心学院
专注
于
人工智能
教育

jieba.lcut()

import jieba

seg_list = jieba.lcut("贪心学院专注于人工智能教育", cut_all=False)
print(seg_list)

# Output
['贪心学院', '专注', '于', '人工智能', '教育']

添加自己定义词语(jieba.add_word)

import jieba

seg_list = jieba.cut("贪心学院专注于人工智能教育", cut_all=False)
print("Default Mode: " + "/".join(seg_list))

jieba.add_word("贪心学院")
seg_list = jieba.cut("贪心学院专注于人工智能教育", cut_all=False)
print("Default Mode: " + "/".join(seg_list))


# Output
Default Mode: 贪心/学院/专注//人工智能/教育
Default Mode: 贪心学院/专注//人工智能/教育

添加停用词

出现频率特别高的和频率特别低的词对于文本分析帮助不大,一般在预处理阶段会过滤掉。
在英文里,经典的停用词为 “The”, “an”…

方法一:自己建立一个停用词词典

stop_words = ['the', 'an', 'is', 'there']
# 在使用时:假设 word_list 包含了文本里的单词
word_list = ['we', 'are', 'the', 'students']
filtered_words = [word for word in word_list if word not in stop_words]
print(filtered_words)


# Output
['we', 'are', 'students']

方法二: 直接利用别人已经构建好的停用词库

from nltk.corpus import stopwords
cachedStopWords = stopwords.words('english')
print(cachedStopWords)

word_list = ['we', 'are', 'the', 'students']
filtered_words = [word for word in word_list if word not in cachedStopWords]
print(filtered_words)


# Output
['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', 'her', 'hers', 'herself', 'it', 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', 'should', 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', 'couldn', 'didn', 'doesn', 'hadn', 'hasn', 'haven', 'isn', 'ma', 'mightn', 'mustn', 'needn', 'shan', 'shouldn', 'wasn', 'weren', 'won', 'wouldn']
['students']
  • 1
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值