Home > Backend Development > Python Tutorial > Python smallseg分词用法实例分析

Python smallseg分词用法实例分析

WBOY
Release: 2016-06-06 11:17:36
Original
1518 people have browsed it

本文实例讲述了Python smallseg分词用法。分享给大家供大家参考。具体分析如下:

#encoding=utf-8 
#import psyco 
#psyco.full() 
words = [x.rstrip() for x in open("main.dic",mode='r',encoding='utf-8') ] 
from smallseg import SEG 
seg = SEG() 
print('Load dict...') 
seg.set(words) 
print("Dict is OK.") 
def cuttest(text): 
  wlist = seg.cut(text) 
  wlist.reverse() 
  tmp = " ".join(wlist) 
  print(tmp) 
  print("================================") 
if __name__=="__main__": 
  cuttest("这是一个伸手不见五指的黑夜。我叫孙悟空,我爱北京,我爱Python和C++。") 
  cuttest("我不喜欢日本和服。") 
  cuttest("雷猴回归人间。") 
  cuttest("工信处女干事每月经过下属科室都要亲口交代24口交换机等技术性器件的安装工作") 
  cuttest("我需要廉租房") 
  cuttest("永和服装饰品有限公司") 
  cuttest("我爱北京天安门") 
  cuttest("abc") 
  cuttest("隐马尔可夫") 
  cuttest("雷猴是个好网站") 
  cuttest("“Microsoft”一词由“MICROcomputer(微型计算机)”和“SOFTware(软件)”两部分组成") 
  cuttest("草泥马和欺实马是今年的流行词汇") 
  cuttest("伊藤洋华堂总府店") 
  cuttest("中国科学院计算技术研究所") 
  cuttest("罗密欧与朱丽叶") 
  cuttest("我购买了道具和服装")

Copy after login

smallseg分词,在python3.3上运行稍微有些问题。py代码xrange在3.*中已经改名字为range了。另外,3.*中也没有decode函数了。

修改了上面的两个地方,代码就可移植性了。效果还可以。

希望本文所述对大家的Python程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template