python文本處理的方案(結(jié)巴分詞并去除符號)
看代碼吧~
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):
"""詞語提煉"""
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去掉符號
看代碼吧~
# 把停用詞做成字典
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)符號
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?time模塊之時(shí)間戳與結(jié)構(gòu)化時(shí)間的使用
這篇文章主要為大家詳細(xì)介紹了Python中的time模塊以及如何利用time模塊實(shí)現(xiàn)時(shí)間戳與結(jié)構(gòu)化時(shí)間,文中的示例代碼講解詳細(xì),需要的可以參考一下2022-06-06
Python實(shí)現(xiàn)音頻添加數(shù)字水印的示例詳解
數(shù)字水印技術(shù)可以將隱藏信息嵌入到音頻文件中而不明顯影響音頻質(zhì)量,下面小編將介紹幾種在Python中實(shí)現(xiàn)音頻數(shù)字水印的方法,希望對大家有所幫助2025-04-04
wx.CheckBox創(chuàng)建復(fù)選框控件并響應(yīng)鼠標(biāo)點(diǎn)擊事件
這篇文章主要為大家詳細(xì)介紹了wx.CheckBox創(chuàng)建復(fù)選框控件并響應(yīng)鼠標(biāo)點(diǎn)擊事件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
在Python中定義函數(shù)并調(diào)用的操作步驟
這篇文章主要介紹了在Python中如何定義函數(shù)并調(diào)用它,函數(shù)的定義和調(diào)用是Python編程中最基本也是最重要的概念之一,掌握它們對于進(jìn)行有效的Python編程至關(guān)重要,需要的朋友可以參考下2024-01-01
Python數(shù)據(jù)序列化技術(shù)總結(jié)
在現(xiàn)代軟件開發(fā)中,數(shù)據(jù)序列化是一個(gè)關(guān)鍵環(huán)節(jié),它允許我們將復(fù)雜的數(shù)據(jù)結(jié)構(gòu)轉(zhuǎn)換為可存儲或可傳輸?shù)母袷?,Python提供了多種數(shù)據(jù)序列化技術(shù),每種技術(shù)都有其獨(dú)特的性能優(yōu)勢和適用場景,本文將詳細(xì)介紹幾種強(qiáng)大的Python數(shù)據(jù)序列化技術(shù),需要的朋友可以參考下2025-03-03
在pycharm中關(guān)掉ipython console/PyDev操作
這篇文章主要介紹了在pycharm中關(guān)掉ipython console/PyDev操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
python datetime處理時(shí)間小結(jié)
這篇文章主要介紹了python datetime處理時(shí)間小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
使用python編寫android截屏腳本雙擊運(yùn)行即可
使用python編寫一個(gè)截屏的腳本,雙擊運(yùn)行腳本就OK,截屏成功后會(huì)將截屏文件已當(dāng)前時(shí)間命名,并保存在存放腳本的當(dāng)前路徑的screenshot文件夾下2014-07-07

