jieba中文处理

分词

 1 import jieba
 2 
 3 seg_list=jieba.cut("我在学习自然语言处理",cut_all=True)
 4 print("/ ".join(seg_list)) #全模式
 5 #我/ 在/ 学习/ 自然/ 自然语言/ 语言/ 处理
 6 
 7 seg_list=jieba.cut("我在学习自然语言处理",cut_all=False)
 8 print("/ ".join(seg_list)) #精确模式 默认是精确模式
 9 #我/ 在/ 学习/ 自然语言/ 处理
10 
11 seg_list=jieba.cut_for_search("我在学习自然语言处理")
12 print("/ ".join(seg_list)) #搜索引擎模式
13 #我/ 在/ 学习/ 自然/ 语言/ 自然语言/ 处理
14 
15 print(" ".join(jieba.lcut_for_search("我在学习自然语言处理"))) #lcut_for_search直接返回List
View Code
添加用户自定义词典
#添加用户自定义词典
print('/'.join(jieba.cut('如果放到旧字典中将出错',HMM=False))) #如果/放到/旧/字典/中将/出错
jieba.suggest_freq(('',''),True)
print('/'.join(jieba.cut('如果放到旧字典中将出错',HMM=False))) #如果/放到/旧/字典/中/将/出错
View Code
关键词提取
  基于TF-IDF算法的关键词抽取
import jieba
import jieba.analyse as analyse
#关键词提取
lines=open('NBA.txt',encoding='utf-8').read()
#topK 返回权重最大的关键词,默认值为20
#withWeight 是否返回权重值
#allowPOS仅仅包括指定词性的词,默认值为空,即不筛选
print("  ".join(analyse.extract_tags(lines,topK=20,withWeight=False,allowPOS=())))
View Code

  基于TextRank算法的关键词抽取

    基本思想:  

      将待抽取关键词的文本进行分词

      以固定窗口大小(默认为5),词之间的共现关系,构建图

      计算图中节点的PageRank,注意是无向带权图

import jieba
import jieba.analyse as analyse
#关键词提取
lines=open('NBA.txt',encoding='utf-8').read()
print("  ".join(analyse.textrank(lines,topK=20,withWeight=False,allowPOS=('ns','n','vn','v'))))
'''
全明星赛  勇士  正赛  指导  对方  投篮  球员  没有  出现  时间  威少  认为  看来  结果  相隔  助攻  现场  三连庄  介绍  嘉宾
'''
View Code

词性标注

import jieba.posseg as pseg
words=pseg.cut("我爱自然语言处理")
for word,flag in words:
    print('%s %s' % (word,flag))
'''
我 r
爱 v
自然语言 l
处理 v
'''
View Code

并行分词

jieba.enable_parallel()

Tokenize:返回词语在原文的起止位置

 1 import jieba
 2 result=jieba.tokenize('自然语言处理非常有用') #默认模式
 3 for tk in result:
 4     print("%s\t\t start:%d \t\t end:%d" % (tk[0],tk[1],tk[2]))
 5 '''
 6 自然语言         start:0          end:4
 7 处理         start:4          end:6
 8 非常         start:6          end:8
 9 有用         start:8          end:10
10 '''
11 print()
12 result=jieba.tokenize('自然语言处理非常有用',mode='search') #搜索模式
13 for tk in result:
14     print("%s\t\t start:%d \t\t end:%d" % (tk[0],tk[1],tk[2]))
15 '''
16 自然         start:0          end:2
17 语言         start:2          end:4
18 自然语言         start:0          end:4
19 处理         start:4          end:6
20 非常         start:6          end:8
21 有用         start:8          end:10
22 '''
View Code

搜索引擎ChineseAnalyzer for Whoosh

 1 from __future__ import unicode_literals
 2 import sys,os
 3 sys.path.append("../")
 4 from whoosh.index import create_in,open_dir
 5 from whoosh.fields import *
 6 from whoosh.qparser import QueryParser
 7 import jieba.analyse
 8 analyzer=jieba.analyse.ChineseAnalyzer()
 9 schema=Schema(title=TEXT(stored=True),path=ID(stored=True),content=TEXT(stored=True,analyzer=analyzer))
10 
11 if not os.path.exists("tmp"):
12     os.mkdir("tmp")
13 
14 ix=create_in("tmp",schema)
15 writer=ix.writer()
16 
17 writer.add_document(
18     title="document1",
19     path="/a",
20     content="This is the first document we've added!"
21 )
22 
23 writer.add_document(
24     title="document2",
25     path="/b",
26     content="The second f中文t is cenve more insterstring吃水果!"
27 )
28 
29 writer.add_document(
30     title="document3",
31     path="/c",
32     content="天气很好不错是这样的!"
33 )
34 writer.add_document(
35     title="document4",
36     path="/c",
37     content="搜索引擎词性标注"
38 )
39 writer.add_document(
40     title="document4",
41     path="/c",
42     content="咱两交换一下把"
43 )
44 
45 writer.commit()
46 searcher=ix.searcher()
47 parser=QueryParser("content",schema=ix.schema)
48 
49 q=parser.parse("搜索")
50 result=searcher.search(q)
51 for hit in result:
52     print(hit.highlights("content"))
53 
54 '''
55 <b class="match term0">搜索</b>引擎词性标注
56 '''
View Code

 

命令行分词:python -m jieba news.txt > cut_result.txt



 
posted @ 2018-03-06 10:26  88aa123  阅读(93)  评论(0)    收藏  举报