python中jieba模块的深入了解

这篇文章主要介绍了python中jieba模块的深入了解,jieba模块是一个python第三方中文分词模块,可以用于将语句中的中文词语分离出来

一、前言        

英语单词之间是通过空格分隔的,但是中文却不存在空格的概念,因此需要一个模块来解决中文的分词问题。jieba模块是一个python第三方中文分词模块,可以用于将语句中的中文词语分离出来

此外,全国计算机等级考试二级python语言程序设计也涉及到该模块的相关知识。因此大家可以好好了解下该模块。

二、模块的安装

 jieba模块作为python的一个第三方模块,是需要我们自行下载安装后才能使用的,我们主要采用pip安装工具进行jieba的安装,具体步骤如下:

在windows操作系统中,快捷键win+R

然后输入cmd,点击确定,打开

输入:

pip install jieba 

即可安装成功。

三、jieba模块具体讲解

3.1分词模式

jieba模块支持三种分词模式:全模式、精准模式以及搜索引擎模式。

①全模式:全模式可以将句子中所有可能的词语全部提取出来,该模式提取速度快,但可能会出现冗余词汇

如图,第一行出现了冗余词汇,其采用的就是全模式,而第二行采用精准模式。

②精准模式:精准模式通过优化的智能算法将语句精准的分隔,适用于文本分析

③搜索引擎模式:搜索引擎模式在精准模式的基础上对词语进行再次划分,提高召回率,适用于搜索引擎分词。 

3.2cut()、lcut()

3.2.1cut(sentence, cut_all=False, HMM=True, use_paddle=False)

参数解析:

  sentence:要分割的str(unicode)。

  cut_all:模型类型。True 表示全模式,False 表示精准模式。其默认为精准模式。

  HMM:是否使用隐马尔可夫模型。

函数功能: 

The main function that segments an entire sentence that contains Chinese characters into separated words.

将包含汉字的整个句子分割成单独的单词的主要功能。

import jieba sentence = 'python是世界上最好的编程语言' ls = jieba.cut(sentence, cut_all=False) print(ls) # 

print(type(ls)) # 

 

如图,其是迭代器类型,可以用以下三种方式显示结果

①' '.join()

# ①''.join ls_1 = ' '.join(ls) print(ls_1) # python 是 世界 上 最好 的 编程 编程语言 语言

②for循环遍历 

# ②for循环遍历 for i in ls: print(i) ''' python 是 世界 上 最好 的 编程语言 '''

③列表推导式

# ③列表推导式 ls_2 = [i for i in ls] print(ls_2) # ['python', '是', '世界', '上', '最好', '的', '编程语言']

3.2.2lcut(sentence,cut_all=False)

    def lcut(self, *args, **kwargs): return list(self.cut(*args, **kwargs))

查看jieba模块,其定义lcut()函数如上,可以发现lcut()函数最终返回的是list(cut())

import jieba sentence = 'python是世界上最好的编程语言' ls = jieba.cut(sentence, cut_all=False) print(ls) print(list(ls)) ls1 = jieba.lcut(sentence, cut_all=True) print(ls1) ls2 = jieba.lcut(sentence) print(ls2)

结果如下 :

注意:cut_all=False是精准模式,也是其默认的类型。

3.3cut_for_search()、lcut_for_search()

cut_for_search(sentence, HMM=True)和lcut_for_search(sentence, HMM=True)和上面所讲的类似。其都是对搜索引擎进行更精细的细分,即采用搜索引擎模式。

import jieba sentence = 'python是世界上最好的编程语言' ls3 = jieba.cut_for_search(sentence) print(ls3) #  print(list(ls3)) # ['python', '是', '世界', '上', '最好', '的', '编程', '语言', '编程语言'] ls4 = jieba.lcut_for_search(sentence) print(ls4) # ['python', '是', '世界', '上', '最好', '的', '编程', '语言', '编程语言']

3.4add_word(self, word, freq=None, tag=None)

Add a word to dictionary. freq and tag can be omitted, freq defaults to be a calculated value that ensures the word can be cut out.
  • 函数功能:在字典中添加一个单词。
  • 参数解析:freq 和 tag 可以省略,freq 默认是一个计算值,保证单词可以被切掉。
