python--中英文混合字符串的切分(中文按字斷開,英文按單詞分開,數字按空格等特殊符號斷開)

待切分句子:

s = "12、China's Legend Holdings will split its several business arms 	to go public on stock markets,  the group's president Zhu Linan said on Tuesday.該集團總裁朱利安週二表示,haha中國聯想控股將分拆其多個業務部門在股市上市,。"

切分結果:

['12', 'china', 's', 'legend', 'holdings', 'will', 'split', 'its', 'several', 'business', 'arms', 'to', 'go', 'public', 'on', 'stock', 'markets', 'the', 'group', 's', 'president', 'zhu', 'linan', 'said', 'on', 'tuesday', '該', '集', '團', '總', '裁', '朱', '利', '安', '周', '二', '表', '示', 'haha', '中', '國', '聯', '想', '控', '股', '將', '分', '拆', '其', '多', '個', '業', '務', '部', '門', '在', '股', '市', '上', '市']

代碼:

import re

def get_word_list(s1):
    # 把句子按字分開,中文按字分,英文按單詞,數字按空格
    regEx = re.compile('[\\W]*')    # 我們可以使用正則表達式來切分句子,切分的規則是除單詞,數字外的任意字符串
    res = re.compile(r"([\u4e00-\u9fa5])")    #  [\u4e00-\u9fa5]中文範圍

    p1 = regEx.split(s1.lower())
    str1_list = []
    for str in p1:
        if res.split(str) == None:
            str1_list.append(str)
        else:
            ret = res.split(str)
            for ch in ret:
                str1_list.append(ch)

    list_word1 = [w for w in str1_list if len(w.strip()) > 0]  # 去掉爲空的字符

    return  list_word1


if __name__ == '__main__':
    s = "12、China's Legend Holdings will split its several business arms to go public on stock markets,  the group's president Zhu Linan said on Tuesday.該集團總裁朱利安週二表示,haha中國聯想控股將分拆其多個業務部門在股市上市。"
    list_word1=get_word_list(s)
    print(list_word1)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章