python中文分詞+詞頻統(tǒng)計(jì)的實(shí)現(xiàn)步驟
提示:文章寫完后,目錄可以自動(dòng)生成,如何生成可參考右邊的幫助文檔
前言
本文記錄了一下Python在文本處理時(shí)的一些過程+代碼
一、文本導(dǎo)入
我準(zhǔn)備了一個(gè)名為abstract.txt的文本文件
接著是在網(wǎng)上下載了stopword.txt(用于結(jié)巴分詞時(shí)的停用詞)
有一些是自己覺得沒有用加上去的
另外建立了自己的詞典extraDict.txt
準(zhǔn)備工作做好了,就來看看怎么使用吧!
二、使用步驟
1.引入庫
代碼如下:
import jieba from jieba.analyse import extract_tags from sklearn.feature_extraction.text import TfidfVectorizer
2.讀入數(shù)據(jù)
代碼如下:
jieba.load_userdict('extraDict.txt') # 導(dǎo)入自己建立詞典
3.取出停用詞表
def stopwordlist(): stopwords = [line.strip() for line in open('chinesestopwords.txt', encoding='UTF-8').readlines()] # ---停用詞補(bǔ)充,視具體情況而定--- i = 0 for i in range(19): stopwords.append(str(10 + i)) # ---------------------- return stopwords
4.分詞并去停用詞(此時(shí)可以直接利用python原有的函數(shù)進(jìn)行詞頻統(tǒng)計(jì))
def seg_word(line): # seg=jieba.cut_for_search(line.strip()) seg = jieba.cut(line.strip()) temp = "" counts = {} wordstop = stopwordlist() for word in seg: if word not in wordstop: if word != ' ': temp += word temp += '\n' counts[word] = counts.get(word, 0) + 1#統(tǒng)計(jì)每個(gè)詞出現(xiàn)的次數(shù) return temp #顯示分詞結(jié)果 #return str(sorted(counts.items(), key=lambda x: x[1], reverse=True)[:20]) # 統(tǒng)計(jì)出現(xiàn)前二十最多的詞及次數(shù)
5. 輸出分詞并去停用詞的有用的詞到txt
def output(inputfilename, outputfilename): inputfile = open(inputfilename, encoding='UTF-8', mode='r') outputfile = open(outputfilename, encoding='UTF-8', mode='w') for line in inputfile.readlines(): line_seg = seg_word(line) outputfile.write(line_seg) inputfile.close() outputfile.close() return outputfile
6.函數(shù)調(diào)用
if __name__ == '__main__': print("__name__", __name__) inputfilename = 'abstract.txt' outputfilename = 'a1.txt' output(inputfilename, outputfilename)
7.結(jié)果
附:輸入一段話,統(tǒng)計(jì)每個(gè)字母出現(xiàn)的次數(shù)
先來講一下思路:
例如給出下面這樣一句話
Love is more than a word
it says so much.
When I see these four letters,
I almost feel your touch.
This is only happened since
I fell in love with you.
Why this word does this,
I haven’t got a clue.
那么想要統(tǒng)計(jì)里面每一個(gè)單詞出現(xiàn)的次數(shù),思路很簡單,遍歷一遍這個(gè)字符串,再定義一個(gè)空字典count_dict,看每一個(gè)單詞在這個(gè)用于統(tǒng)計(jì)的空字典count_dict中的key中存在否,不存在則將這個(gè)單詞當(dāng)做count_dict的鍵加入字典內(nèi),然后值就為1,若這個(gè)單詞在count_dict里面已經(jīng)存在,那就將它對應(yīng)的鍵的值+1就行
下面來看代碼:
#定義字符串 sentences = """ # 字符串很長時(shí)用三個(gè)引號 Love is more than a word it says so much. When I see these four letters, I almost feel your touch. This is only happened since I fell in love with you. Why this word does this, I haven't got a clue. """ #具體實(shí)現(xiàn) # 將句子里面的逗號去掉,去掉多種符號時(shí)請用循環(huán),這里我就這樣吧 sentences=sentences.replace(',','') sentences=sentences.replace('.','') # 將句子里面的.去掉 sentences = sentences.split() # 將句子分開為單個(gè)的單詞,分開后產(chǎn)生的是一個(gè)列表sentences # print(sentences) count_dict = {} for sentence in sentences: if sentence not in count_dict: # 判斷是否不在統(tǒng)計(jì)的字典中 count_dict[sentence] = 1 else: # 判斷是否不在統(tǒng)計(jì)的字典中 count_dict[sentence] += 1 for key,value in count_dict.items(): print(f"{key}出現(xiàn)了{(lán)value}次")
輸出結(jié)果是這樣:
總結(jié)
以上就是今天要講的內(nèi)容,本文僅僅簡單介紹了python的中文分詞及詞頻統(tǒng)計(jì)!
到此這篇關(guān)于python中文分詞+詞頻統(tǒng)計(jì)的實(shí)現(xiàn)步驟的文章就介紹到這了,更多相關(guān)python中文分詞 詞頻統(tǒng)計(jì)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 一文帶你掌握Python中文詞頻統(tǒng)計(jì)
- Python可視化單詞統(tǒng)計(jì)詞頻統(tǒng)計(jì)中文分詞的實(shí)現(xiàn)步驟
- Python jieba 中文分詞與詞頻統(tǒng)計(jì)的操作
- python實(shí)現(xiàn)簡單中文詞頻統(tǒng)計(jì)示例
- Python英文文章詞頻統(tǒng)計(jì)(14份劍橋真題詞頻統(tǒng)計(jì))
- python寫程序統(tǒng)計(jì)詞頻的方法
- python利用多種方式來統(tǒng)計(jì)詞頻(單詞個(gè)數(shù))
- Python統(tǒng)計(jì)中文詞頻的四種方法小結(jié)
相關(guān)文章
Python數(shù)據(jù)類型之Set集合實(shí)例詳解
這篇文章主要介紹了Python數(shù)據(jù)類型之Set集合,結(jié)合實(shí)例形式詳細(xì)分析了Python數(shù)據(jù)類型中集合的概念、原理、創(chuàng)建、遍歷、交集、并集等相關(guān)操作技巧,需要的朋友可以參考下2019-05-05python?實(shí)現(xiàn)dcmtk關(guān)聯(lián)pacs功能推送下拉影像(推薦)
這篇文章主要介紹了python?實(shí)現(xiàn)dcmtk關(guān)聯(lián)pacs功能?推送下拉影像,包含dcmtk關(guān)聯(lián)pacs技術(shù)筆記等相關(guān)知識,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10Python二進(jìn)制文件讀取并轉(zhuǎn)換為浮點(diǎn)數(shù)詳解
這篇文章主要介紹了Python二進(jìn)制文件讀取并轉(zhuǎn)換為浮點(diǎn)數(shù)詳解,用python讀取二進(jìn)制文件,這里主要用到struct包,而這個(gè)包里面的方法主要是unpack、pack、calcsize。,需要的朋友可以參考下2019-06-06PyQt5高級界面控件之QTableWidget的具體使用方法
這篇文章主要介紹了PyQt5高級界面控件之QTableWidget的具體使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02