使用python進行文本預(yù)處理和提取特征的實例
更新時間:2018年06月05日 15:14:25 作者:Johline
今天小編就為大家分享一篇使用python進行文本預(yù)處理和提取特征的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
如下所示:
<strong><span style="font-size:14px;">文本過濾</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)#只保留中文和標點和數(shù)字 result = re.sub(r'[^\u4e00-\u9fa5,A-Za-z0-9]', "",content)#只保留中文、英文和數(shù)字
文本去除兩個以上空格
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())#只保留中文和標點和數(shù)字 return result
文本去停用詞
def text_to_wordlist(text):
result = re.sub(r'[^\u4e00-\u9fa5]', "",text)
f1_seg_list = jieba.cut(result)#需要添加一個詞典,來彌補結(jié)巴分詞中沒有的詞語,從而保證更高的正確率
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=())#關(guān)鍵詞提取,在這里對jieba的tfidf.py進行了修改
return keyWord
以上這篇使用python進行文本預(yù)處理和提取特征的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
舉例講解Python設(shè)計模式編程中的訪問者與觀察者模式
這篇文章主要介紹了Python設(shè)計模式編程中的訪問者與觀察者模式,設(shè)計模式的制定有利于團隊協(xié)作編程代碼的協(xié)調(diào),需要的朋友可以參考下2016-01-01
Python生成器實現(xiàn)簡單"生產(chǎn)者消費者"模型代碼實例
這篇文章主要介紹了Python生成器實現(xiàn)簡單"生產(chǎn)者消費者"模型代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-03-03
Python GUI編程之tkinter 關(guān)于 ttkbootstrap 的使用
ttkbootstrap 是一個基于 tkinter 的界面美化庫,使用這個工具可以開發(fā)出類似前端 bootstrap 風(fēng)格的 tkinter 桌面程序,這篇文章主要介紹了Python GUI編程之tkinter 關(guān)于 ttkbootstrap 的使用詳解,需要的朋友可以參考下2022-03-03
解決PyCharm不在run輸出運行結(jié)果而不是再Console里輸出的問題
這篇文章主要介紹了解決PyCharm不在run輸出運行結(jié)果而不是再Console里輸出的問題,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
Tensorflow 實現(xiàn)修改張量特定元素的值方法
今天小編就為大家分享一篇Tensorflow 實現(xiàn)修改張量特定元素的值方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07

