python 文本單詞提取和詞頻統(tǒng)計(jì)的實(shí)例
這些對(duì)文本的操作經(jīng)常用到, 那我就總結(jié)一下。 陸續(xù)補(bǔ)充。。。
操作:
strip_html(cls, text) 去除html標(biāo)簽
separate_words(cls, text, min_lenth=3) 文本提取
get_words_frequency(cls, words_list) 獲取詞頻
源碼:
class DocProcess(object): @classmethod def strip_html(cls, text): """ Delete html tags in text. text is String """ new_text = " " is_html = False for character in text: if character == "<": is_html = True elif character == ">": is_html = False new_text += " " elif is_html is False: new_text += character return new_text @classmethod def separate_words(cls, text, min_lenth=3): """ Separate text into words in list. """ splitter = re.compile("\\W+") return [s.lower() for s in splitter.split(text) if len(s) > min_lenth] @classmethod def get_words_frequency(cls, words_list): """ Get frequency of words in words_list. return a dict. """ num_words = {} for word in words_list: num_words[word] = num_words.get(word, 0) + 1 return num_words
以上這篇python 文本單詞提取和詞頻統(tǒng)計(jì)的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python中的xml與dict的轉(zhuǎn)換方法詳解
這篇文章主要介紹了Python中的xml與dict的轉(zhuǎn)換方法詳解,xml 是指可擴(kuò)展標(biāo)記語(yǔ)言,一種標(biāo)記語(yǔ)言類似html,作用是傳輸數(shù)據(jù),而且不是顯示數(shù)據(jù)。可以自定義標(biāo)簽,需要的朋友可以參考下2023-07-07Django模板標(biāo)簽{% for %}循環(huán),獲取制定條數(shù)據(jù)實(shí)例
這篇文章主要介紹了Django模板標(biāo)簽{% for %}循環(huán),獲取制定條數(shù)據(jù)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-05-05Python實(shí)現(xiàn)查找數(shù)據(jù)庫(kù)最接近的數(shù)據(jù)
這篇文章主要介紹了Python實(shí)現(xiàn)查找數(shù)據(jù)庫(kù)最接近的數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06詳解JavaScript編程中的window與window.screen對(duì)象
這篇文章主要介紹了JavaScript編程中的window與window.screen對(duì)象,是JS在瀏覽器中視圖編程的基礎(chǔ),需要的朋友可以參考下2015-10-10python 實(shí)現(xiàn)添加標(biāo)簽&打標(biāo)簽的操作
這篇文章主要介紹了python 實(shí)現(xiàn)添加標(biāo)簽&打標(biāo)簽的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05