Use jieba library foundation library of Python (third-party Chinese vocabulary libraries)

You learn python friends, whether have encountered such a problem, for example as follows:

I am proud of my motherland

What happens if we need to extract the middle of the word to go do?

Nature is calling the string split () function to

Then converts these into Chinese yet, "I am proud of my country" re-word what will happen?

China is not like the English vocabulary as text and can be distinguished by a space or punctuation,

This would lead to such "proud" to open the "arrogance", "proud", or the "to", "my" combined into "my", etc.

That how to avoid these problems? It uses python library foundation library --jieba today introduced

First, what is jieba library?

jieba library is an excellent word of Chinese third-party libraries, it can use a Chinese dictionary to determine the probability of association between the characters ,

The probability of large Chinese characters between the composition of the phrase, word formation result, theChinese text to get a single word by word.

jieba word of three modes : fine mode, full mode, search engine mode

- exact model: separating text precise cut, there is no redundancy word

- Full Mode: all possible words in the text are scanned, redundant

- Search engine mode: the precise mode on the basis of long-term re-segmentation

Second, the installation jieba library

Jieba library installation is relatively simple, I introduce a few simple methods

1. Automatic installation

Open cmd command prompt, then enter the code

easy_install jieba

pip install jieba

pip3 install jieba

Any one of three sections of code to automatically download and install

 

2. Semi-automatic installation

First open jieba library website: http: //pypi.python.org/pypi/jieba/

然后下载并运行python setup.py install

最后将 jieba 目录放置于当前目录或者 site-packages 目录

 

 

 3.软件安装

许多编辑软件都可以在软件内部安装,以pycharm2019为例子

首先打开pycharm,在左上角文件中可以找到设置,然后打开设置

 

 

 右侧项目相关可以找到 project interpreter,进入可以查看项目引用的模块

点击右侧的加号,在available packages 中搜索jieba  选中后点击左下角安装即可

 

 

 4.检测安装

打开命令提示符(cmd)进入python环境

输入import jieba 如下图所示即为安装成功

 

 

 

三、主要函数

jieba.cut(s)        

被运用于精确模式,将会返回一个可迭代的数据类型

jieba.cut(s,cut_all=True) 

被运用于全模式,输出文本s中的所有可能单词

jieba.cut_for_search(s)

搜索引擎模式,适合搜索引擎建立索引的分词结果

jieba.lcut(s)

被运用于精确模式,将会返回一个列表类型

jieba.lcut(s,cut_all=True)

被运用于全模式,返回一个列表类型

jieba.lcut_for_search(s)

搜索引擎模式,返回一个列表类型

jieba.add_word(w)

向分词词典加入新词

 

相信不少同学已经看得有点蒙,那么接下来我将通过代码来实际对比不同点

首先我们对比三个不同的模式 ,之前的介绍可以看出:

精确模式将不会出现冗余,所有词汇都是根据最大可能性而进行组合的出结果

全模式与精确模式最大区别在于,全模式将会把所有可能拼接的词汇全部展现

搜索引擎模式则是在精确模式的前提下对较长词汇进行再一次分割

 

我们以“我因自己是中华人民共和国的一份子而感到骄傲”为例

*精确模式结果如下 :

 

 

 ['我', '因', '自己', '是', '中华人民共和国', '的', '一份', '子', '而', '感到', '骄傲']

 *全模式结果如下:

 

 

 ['我', '因', '自己', '是', '中华', '中华人民', '中华人民共和国', '华人', '人民', '人民共和国', '共和', '共和国', '的', '一份', '份子', '而', '感到', '骄傲']

 

 *搜索引擎模式结果如下:

 

 ['我', '因', '自己', '是', '中华', '华人', '人民', '共和', '共和国', '中华人民共和国', '的', '一份', '子', '而', '感到', '骄傲']

可以很明显的对比出来,全模式将所有课能出现的词汇进行罗列,而搜索引擎模式与精确模式十分相似,但对“中华人名共和国“”这一词汇进行分词

 

至于有的同学发现有些函数十分相似,比如说cut()与lcut()

两者之间其实差距不大,主要不同在于返回类型,加“l”的一般返回为列表类型。

如果觉得有所帮助,还望各位大佬点赞支持谢谢

 

Guess you like

Origin www.cnblogs.com/lyy135146/p/11627709.html