日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

词频统计预处理之综合练习

發布時間:2023/11/29 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 词频统计预处理之综合练习 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

下載一首英文的歌詞或文章

news=''' ''',

生成詞頻統計

sep=''',.;:''""''' for c in sep:news=news.replace(c,' ')wordlist=news.lower().split()wordDict={} for w in wordlist:wordDict[w]=wordDict.get(w,0)+1 ''' wordSet=set(wordlist) for w in wordSet:wordDict[w]=wordlist.count(w) ''' for w in wordDict:print(w, wordDict[w])

  部分演示效果如下圖所示:

排序

wordSet=set(wordlist) for w in wordSet:wordDict[w]=wordlist.count(w) dictList=list(wordDict.items()) dictList.sort(key=lambda x:x[1],reverse=True)print(dictList)

  效果演示如下圖所示:

排除語法型詞匯,代詞、冠詞、連詞

exclude={'the','a','an','and','of','with','to','by','am','are','is','which','on'} wordSet=set(wordlist)-exclude for w in wordSet:wordDict[w]=wordlist.count(w) dictList=list(wordDict.items()) dictList.sort(key=lambda x:x[1],reverse=True)print(dictList)

  效果演示如下圖所示:

輸出詞頻最大TOP20

for i in range(20):print(dictList[i])

  效果演示如下圖所示:

將分析對象存為utf-8編碼的文件,通過文件讀取的方式獲得詞頻分析內容。

print('author:xujinpei') f=open('news.txt','r') news=f.read() f.close() print(news)

  效果演示如下圖所示:

?中文詞頻統計,下載一長篇中文文章。

import jieba#打開文件 file = open("gzccnews.txt",'r',encoding="utf-8") notes = file.read(); file.close();#替換標點符號 sep = ''':。,?!;∶ ...“”''' for i in sep:notes = notes.replace(i,' ');notes_list = list(jieba.cut(notes));#排除單詞 exclude =[' ','\n','你','我','他','和','但','了','的','來','是','去','在','上','高']#方法②,遍歷列表 notes_dict={} for w in notes_list:notes_dict[w] = notes_dict.get(w,0)+1# 排除不要的單詞 for w in exclude:del (notes_dict[w]);for w in notes_dict:print(w,notes_dict[w])# 降序排序 dictList = list(notes_dict.items()) dictList.sort(key=lambda x:x[1],reverse=True); print(dictList)#輸出詞頻最大TOP20 for i in range(20):print(dictList[i])#把結果存放到文件里 outfile = open("top20.txt","a") for i in range(20):outfile.write(dictList[i][0]+" "+str(dictList[i][1])+"\n") outfile.close();

?效果演示如下圖所示:

轉載于:https://www.cnblogs.com/xujinpei/p/8658461.html

總結

以上是生活随笔為你收集整理的词频统计预处理之综合练习的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。