NLP - jieba (1)

1. Basic functions and word usage

Each word (Unicode) and after jieba.cut structure is returned jieba.cut_for_search an iterative generator, may be used to obtain word for loop obtained

jieba.cut method takes three input parameters:

  • Required word string
  • cut_all parameters used to control whether to use the full mode
  • HMM parameters are used to control whether the HMM model

The method takes two parameters jieba.cut_for_search

  • Required word string
  • Whether to use HMM models.
    This method is suitable for a search engine to build an inverted index word, relatively small particle size
import jieba

seg_list=jieba.cut("我在学习自然语言处理",cut_all=True)
print(seg_list)
print("Full Mode:"+"/".join(seg_list)) #全模式
seg_list1=jieba.cut('我在学习自然语言处理',cut_all=False)
print("Default Mode:"+"/".join(seg_list1)) #精确模式
seg_list2 = jieba.cut("他毕业于上海交通大学,在百度深度学习研究院进行研究")  # 默认是精确模式
print(", ".join(seg_list2))
seg_list = jieba.cut_for_search("小明硕士毕业于中国科学院计算所,后在哈佛大学深造")  # 搜索引擎模式
print(", ".join(seg_list))

Full Mode:// 学习/ 自然/ 自然语言/ 语言/ 处理
Default Mode:// 学习/ 自然语言/ 处理
他, 毕业,, 上海交通大学,,, 百度, 深度, 学习, 研究院, 进行, 研究
小明, 硕士, 毕业,, 中国, 科学, 学院, 科学院, 中国科学院, 计算, 计算所,,,, 哈佛, 大学, 哈佛大学, 深造

jieba.lcut and jieba.lcut_for_search direct return list

result_lcut = jieba.lcut("小明硕士毕业于中国科学院计算所,后在哈佛大学深造")
print(result_lcut)
print(" ".join(result_lcut))
print( " ".join(jieba.lcut_for_search("小明硕士毕业于中国科学院计算所,后在哈佛大学深造")))

[u'\u5c0f\u660e', u'\u7855\u58eb', u'\u6bd5\u4e1a', u'\u4e8e', u'\u4e2d\u56fd\u79d1\u5b66\u9662', u'\u8ba1\u7b97\u6240', u'\uff0c', u'\u540e', u'\u5728', u'\u54c8\u4f5b\u5927\u5b66', u'\u6df1\u9020']
小明 硕士 毕业 于 中国科学院 计算所 , 后 在 哈佛大学 深造
小明 硕士 毕业 于 中国 科学 学院 科学院 中国科学院 计算 计算所 , 后 在 哈佛 大学 哈佛大学 深造

Add user-defined dictionary

Many times we need to be word for their own scene, there will be some technical terms in the field.

  • User dictionary can be loaded with jieba.load_userdict (file_name)

  • Some words themselves can manually add the following method:

    • With add_word (word, freq = None, tag = None) and del_word (word) dynamically modify the dictionary in the program
    • With suggest_freq (segment, tune = True) individual words in the word frequency may be adjusted, so that it can (or can not) be divided out
print('/'.join(jieba.cut('如果放到旧字典中将出错。', HMM=False)))
如果/放到//字典/中将/出错/。
jieba.suggest_freq(('中', '将'), True)
494
print('/'.join(jieba.cut('如果放到旧字典中将出错。', HMM=False)))
如果/放到//字典///出错/

Guess you like

Origin blog.csdn.net/lgy54321/article/details/90669712