python文本處理的方案(結(jié)巴分詞并去除符號(hào))
更新時(shí)間:2021年05月26日 11:03:58 作者:依我去
這篇文章主要介紹了python文本處理的方案(結(jié)巴分詞并去除符號(hào)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
看代碼吧~
import re import jieba.analyse import codecs import pandas as pd def simplification_text(xianbingshi): """提取文本""" xianbingshi_simplification = [] with codecs.open(xianbingshi,'r','utf8') as f: for line in f : line = line.strip() line_write = re.findall('(?<=\<b\>).*?(?=\<e\>)',line) for line in line_write: xianbingshi_simplification.append(line) with codecs.open(r'C:\Users\Administrator.SC-201812211013\PycharmProjects\untitled29\yiwoqu\code\xianbingshi_write.txt','w','utf8') as f: for line in xianbingshi_simplification: f.write(line + '\n') def jieba_text(): """""" word_list = [] data = open(r"C:\Users\Administrator.SC-201812211013\PycharmProjects\untitled29\xianbingshi_write.txt", encoding='utf-8').read() seg_list = jieba.cut(data, cut_all=False) # 精確模式 for i in seg_list: word_list.append(i.strip()) data_quchong = pd.DataFrame({'a':word_list}) data_quchong.drop_duplicates(subset=['a'],keep='first',inplace=True) word_list = data_quchong['a'].tolist() with codecs.open('word.txt','w','utf8')as w: for line in word_list: w.write(line + '\n') def word_messy(word): """詞語(yǔ)提煉""" word_sub_list = [] with codecs.open(word,'r','utf8') as f: for line in f: line_sub = re.sub("^[1-9]\d*\.\d*|^[A-Za-z0-9]+$|^[0-9]*$|^(-?\d+)(\.\d+)?$|^[A-Za-z0-9]{4,40}.*?",'',line) word_sub_list.append(line_sub) word_sub_list.sort() with codecs.open('word.txt','w','utf8')as w: for line in word_sub_list: w.write(line.strip("\n") + '\n') if __name__ == '__main__': xianbingshi = r'C:\Users\Administrator.SC-201812211013\PycharmProjects\untitled29\yiwoqu\xianbingshi_sub_sen_all(1).txt' # simplification_text(xianbingshi) # word = r'C:\Users\Administrator.SC-201812211013\PycharmProjects\untitled29\word.txt' simplification_text(xianbingshi)
補(bǔ)充:python 進(jìn)行結(jié)巴分詞 并且用re去掉符號(hào)
看代碼吧~
# 把停用詞做成字典 stopwords = {} fstop = open('stop_words.txt', 'r',encoding='utf-8',errors='ingnore') for eachWord in fstop: stopwords[eachWord.strip()] = eachWord.strip() #停用詞典 fstop.close() f1=open('all.txt','r',encoding='utf-8',errors='ignore') f2=open('allutf11.txt','w',encoding='utf-8') line=f1.readline() while line: line = line.strip() #去前后的空格 line = re.sub(r"[0-9\s+\.\!\/_,$%^*()?;;:-【】+\"\']+|[+——!,;:。?、~@#¥%……&*()]+", " ", line) #去標(biāo)點(diǎn)符號(hào) seg_list=jieba.cut(line,cut_all=False) #結(jié)巴分詞 outStr="" for word in seg_list: if word not in stopwords: outStr+=word outStr+=" " f2.write(outStr) line=f1.readline() f1.close() f2.close()
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python連接es筆記之創(chuàng)建和刪除操作示例詳解
這篇文章主要為大家介紹了Python連接es筆記之創(chuàng)建和刪除操作示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05python+opencv實(shí)現(xiàn)視頻抽幀示例代碼
下面是采用以幀數(shù)為間隔的方法進(jìn)行視頻抽幀,為了避免不符合項(xiàng)目要求的數(shù)據(jù)增強(qiáng),博主要求技術(shù)人員在錄制視頻時(shí)最大程度地讓攝像頭進(jìn)行移動(dòng)、旋轉(zhuǎn)以及遠(yuǎn)近調(diào)節(jié)等,對(duì)python opencv視頻抽幀示例代碼感興趣的朋友一起看看吧2021-06-06python使用yield壓平嵌套字典的超簡(jiǎn)單方法
這篇文章主要給大家介紹了關(guān)于python使用yield壓平嵌套字典的超簡(jiǎn)單方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11TFRecord格式存儲(chǔ)數(shù)據(jù)與隊(duì)列讀取實(shí)例
今天小編就為大家分享一篇TFRecord格式存儲(chǔ)數(shù)據(jù)與隊(duì)列讀取實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-01-01django 實(shí)現(xiàn)電子支付功能的示例代碼
這篇文章主要介紹了django 實(shí)現(xiàn)電子支付功能的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07python實(shí)現(xiàn)Pyecharts實(shí)現(xiàn)動(dòng)態(tài)地圖(Map、Geo)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)Pyecharts實(shí)現(xiàn)動(dòng)態(tài)地圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03