import jieba sentence = 'python是世界上最好的编程语言' ls2 = jieba.lcut(sentence) print(ls2) ls5 = jieba.add_word('最好的') ls6 = jieba.lcut(sentence) print(ls6)

 

结果如上,最终最好的就没有被切掉。

3.5del_word(word)

函数功能:分词词典中删除词word

import jieba sentence = 'python是世界上最好的编程语言' ls2 = jieba.lcut(sentence) print(ls2) ls7 = jieba.del_word('世界') ls8 = jieba.lcut(sentence) print(ls8)

不过经过笔者更改word,发现word是编程语言时,最后就分割成了编程和语言;当word是编程时,结果没变化;当word是python时,结果也没变化。因此有些需要笔者自己去尝试。

3.6suggest_freq(segment, tune=False)

 """ Suggest word frequency to force the characters in a word to be joined or splitted. Parameter: - segment : The segments that the word is expected to be cut into, If the word should be treated as a whole, use a str. - tune : If True, tune the word frequency. Note that HMM may affect the final result. If the result doesn't change, set HMM=False. """
  • 函数功能:建议词频,强制将单词中的字符合并或拆分。
  • 参数解析:
    •   segment :该单词预期被切割成的片段,如果该单词应该被视为一个整体,则使用str。
    •   tune : 如果为True,则调整词频。

注意:HMM可能会影响最终结果。如果结果不变,设置HMM=False。 

3.7tokenize(unicode_sentence, mode="default", HMM=True)

  """ Tokenize a sentence and yields tuples of (word, start, end) Parameter: - sentence: the str(unicode) to be segmented. - mode: "default" or "search", "search" is for finer segmentation. - HMM: whether to use the Hidden Markov Model. """
  • 函数功能:标记一个句子并产生 (word, start, end) 的元组
  • 参数解析:
    •     unicode_sentence:要分割的 str(unicode)。
    •     模式:"default" or "search", "search" is for finer segmentation.    “默认”或“搜索”,“搜索”用于更精细的分割。
    •     HMM: 是否使用隐马尔可夫模型。 

四、所需代码展示

# -*- coding: utf-8-*- import jieba sentence = 'python是世界上最好的编程语言' ls = jieba.cut(sentence, cut_all=False) # print(ls) # print(list(ls)) # #  # print(type(ls)) # #  # # ①''.join # ls_1 = ' '.join(ls) # print(ls_1) # # python 是 世界 上 最好 的 编程语言 # ②for循环遍历 # for i in ls: #     print(i) # ''' # python # 是 # 世界 # 上 # 最好 # 的 # 编程语言 # ''' # # ③列表推导式 # ls_2 = [i for i in ls] # print(ls_2) # # ['python', '是', '世界', '上', '最好', '的', '编程语言'] # ls1 = jieba.lcut(sentence, cut_all=True) # print(ls1) ls2 = jieba.lcut(sentence) print(ls2) # ls3 = jieba.cut_for_search(sentence) # print(ls3) # #  # print(list(ls3)) # # ['python', '是', '世界', '上', '最好', '的', '编程', '语言', '编程语言'] # ls4 = jieba.lcut_for_search(sentence) # print(ls4) # ['python', '是', '世界', '上', '最好', '的', '编程', '语言', '编程语言'] # ls5 = jieba.load_userdict('文案.txt') # ls6 = jieba.lcut(sentence) # print(ls6) # ls5 = jieba.add_word('最好的') # ls6 = jieba.lcut(sentence) # print(ls6) ls7 = jieba.del_word('世界') ls8 = jieba.lcut(sentence) print(ls8)

需要的可以自行复制

五、总结

  • ①全国计算机等级考试二级python语言程序设计中涉及到的内容一般只是分词模式、lcut()、lcut_for_search()和add_word()这几方面知识;
  • ②笔者所写的不是特别详细,要是之后有好的案例或者其他方式,会进行添加以及完善3.6,3.7的内容;
  • ③该模块的理解与使用不是特别难,希望大家自己动手试试,找几个案例,敲敲代码!!

到此这篇关于python中jieba模块的深入了解的文章就介绍到这了,更多相关python jieba 内容请搜索0133技术站以前的文章或继续浏览下面的相关文章希望大家以后多多支持0133技术站!

以上就是python中jieba模块的深入了解的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » python