pymmseg 安装方法以及乱码解决
pymmseg-cpp is a Python port of the rmmseg-cpp project. rmmseg-cpp is a MMSEG Chinese word segmenting algorithm implemented in C++ with a Ruby interface.
下载地址:http://code.google.com/p/pymmseg-cpp/
windows下用户可以下载 pymmseg-cpp-win32-1.0.1.tar.gz,安装方法如下:
1.将压缩包解压
2.安装vs2008,使用VS2008的命令行窗口来编译程序,位置在 工具/visual studio 2008 command prompt
从这个命令行窗口进入pymmseg/mmseg-cpp文件夹。输入python build.py,回车
编写程序如下
#coding:UTF-8 from pymmseg import mmseg mmseg.dict_load_defaults() text = '今天我真的好开心' algor = mmseg.Algorithm(text) for tok in algor: print '%s [%d..%d]'%(tok.text,tok.start,tok.end)
运行后会出现乱码,这是因为mmseg支持的是utf8, windows的本地默认编码是cp936,也就是gbk编码
将代码改写如下即可
# coding: UTF-8 from pymmseg import mmseg mmseg.dict_load_defaults() text = '今天我真的好开心' algor = mmseg.Algorithm(text) for tok in algor: print '%s [%d..%d]' % (tok.text.decode('UTF-8').encode('GBK'), tok.start, tok.end)