关键字简单分词

中文分词

1、安装

pip install jieba

2、使用

import jieba

# 全模式
text = "我来到中国北京天安门"
seg_list = jieba.cut(text, cut_all=True)
print("     ".join(seg_list))   # 我     来到     中国     北京     天安     天安门

# 精确模式
seg_list = jieba.cut(text, cut_all=False)
print("     ".join(seg_list))   # 我     来到     中国     北京     天安门

# 默认是精确模式
seg_list = jieba.cut(text)
print("     ".join(seg_list))   # 我     来到     中国     北京     天安门

# 搜索引擎模式
seg_list = jieba.cut_for_search(text)
print("     ".join(seg_list))   # 我     来到     中国     北京     天安     天安门

3、其他工具

jieba、SnowNLP(MIT)、pynlpir、thulac

日语分词

windows10+python36                     https://pypi.org/project/mecab-python-windows/#files(下载安装,或者直接pip 安装)

Unix                                                 https://blog.csdn.net/willduan1/article/details/68945327(mac系统安装好像很简单哎!)

2、使用

import MeCab

mecab = MeCab.Tagger ("-Ochasen")

print(mecab.parse("MeCabを用いて文章を分割してみます。"))

3、其他工具

pip instrall janome 

英语分词

不使用工具 ,直接re和split()解决吧。。

简单综合运用

class AppNameCut:
    def __init__(self, app_name, country_id):
        self.app_name = app_name
        self.country_id = country_id
        self.rule = re.compile(r'((?=[\x21-\x7e]+)[^A-Za-z0-9])')
        self.keyword_list = []
        self.main()

    def english_(self):
        result = re.sub(self.rule, ' ', self.app_name)
        self.keyword_list = result.split(" ")
        self.keyword_list = [i for i in self.keyword_list if i]

    def chinese_(self):
        result = re.sub(self.rule, ' ', self.app_name)
        self.keyword_list = [j for i in jieba.cut(result) for j in i.split()]

    def japan_korea_(self):
        result = re.sub(self.rule, " ", self.app_name)
        # self.keyword_list = MeCab.Tagger("-Ochasen").parse(result).split()[::4][:-1]

    def main(self):
        fn = self.chinese_ if self.country_id in [1, 2, 3] else self.japan_korea_ if \
            self.country_id in [5, 6] else self.english_
        fn()
        return self.keyword_list

 

posted @ 2019-09-12 10:30  洛丶丶丶  阅读(315)  评论(0编辑  收藏  举报