小編給大家分享一下怎么使用python進行文本預處理和提取特征,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
Python主要應用于:1、Web開發;2、數據科學研究;3、網絡爬蟲;4、嵌入式應用開發;5、游戲開發;6、桌面應用開發。
如下所示:
<strong><span >文本過濾</span></strong>
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 re def 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 keyList def 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
看完了這篇文章,相信你對“怎么使用python進行文本預處理和提取特征”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。