背景

在许多时候为了更好的解析文本,我们不仅仅需要将文本分词,去停这么简单,除了获取关键词与新词汇以外,我们还需要对获取每个粒度的其他信息,比如词性标注,在python中NLPIR就可以很好的完成这个任务,如果你没有NLPIR那么你可以参考这篇文章NLPIR快速搭建,或者直接下载我已经准备好的汉语自然语言处理文件包NLP源码集合

代码,亦是我的笔记

# - * - coding: utf - 8 -*-
#
# 作者:田丰(FontTian)
# 创建时间:'2017/7/3'
# 邮箱:fonttian@Gmaill.com
# CSDN:http://blog.csdn.net/fontthroneimport nltk
import sys
import nlpirsys.path.append("../")reload(sys)
sys.setdefaultencoding('utf-8')import jieba
from jieba import possegdef cutstrpos(txt):# 分词+词性cutstr = posseg.cut(txt)result = ""for word, flag in cutstr:result += word + "/" + flag + ' 'return resultdef cutstring(txt):# 分词cutstr = jieba.cut(txt)result = " ".join(cutstr)return result# 读取文件
txtfileobject = open('txt/nltest1.txt')
textstr = ""
try:filestr = txtfileobject.read()
finally:txtfileobject.close()# 使用NLPIR2016 进行分词
def ChineseWordsSegmentationByNLPIR2016(text):txt = nlpir.seg(text)seg_list = []for t in txt:seg_list.append(t[0].encode('utf-8'))return seg_liststopwords_path = 'stopwords\stopwords1893.txt'  # 停用词词表# 去除停用词
def ClearStopWordsWithListByNLPIR2016(seg_list):mywordlist = []liststr = "/ ".join(seg_list)f_stop = open(stopwords_path)try:f_stop_text = f_stop.read()f_stop_text = unicode(f_stop_text, 'utf-8')finally:f_stop.close()f_stop_seg_list = f_stop_text.split('\n')for myword in liststr.split('/'):if not (myword.strip() in f_stop_seg_list) and len(myword.strip()) > 1:mywordlist.append(myword)return ''.join(mywordlist)# print filestr
filestr2 = ClearStopWordsWithListByNLPIR2016(ChineseWordsSegmentationByNLPIR2016(filestr)).replace(' ', '')# 中文分词并标注词性
posstr = cutstrpos(filestr2)print '**** show is end ****'print ' '
print 'This is posster'
print posstrstrtag = [nltk.tag.str2tuple(word) for word in posstr.split()]
# for item in strtag:
#     print item
strsBySeg = nlpir.seg(filestr)
strsBySeg2 = nlpir.seg(filestr2)
strsByParagraphProcess = nlpir.ParagraphProcess(filestr, 1)
strsByParagraphProcessA = nlpir.ParagraphProcessA(filestr, ChineseWordsSegmentationByNLPIR2016(filestr)[0], 1)print ' '
print ' '
print '**** strtag ****'for word, tag in strtag:print word, "/", tag, "|",print ' '
print ' '
print '**** strsBySeg ****'
for word, tag in strsBySeg:print word, "/", tag, "|",print ' '
print ' '
print '**** strsBySeg2 ****'
for word, tag in strsBySeg2:print word, "/", tag, "|",print ' '
print ' '
print '**** strsByParagraphProcess ****'
print strsByParagraphProcess# print ' '
# print ' '
# print '**** strsByParagraphProcessA ****'
#
# for item in strsByParagraphProcessA:
#     print item,print ' '
print ' '
print '**** show is end ****

实用示例

NLPIR会自动对人名进行分词与标注,借助该功能我们可以获取自定义新词,或者提取与某类人有关的句子.下面是我前段时间在写一个项目demon时刚写的测试代码

