本文共 4531 字,大约阅读时间需要 15 分钟。
github地址:https://github.com/isnowfy/snownlp
pip install snownlp
# -*- coding: utf-8 -*-from __future__ import unicode_literalsfrom . import normalfrom . import segfrom . import tagfrom . import sentimentfrom .sim import bm25from .summary import textrankfrom .summary import words_mergeclass SnowNLP(object): def __init__(self, doc): self.doc = doc self.bm25 = bm25.BM25(doc) @property def words(self): return seg.seg(self.doc) @property def sentences(self): return normal.get_sentences(self.doc) @property def han(self): return normal.zh2hans(self.doc) @property def pinyin(self): return normal.get_pinyin(self.doc) @property def sentiments(self): return sentiment.classify(self.doc) @property def tags(self): words = self.words tags = tag.tag(words) return zip(words, tags) @property def tf(self): return self.bm25.f @property def idf(self): return self.bm25.idf def sim(self, doc): return self.bm25.simall(doc) def summary(self, limit=5): doc = [] sents = self.sentences for sent in sents: words = seg.seg(sent) words = normal.filter_stop(words) doc.append(words) rank = textrank.TextRank(doc) rank.solve() ret = [] for index in rank.top_index(limit): ret.append(sents[index]) return ret def keywords(self, limit=5, merge=False): doc = [] sents = self.sentences for sent in sents: words = seg.seg(sent) words = normal.filter_stop(words) doc.append(words) rank = textrank.KeywordTextRank(doc) rank.solve() ret = [] for w in rank.top_index(limit): ret.append(w) if merge: wm = words_merge.SimpleMerge(self.doc, ret) return wm.merge() return ret
print "1_____分词结果#######"+json.dumps(s.words, encoding="UTF-8", ensure_ascii=False) # [u'这个', u'东西', u'真心',# u'很', u'赞']print "2_____关键字#######"+json.dumps(s.tags, encoding="UTF-8", ensure_ascii=False)#print s.tags # [(u'这个', u'r'), (u'东西', u'n'),# (u'真心', u'd'), (u'很', u'd'),# (u'赞', u'Vg')]print "3_____积极/消极#######"+str(s.sentiments) # 0.9769663402895832 positive的概率print "4_____文本相似度#######"+str(s.sim("你好"))print "5_____摘要#######"+json.dumps(s.summary(), encoding="UTF-8", ensure_ascii=False)print "6_____关键词#######"+' '.join(s.keywords())print "7_____繁体转简体中文#######"+s.hanprint "8_____汉语拼音#######"+json.dumps(s.pinyin, encoding="UTF-8", ensure_ascii=False)
SnowNLP是一个python写的类库,可以方便的处理中文文本内容,是受到了的启发而写的,由于现在大部分的自然语言处理库基本都是针对英文的,于是写了一个方便处理中文的类库,并且和TextBlob不同的是,这里没有用NLTK,所有的算法都是自己实现的,并且自带了一些训练好的字典。注意本程序都是处理的unicode编码,所以使用时请自行decode成unicode。
from snownlp import SnowNLPs = SnowNLP(u'这个东西真心很赞')s.words # [u'这个', u'东西', u'真心', # u'很', u'赞']s.tags # [(u'这个', u'r'), (u'东西', u'n'), # (u'真心', u'd'), (u'很', u'd'), # (u'赞', u'Vg')]s.sentiments # 0.9769663402895832 positive的概率s.pinyin # [u'zhe', u'ge', u'dong', u'xi', # u'zhen', u'xin', u'hen', u'zan']s = SnowNLP(u'「繁體字」「繁體中文」的叫法在臺灣亦很常見。')s.han # u'「繁体字」「繁体中文」的叫法 # 在台湾亦很常见。'text = u'''自然语言处理是计算机科学领域与人工智能领域中的一个重要方向。它研究能实现人与计算机之间用自然语言进行有效通信的各种理论和方法。自然语言处理是一门融语言学、计算机科学、数学于一体的科学。因此,这一领域的研究将涉及自然语言,即人们日常使用的语言,所以它与语言学的研究有着密切的联系,但又有重要的区别。自然语言处理并不是一般地研究自然语言,而在于研制能有效地实现自然语言通信的计算机系统,特别是其中的软件系统。因而它是计算机科学的一部分。'''s = SnowNLP(text)s.keywords(3) # [u'语言', u'自然', u'计算机']s.summary(3) # [u'因而它是计算机科学的一部分', # u'自然语言处理是一门融语言学、计算机科学、 # 数学于一体的科学', # u'自然语言处理是计算机科学领域与人工智能 # 领域中的一个重要方向']s.sentencess = SnowNLP([[u'这篇', u'文章'], [u'那篇', u'论文'], [u'这个']])s.tfs.idfs.sim([u'文章'])# [0.3756070762985226, 0, 0]
$ pip install snownlp
现在提供训练的包括分词,词性标注,情感分析,而且都提供了我用来训练的原始文件 以分词为例 分词在snownlp/seg
目录下
from snownlp import segseg.train('data.txt')seg.save('seg.marshal')#from snownlp import tag#tag.train('199801.txt')#tag.save('tag.marshal')#from snownlp import sentiment#sentiment.train('neg.txt', 'pos.txt')#sentiment.save('sentiment.marshal')
这样训练好的文件就存储为seg.marshal
了,之后修改snownlp/seg/__init__.py
里的data_path
指向刚训练好的文件即可
转载地址:http://gchpl.baihongyu.com/