用 python 進行文本預先處理和提取特徵__python

來源:互聯網
上載者:User
文本過濾
result = re.sub(r'[^\u4e00-\u9fa5,。。。,、;:“ ”‘ ’( )《 》〈 〉]', "", content)#只保留中文和標點
result = re.sub(r'[^\u4e00-\u9fa5]', "",content)#只保留中文result = re.sub(r'[^\0-9\.\u4e00-\u9fa5,。。。,、;:“ ”‘ ’( )《 》〈 〉]', "", content)#只保留中文和標點和數字result = re.sub(r'[^\u4e00-\u9fa5,A-Za-z0-9]', "",content)#只保留中文、英文和數字

文本去除兩個以上空格

content=re.sub(r'\s{2,}', '', content)

bas4編碼變成中文

def bas4_decode(bas4_content):    decodestr= base64.b64decode(bas4_content)    result = re.sub(r'[^\0-9\.\u4e00-\u9fa5,。。。,、;:“ ”‘ ’( )《 》〈 〉]', "", decodestr.decode())#只保留中文和標點和數字    return result

文本去停用詞

def text_to_wordlist(text):    result = re.sub(r'[^\u4e00-\u9fa5]', "",text)    f1_seg_list = jieba.cut(result)#需要添加一個詞典,來彌補結巴分詞中沒有的詞語,從而保證更高的正確率    f_stop = codecs.open(".\stopword.txt","r","utf-8")    try:        f_stop_text = f_stop.read()    finally:        f_stop.close()    f_stop_seg_list = f_stop_text.split()    test_words = []    for myword in f1_seg_list:        if myword not in f_stop_seg_list:            test_words.append(myword)                return test_words


文本特徵提取

import jieba  import jieba.analyse  import numpy as np  #import json  import redef Textrank(content):    result = re.sub(r'[^\u4e00-\u9fa5]', "",content)    seg = jieba.cut(result)      jieba.analyse.set_stop_words('stopword.txt')    keyList=jieba.analyse.textrank('|'.join(seg), topK=10, withWeight=False)      return keyListdef TF_IDF(content):    result = re.sub(r'[^\u4e00-\u9fa5]', "",content)    seg = jieba.cut(result)      jieba.analyse.set_stop_words('stopword.txt')    keyWord = jieba.analyse.extract_tags(          '|'.join(seg), topK=10, withWeight=False, allowPOS=())#關鍵詞提取,在這裡對jieba的tfidf.py進行了修改       return keyWord




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.