# - * - coding: utf - 8 -*-
#
# 作者:田丰(FontTian)
# 创建时间:'2017/7/11'
# 邮箱:fonttian@Gmaill.com
# CSDN:http://blog.csdn.net/fontthrone
from os import path
from scipy.misc import imread
import matplotlib.pyplot as plt
import jieba
from nlpir import *
from wordcloud import WordCloud, ImageColorGenerator
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
d = path.dirname(__file__)text = '接待钟世镇院士,筹备杨东奇部长接待事宜。'
stopwords_path = 'stopwords\CNENstopwords.txt'  # 停用词词表
number = 10def ShowByItem(List):print '********* show ', str(List), ' end *********'for item in List:print item,printprint '********* show ', str(List), ' end *********'#  使用NLPIR2016 获取名字
def FindAcademicianNameByNLPIR2016(text,isAddYuanShi):txt = seg(text)seg_list = []for i in range(len(txt)):if txt[i][1] == 'nr' and txt[i+1][0] == '院士':if isAddYuanShi == 1:seg_list.append(txt[i][0].encode('utf-8')+'院士')else:seg_list.append(txt[i][0].encode('utf-8'))return seg_liststr2 = FindAcademicianNameByNLPIR2016(text,1)ShowByItem(str2)# 输出
********* show  ['\xe9\x92\x9f\xe4\xb8\x96\xe9\x95\x87\xe9\x99\xa2\xe5\xa3\xab']  end
钟世镇院士
********* show  ['\xe9\x92\x9f\xe4\xb8\x96\xe9\x95\x87\xe9\x99\xa2\xe5\xa3\xab']  end 

在demon中使用的

使用NLPIR2016 获取名字
def FindAcademicianNameByNLPIR2016(text,isAddYuanShi):txt = seg(text)seg_list = []for i in range(len(txt)):if txt[i][1] == 'nr' and txt[i+1][0] == '院士':if isAddYuanShi == 1:seg_list.append(txt[i][0].encode('utf-8')+'院士')else:seg_list.append(txt[i][0].encode('utf-8'))strAcademicianName = FindAcademicianNameByNLPIR2016(fullContent,1)
strAcademicianName = list(set(strAcademicianName))
# 利用pandas存储
dfAcademicianName = pd.DataFrame(strAcademicianName)
dfAcademicianName.columns = ['AcademicianName']
dfAcademicianName.to_csv('csv/dfAcademicianName')
# 利用Pandas 获取
dfNewWords = pd.read_csv("csv/dfNewWords")
dfAcademicianName = pd.read_csv("csv/dfAcademicianName")# 你也可以将其加入用户新词汇
# add_word(dfAcademicianName['AcademicianName'])# 提取所有带有院士的报告
def GetAcademicianCSV(df,strColumn,df1):dfAcademicianName = pd.read_csv("csv/dfAcademicianName")listAcademicianName = list(dfAcademicianName['AcademicianName'])print type(listAcademicianName)mywordlistAcademicianName =[]mywordlisttime = []mywordAca = []df1 = df1.copy()numlen = len(df1.index)for i in range(numlen):for myword in df1.loc[i, strColumn].split():if (myword in listAcademicianName) and len(myword) > 1:print mywordmywordlistAcademicianName.append(df.loc[i, strColumn])mywordAca.append(myword)mywordlisttime.append(df.loc[i, 'time'])return mywordlistAcademicianName,mywordlisttime,mywordAca# 返回的信息
mywordlistAcademicianName, mywordlisttime,mywordAca = GetAcademicianCSV(df,'content',df1)

效果如下

