≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 网络编程 > Python

python 中英文分词实现方法

人气:844 时间:2018-10-26

这篇文章主要为大家详细介绍了python 中英文分词实现方法,具有一定的参考价值,可以用来参考一下。

对python这个高级语言感兴趣的小伙伴,下面一起跟随四海网的小编两巴掌来看看吧!

说到分词大家肯定一般认为是很高深的技术,但是今天作者用短短几十行代码就搞定了,感叹python很强大啊!作者也很强大。不过这个只是正向最大匹配,没有机器学习能力

注意:使用前先要下载搜狗词库
 

# @param 用几十行代码实现python中英文分词
# @author 四海网|q1010.com 

# -*- coding:utf-8 -*-
  
#写了一个简单的支持中文的正向最大匹配的机械分词,其它不用解释了,就几十行代码
#附:搜狗词库下载地址:http://vdisk.weibo.com/s/7RlE5
  
import string
__dict = {} 
  
def load_dict(dict_file='words.dic'):
    #加载词库,把词库加载成一个key为首字符,value为相关词的列表的字典
  
    words = [unicode(line, 'utf-8').split() for line in open(dict_file)]
  
    for word_len, word in words:
        first_char = word[0]
        __dict.setdefault(first_char, [])
        __dict[first_char].append(word)
     
    #按词的长度倒序排列
    for first_char, words in __dict.items():
        __dict[first_char] = sorted(words, key=lambda x:len(x), reverse=True)
  
def __match_ascii(i, input):
    #返回连续的英文字母,数字,符号
    result = ''
    for i in range(i, len(input)):
        if not input[i] in string.ascii_letters: break
        result += input[i]
    return result
  
  
def __match_word(first_char, i , input):
    #根据当前位置进行分词,ascii的直接读取连续字符,中文的读取词库
  
    if not __dict.has_key(first_char): 
        if first_char in string.ascii_letters:
            return __match_ascii(i, input)
        return first_char
  
    words = __dict[first_char]
    for word in words:
        if input[i:i+len(word)] == word:
            return word
  
    return first_char
  
def tokenize(input):
    #对input进行分词,input必须是uncode编码
  
    if not input: return []
  
    tokens = []
    i = 0
    while i < len(input):
        first_char = input[i] 
        matched_word = __match_word(first_char, i, input)
        tokens.append(matched_word)
        i += len(matched_word)
  
    return tokens
  
  
if __name__ == '__main__':
    def get_test_text():
        import urllib2
        url = "http://news.baidu.com/n?cmd=4&class=rolling&pn=1&from=tab&sub=0"
        text = urllib2.urlopen(url).read()
        return unicode(text, 'gbk')
  
    def load_dict_test():
        load_dict()
        for first_char, words in __dict.items():
            print '%s:%s' % (first_char, ' '.join(words))
  
    def tokenize_test(text):
        load_dict()
        tokens = tokenize(text)
        for token in tokens:
            print token
  
    tokenize_test(unicode(u'美丽的花园里有各种各样的小动物'))
    tokenize_test(get_test_text())

# End www_512pic_com
我也学习啦~~~

本文来自:http://www.q1010.com/181/2377-0.html

注:关于python 中英文分词实现方法的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:分词

您可能感兴趣的文章

  • Python中文分词的简单示例
  • python汉语分词的简单示例
上一篇:python 通过thrift操作hbase的完整代码
下一篇:python smtplib使用注意
热门文章
  • Python 处理Cookie的菜鸟教程(一)Cookie库
  • python之pandas取dataframe特定行列的简单示例
  • Python解决json.dumps错误::‘utf8’ codec can‘t decode byte
  • Python通过pythony连接Hive执行Hql的脚本
  • Python 三种方法删除列表中重复元素的简单示例
  • python爬虫代码示例
  • Python 中英文标点转换示例
  • Python 不得不知的开源项目解析
  • Python urlencode编码和url拼接实现方法
  • python按中文拆分中英文混合字符串的简单示例
  • 最新文章
    • Python利用numpy三层神经网络的简单示例
    • pygame可视化幸运大转盘的简单示例
    • Python爬虫之爬取二手房信息的简单示例
    • Python之time库的简单示例
    • OpenCV灰度、高斯模糊、边缘检测的简单示例
    • Python安装Bs4及使用的简单示例
    • django自定义manage.py管理命令的简单示例
    • Python之matplotlib 向任意位置添加一个子图(axes)的简单示例
    • Python图像标签标注软件labelme分析的简单示例
    • python调用摄像头并拍照发邮箱的简单示例

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。