中文分词jieba python 学习关键词

主要功能

  1. :分词

  • jieba.cut 方法接受三个输入参数: 需要分词的字符串;cut_all 参数用来控制是否采用全模式;HMM 参数用来控制是否使用 HMM 模型
  • jieba.cut_for_search 方法接受两个参数:需要分词的字符串;是否使用 HMM 模型。该方法适合用于搜索引擎构建倒排索引的分词,粒度比较细
  • 注意:待分词的字符串可以是 GBK 字符串、UTF-8 字符串或者 unicode
  • jieba.cut 以及 jieba.cut_for_search 返回的结构都是一个可迭代的 generator,可以使用 for 循环来获得分词后得到的每一个词语(unicode),也可以用 list(jieba.cut(...)) 转化为 list

代码示例( 分词 )

#encoding=utf-8
import jieba

seg_list = jieba.cut("我来到北京清华大学", cut_all=True)
print("Full Mode:", "/ ".join(seg_list))  # 全模式

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

seg_list = jieba.cut("他来到了网易杭研大厦")  # 默认是精确模式
print(", ".join(seg_list))

seg_list = jieba.cut_for_search("小明硕士毕业于中国科学院计算所,后在日本京都大学深造")  # 搜索引擎模式
print(", ".join(seg_list))

输出:

【全模式】: 我/ 来到/ 北京/ 清华/ 清华大学/ 华大/ 大学

【精确模式】: 我/ 来到/ 北京/ 清华大学

【新词识别】:他, 来到, 了, 网易, 杭研, 大厦    (此处,“杭研”并没有在词典中,但是也被Viterbi算法识别出来了)

【搜索引擎模式】: 小明, 硕士, 毕业, 于, 中国, 科学, 学院, 科学院, 中国科学院, 计算, 计算所, 后, 在, 日本, 京都, 大学, 日本京都大学, 深造


功能 2) :添加自定义词典

开发者可以指定自己自定义的词典,以便包含jieba词库里没有的词。虽然jieba有新词识别能力,但是自行添加新词可以保证更高的正确率 
用法: 

jieba.load_userdict(file_name) # file_name为自定义词典的路径 


词典格式和dict.txt一样,一个词占一行;每一行分三部分,一部分为词语,另一部分为词频,最后为词性(可省略),用空格隔开 

一定要保存为"utf-8"格式的TXT文件!

范例: 
自定义词典: 

云计算 5 

import jieba

jieba.load_userdict("userdict.txt")

jieba.load_userdict("C:\\Python36\\Lib\\site-packages\\jieba\\custom.txt")

# 如果想单独使用自己定义的词典,使用jieba.set_dictionary("D:\\Python27\\Lib\\site-packages\\jieba\\custom.txt"),这里在custom.txt中加了丽江古城

功能 4) : 词性标注

标注句子分词后每个词的词性,采用和ictclas兼容的标记法 
用法示例

>>> import jieba.posseg as pseg
>>> words = pseg.cut("我爱北京天安门")
>>> for w in words:
...    print w.word, w.flag
...
  • 1
  • 2
  • 3
  • 4
  • 5

我 r 
爱 v 
北京 ns 
天安门 ns

# 动态增加和删除词典:

# encoding=utf-8
import sys
sys.path.append("../")
import jieba
s = "我喜欢看最强大脑"
jieba.add_word("最强大脑", freq = 20000, tag = None)
# jieba.del_word("最强")
cut = jieba.cut(s)

print (','.join(cut))


#关键词提取所使用停用词

#encoding=utf-8
import jieba
import jieba.analyse



jieba.analyse.set_stop_words("C:\\Python36\\Lib\\site-packages\\jieba\\stop_words.txt")
seg_list = jieba.cut("我来到北京清华大学", cut_all=False)
print("Default Mode:", "/ ".join(seg_list))  # 精确模式


#encoding=utf-8
import jieba.analyse
import jieba.posseg as pseg
import time


jieba.analyse.set_stop_words("C:\\Python36\\Lib\\site-packages\\jieba\\stop_words.txt")

#要分析的文本,注意编码

filename='1.txt'




def file_jieba_wordcout(filename):
    file=open(filename,'r').read()
    file=jieba.cut(file)
    dict={}
    for word in file:
        if word in dict:
            dict[word]+=1
        else:
            dict[word]=1
    file.close()
    return dict




def print_top100(filename):
    words=file_jieba_wordcout(filename)
    dict1=sorted(words.items(),key=lambda item:item[1], reverse = True)
    for item in dict1[:100]:
        print(item[0],item[1])




# wordcout 前100 次
# print_top100(filename)




#基于 TF-IDF 算法的关键词抽取
TFIDF_result=jieba.analyse.extract_tags(open(filename,'rU').read(), topK=100, withWeight=False, allowPOS=())
print(TFIDF_result)


# 基于 TextRank 算法的关键词抽取
# TextRank_result=jieba.analyse.textrank(open(filename,'rU').read(), topK=100, withWeight=False, allowPOS=('ns', 'n', 'vn', 'v'))
# print(TextRank_result)
#词性标注

猜你喜欢

转载自blog.csdn.net/pySVN8A/article/details/81015658