使用NLPIR 进行中文分词并标注词性相关推荐

  1. 自然语言处理学习8:python使用standford CoreNLP进行中文分词、标注和命名实体识别

    jieba分词可以进行中文分词和标注,但是无法进行命名实体识别. 1. 环境配置 (1) 下载安装JDK 1.8及以上版本 (2)下载Stanford CoreNLP文件,解压. (3)处理中文还需要 ...

  2. Ansj中文分词Java开发词性分类

    用ansj分词后,只提取满足提交的词性的单词. Ansj下载地址:http://maven.ansj.org/org/ansj/ansj_seg/ 辅助包NLP下载地址:http://maven.an ...

  3. Linux环境下使用NLPIR(ICTCLAS)中文分词详解

    本文作者:合肥工业大学 管理学院 钱洋 email:1563178220@qq.com 欢迎交流,禁止将本人博客直接复制下来,上传到百度文库等平台. NLPIR介绍 NLPIR是中科院出的一款汉语分词 ...

  4. python语言常用的中文分词第三方库是_基于boost使用Python调用NLPIR(ICTCLAS2013)中文分词组件...

    最近需要用到中文分词,本来想省事,用python的第三方库结巴分词,但看了下API,计算文本关键词的方法没有没有返回关键字对应的权值,翻了下文档应该是不还不支持,只好继续使用中科院的那套ICTCLAS ...

  5. 中文分词工具探析(一):ICTCLAS (NLPIR)

    [开源中文分词工具探析]系列: 开源中文分词工具探析(一):ICTCLAS (NLPIR) 开源中文分词工具探析(二):Jieba 开源中文分词工具探析(三):Ansj 开源中文分词工具探析(四):T ...

  6. Python 中文分词 NLPIR 快速搭建

    前述 本篇文章写完需要半个小时,阅读需要十分钟,读完后,你将学会在Python中使用NLPIR,以及关于使用它的一些有用的基础知识 NLPIR 是中科院的汉语分词系统,在Python中使用也比较广泛, ...

  7. 中文分词入门之字标注法3

    中文分词入门之字标注法3 http://www.52nlp.cn/%E4%B8%AD%E6%96%87%E5%88%86%E8%AF%8D%E5%85%A5%E9%97%A8%E4%B9%8B%E5% ...

  8. ictclas,ansj,结巴分词,StanfordNLP中文分词以及所用词性标注集

    NLPIR(ICTCLAS),参见java实现NLPIR(ICTCLAS)分词:http://www.bubuko.com/infodetail-665665.html,词性标注使用北大词性标注集.在 ...

  9. Jieba中文分词下如何画词云图?

    配置:anaconda3 + Pycharm 文章目录 WordCloud 关于Jieba分词 Jieba中文分词 +绘制词云图 案例 WordCloud 英文文本 导入第三方模块 from word ...

最新文章

  1. 采用三层架构(JAVA)设计学生管理系统
  2. gp的分布、分区策略(概述)
  3. 【CodeForces - 988C 】Equal Sums (思维,STLmap,STLset,tricks)
  4. 列出一个目录中所有文件及大小
  5. ViewPage最全解析
  6. Office 365 机器人(Bot)开发入门
  7. PAT 1018 锤子剪刀布
  8. 【贪心 和 DP + 卖股票】LeetCode 122. Best Time to Buy and Sell Stock II
  9. Gstreamer 搭建RTSP服务器(九)
  10. win11右键新建没有txt文本文档记事本怎么办
  11. 算法最优化(2)线性规划问题中的常见概念辨析:可行解,最优解,基,基向量,非基向量,基变量,非基变量等等
  12. html 网页公式编辑软件,LaTeX 公式编辑器网页版
  13. Twincat3 硬件: 台湾DFI工业控制器,CPU1.1GHZ,RAM512
  14. Caused by: java.lang.UnsatisfiedLinkError: Library hello-jni not found“问题解决
  15. buct寒假集训——lca
  16. vue 安装不上,报错,解决办法如下
  17. linux下阅读MHT文件
  18. 如何配置tracker?
  19. “三次握手,四次挥手”这么讲,保证你忘不了
  20. [导入]山寨也疯狂:前卫手表造型 Cool G108 Watch手机亮相

热门文章

  1. Java锁详解:“独享锁/共享锁+公平锁/非公平锁+乐观锁/悲观锁+线程锁”
  2. 一步步实施 DevOps (三)
  3. WP7进阶】——XNA游戏平面矩形碰撞检测
  4. inotify_add_watch使用注意
  5. SpringMVC 简单拦截器配置
  6. 凌晨1点突发致命生产事故!看的我惊心动魄…
  7. springboot整合flowable
  8. STL泛型算法:reverse
  9. background-attachment: fixed的用法
  10. c 多语言切换dll,【图片】老C教学之——给你的程序添加多语言支持【dll】【vb吧】_百度贴吧...