python 文本單詞提取和詞頻統(tǒng)計的實例
這些對文本的操作經(jīng)常用到, 那我就總結(jié)一下。 陸續(xù)補充。。。
操作:
strip_html(cls, text) 去除html標簽
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)計的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
使用pytorch搭建AlexNet操作(微調(diào)預訓練模型及手動搭建)
今天小編就為大家分享一篇使用pytorch搭建AlexNet操作(微調(diào)預訓練模型及手動搭建),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
python?tkinter實現(xiàn)學生信息管理系統(tǒng)
這篇文章主要為大家詳細介紹了python?tkinter實現(xiàn)學生信息管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02
解決Python?出現(xiàn)File?“<stdin>“,?line?1非語法錯誤的問題
這篇文章主要介紹了Python?出現(xiàn)File?“<stdin>“,?line?1非語法錯誤的解決辦法,本文給大家講解的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03
使用Python爬蟲框架獲取HTML網(wǎng)頁中指定區(qū)域的數(shù)據(jù)
在當今互聯(lián)網(wǎng)時代,數(shù)據(jù)已經(jīng)成為了一種寶貴的資源,無論是進行市場分析、輿情監(jiān)控,還是進行學術研究,獲取網(wǎng)頁中的數(shù)據(jù)都是一個非常重要的步驟,Python提供了多種爬蟲框架來幫助我們高效地獲取網(wǎng)頁數(shù)據(jù),本文將詳細介紹如何使用Python爬蟲框架來獲取HTML網(wǎng)頁中指定區(qū)域的數(shù)據(jù)2025-03-03

