Python入门:jieba库的使用

jieba库是一款优秀的 Python 第三方中文分词库,jieba 支持三种分词模式:精确模式、全模式和搜索引擎模式,下面是三种模式的特点。

精确模式:试图将语句最精确的切分,不存在冗余数据,适合做文本分析

全模式:将语句中所有可能是词的词语都切分出来,速度很快,但是存在冗余数据

搜索引擎模式:在精确模式的基础上,对长词再次进行切分

一、jieba库的安装

因为 jieba 是一个第三方库,所有需要我们在本地进行安装。

Windows 下使用命令安装:在联网状态下,在命令行下输入 pip install jieba 进行安装,安装完成后会提示安装成功
这里写图片描述
在 pyCharm 中安装:打开 settings,搜索 Project Interpreter,在右边的窗口选择 + 号,点击后在搜索框搜索 jieba,点击安装即可

二、jieba三种模式的使用
# -*- coding: utf-8 -*-
import jieba

seg_str = "好好学习,天天向上。"

print("/".join(jieba.lcut(seg_str)))    # 精简模式,返回一个列表类型的结果
print("/".join(jieba.lcut(seg_str, cut_all=True)))      # 全模式,使用 'cut_all=True' 指定 
print("/".join(jieba.lcut_for_search(seg_str)))     # 搜索引擎模式

分词效果:
这里写图片描述

三、jieba 分词简单应用

需求:使用 jieba 分词对一个文本进行分词,统计次数出现最多的词语,这里以三国演义为例

# -*- coding: utf-8 -*-
import jieba

txt = open("三国演义.txt", "r", encoding='utf-8').read()
words = jieba.lcut(txt)     # 使用精确模式对文本进行分词
counts = {}     # 通过键值对的形式存储词语及其出现的次数

for word in words:
    if len(word) == 1:    # 单个词语不计算在内
        continue
    else:
        counts[word] = counts.get(word, 0) + 1    # 遍历所有词语,每出现一次其对应的值加 1

items = list(counts.items())
items.sort(key=lambda x: x[1], reverse=True)    # 根据词语出现的次数进行从大到小排序

for i in range(3):
    word, count = items[i]
    print("{0:<5}{1:>5}".format(word, count))

统计结果:
这里写图片描述
你可以随便找一个文本文档,也可以到 https://github.com/coderjas/python-quick 下载上面例子中的文档。

四、扩展:英文单词统计

上面的例子统计实现了中文文档中出现最多的词语,接着我们就来统计一下一个英文文档中出现次数最多的单词。原理同上

# -*- coding: utf-8 -*-

def get_text():
    txt = open("1.txt", "r", encoding='UTF-8').read()
    txt = txt.lower()
    for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_‘{|}~':
        txt = txt.replace(ch, " ")      # 将文本中特殊字符替换为空格
    return txt

file_txt = get_text()
words = file_txt.split()    # 对字符串进行分割,获得单词列表
counts = {}

for word in words:
    if len(word) == 1:
        continue
    else:
        counts[word] = counts.get(word, 0) + 1 

items = list(counts.items())    
items.sort(key=lambda x: x[1], reverse=True)      

for i in range(5):
    word, count = items[i]
    print("{0:<5}->{1:>5}".format(word, count))

统计结果:
这里写图片描述

  • 249
    点赞
  • 1128
    收藏
    觉得还不错? 一键收藏
  • 39
    评论
Python中的jieba是一款常用的中文文本分词工具,它提供了简单易用的分词功能。以下是jieba的基本使用方法: 1. 安装jieba:首先,需要安装jieba。可以使用pip命令进行安装:`pip install jieba` 2. 导入jieba:在Python脚本中,使用`import jieba`语句导入jieba。 3. 分词操作:使用`jieba.cut()`函数进行分词操作。jieba提供了多种分词模式,常用的有精确模式、全模式和搜索引擎模式。 - 精确模式(默认模式):`jieba.cut(sentence)`返回一个可迭代的generator对象,可以通过for循环遍历获取分词结果。 ```python import jieba sentence = "我喜欢用Python编程" words = jieba.cut(sentence) for word in words: print(word) ``` - 全模式:`jieba.cut(sentence, cut_all=True)`会将句子中所有可能的词均进行分词,返回一个可迭代的generator对象。 ```python import jieba sentence = "我喜欢用Python编程" words = jieba.cut(sentence, cut_all=True) for word in words: print(word) ``` - 搜索引擎模式:`jieba.cut_for_search(sentence)`在精确模式的基础上,对长词再次切分,返回一个可迭代的generator对象。 ```python import jieba sentence = "我喜欢用Python编程" words = jieba.cut_for_search(sentence) for word in words: print(word) ``` 4. 添加自定义词典:jieba还支持用户自定义词典,可以添加特定的词语或词频,以提高分词准确性。 ```python import jieba sentence = "我喜欢用Python编程" jieba.add_word("Python") words = jieba.cut(sentence) for word in words: print(word) ``` 以上是jieba的基本使用方法,你可以根据具体需求选择不同的分词模式,并通过自定义词典来增加分词准确性。jieba还提供了许多其他功能,如关键词提取、词性标注等,你可以参考官方文档来了解更多用法和功能。
评论 39
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值