综合练习:词频统计(代码片段)

compione compione     2022-10-29     264

关键词:

1.英文词频统

下载一首英文的歌词或文章

将所有,.?!’:等分隔符全部替换为空格

将所有大写转换为小写

生成单词列表

生成词频统计

排序

排除语法型词汇,代词、冠词、连词

输出词频最大TOP20

1.英文词频统

下载一首英文的歌词或文章

将所有,.?!’:等分隔符全部替换为空格

将所有大写转换为小写

生成单词列表

生成词频统计

排序

排除语法型词汇,代词、冠词、连词

输出词频最大TOP20

f=open(‘sugar.txt‘,‘r‘)
news=f.read()
f.close()
sep=‘‘‘,.‘!"?:‘‘‘
exclude=‘the‘,‘a‘,‘an‘,‘to‘,‘be‘,‘i‘,‘so‘,‘over‘,
for c in sep:
   news=news.replace(c,‘‘)
   wordList=news.lower().split()
wordDict=
wordSet=set(wordList)-exclude
for w in wordSet:
    wordDict[w]=wordList.count(w)
for w in wordDict:
      print(w,wordDict[w])

      dic = sorted(wordDict.items(), key=lambda d: d[1], reverse=True)
      print(dic)
      for i in range(20):
          print(dic[i])

  

2.中文词频统计

下载一长篇中文文章。

从文件读取待分析文本。

news = open(‘gzccnews.txt‘,‘r‘,encoding = ‘utf-8‘)

安装与使用jieba进行中文分词。

pip install jieba

import jieba

list(jieba.lcut(news))

生成词频统计

排序

排除语法型词汇,代词、冠词、连词

输出词频最大TOP20(或把结果存放到文件里)

import jieba
n= open(news.txt,r,encoding=UTF-8)
news=n.read()
n.close()
news = list(jieba.cut(news))
s= "","","","","",""," ","","","","\ufeff","\n","\u3000"
newsset=set(news)-s
exclude=,,,,,,
newsset=newsset-exclude
strdict = 
# 通过遍历列表创建字典
for i in newsset:
    strdict[i] = news.count(i)
dictlist = list(strdict.items())
dictlist.sort(key=lambda x: x[1], reverse=True)
f = open(newscount.txt, a)
for i in range(20):
    f.write(dictlist[i][0] +   + str(dictlist[i][1]) + \n)
f.close()

 

综合练习:词频统计(代码片段)

str_context=‘‘‘Thereareseveralreasonsforthis.Tobeginwith,nowadayscollegestudentsaimtoohigh.Alltheywantare“good”jobswhichcouldoffergoodsalary,comfortableworkingconditions,highsocialstatusamongothers.Co 查看详情

综合练习:英文词频统计(代码片段)

str=‘‘‘AslongasyoulovemeWe‘reunderpressureSevenbillionpeopleintheworldtryingtofitinKeepittogetherSmileonyourfaceeventhoughyourheartisfrowningButheynowyouknowgirlWebothknowit‘sacruelworldButIwilltakemy 查看详情

综合练习:词频统计(代码片段)

song=‘‘‘Anemptystreet,Anemptyhouse,Aholeinsidemyheart,I‘mallalone,Theroomsaregettingsmaller,Iwonderhow,Iwonderwhy,Iwonderwheretheyare,Thedayswehad,Thesongswesangtogether,Ohyeah,Andohmylove,I‘mholdingo 查看详情

综合练习:词频统计(代码片段)

综合练习词频统计预处理下载一首英文的歌词或文章将所有,.?!’:等分隔符全部替换为空格将所有大写转换为小写生成单词列表生成词频统计排序排除语法型词汇,代词、冠词、连词输出词频最大TOP20将分析对象存为utf-8编码... 查看详情

综合练习:词频统计(代码片段)

综合练习词频统计预处理下载一首英文的歌词或文章将所有,.?!’:等分隔符全部替换为空格将所有大写转换为小写生成单词列表生成词频统计排序排除语法型词汇,代词、冠词、连词输出词频最大TOP20将分析对象存为utf-8编码... 查看详情

综合练习:英文词频统计(代码片段)

词频统计预处理下载一首英文的歌词或文章将所有,.?!’:等分隔符全部替换为空格将所有大写转换为小写生成单词列表生成词频统计排序排除语法型词汇,代词、冠词、连词输出词频最大TOP10song=‘‘‘Ifyousayyou’rethefireworkatthe... 查看详情

综合练习:英文词频统计(代码片段)

词频统计预处理下载一首英文的歌词或文章将所有,.?!’:等分隔符全部替换为空格将所有大写转换为小写生成单词列表生成词频统计排序排除语法型词汇,代词、冠词、连词输出词频最大TOP10song=‘‘‘Troublewillfindyounomatterwherey... 查看详情

综合练习:词频统计(代码片段)

song=‘‘‘justcolseyoureyes,thesunisgoingdownyou‘llbeallright,noonecanhurtyounowcomemorninglight,youandi’llbesafeandsound‘‘‘str1=song.replace(‘’‘,‘‘).lower().split()str2=song.split()c=foriinstr2:count 查看详情

综合练习:英文词频统计(代码片段)

#coding=utf-8song=‘‘‘Skies,wherethebluebirdsfly,Cloudswherethepeopleplacetheirsoulsonto.Brighter,thesunshinesthatgothroughmytears,likesearchingforwhathealsmysorrows,Cry,whenthetwilight‘scome,Rise,when 查看详情

综合练习:词频统计(代码片段)

1.英文词频统下载一首英文的歌词或文章将所有,.?!’:等分隔符全部替换为空格将所有大写转换为小写生成单词列表生成词频统计排序排除语法型词汇,代词、冠词、连词输出词频最大TOP201.英文词频统下载一首英文的歌词或文... 查看详情

综合练习:英文词频统计(代码片段)

词频统计预处理下载一首英文的歌词或文章将所有,.?!’:等分隔符全部替换为空格将所有大写转换为小写生成单词列表生成词频统计排序排除语法型词汇,代词、冠词、连词输出词频最大TOP101#-*-coding:UTF-8-*-2#-*-author:yjw-*-34Music=... 查看详情

综合练习:词频统计(代码片段)

下载一首英文的歌词或文章将所有,.?!’:等分隔符全部替换为空格将所有大写转换为小写生成单词列表f=open(‘news.txt‘,‘r‘)news=f.read()f.close()sep=‘‘‘,.‘!"?:‘‘‘forcinsep:news=news.replace(c,‘‘)wordList=news.lower().split()forwinwordLis... 查看详情

综合练习:词频统计(代码片段)

file=open(‘song.txt‘,‘r‘)#只读打开文件lyrics=file.read()#读取文件内容file.close()#关闭文件资源sep=‘‘‘,.?!:‘-‘‘‘forcinsep:song=song.replace(c,‘‘)wordList=song.lower().split()wordDict=forwinwordList:wordDict[w]=wordDic 查看详情

综合练习:词频统计(代码片段)

1.英文词频统f=open(‘lyric.txt‘,‘r‘)lyric=f.read()f.close()punctuation=‘‘‘,.?/:;‘"‘‘‘a=‘in‘,‘on‘,‘with‘,‘by‘,‘for‘,‘at‘,‘about‘,‘under‘,‘of‘,‘i‘,‘a‘,‘is‘,‘its‘,‘so‘,‘and‘,‘dont‘,‘it‘,‘to‘,‘ill‘,‘t... 查看详情

综合练习:词频统计(代码片段)

#读取英文歌词文件file=open(‘dd.txt‘,‘r‘,encoding=‘utf-8‘)word=file.read()file.close()#排除符号di=‘‘‘.,‘‘""?!:;‘‘‘foriindi:word=word.replace(i,‘‘)#大写转换小写空格分割word=word.lower().split()#定义字典存放数据diet=forcinword:coun 查看详情

综合练习:词频统计(代码片段)

联系要求下载一首英文的歌词或文章将歌词存入文件中,然后读取出来将所有,.?!’:等分隔符全部替换为空格将所有大写转换为小写生成单词列表生成词频统计排序排除语法型词汇,代词、冠词、连词输出词频最大TOP20将分析... 查看详情

综合练习:词频统计(代码片段)

 下载一首英文的歌词或文章f=open("F:\song.txt","r")str1=f.read()f.close() 将所有,.?!’:等分隔符全部替换为空格c="‘,./‘"forwinc:str1.replace(w,‘‘)将所有大写转换为小写生成单词列表wordList=str1.lower().split()生成词频统计wordDict=word... 查看详情

综合练习:词频统计(代码片段)

f=open(‘/Users/Administrator/Desktop/we.txt‘,‘r‘)text=f.read()f.close()print(text)rep=‘‘‘,.!:?‘‘‘exclude=‘a‘,‘the‘,‘for‘,‘in‘,‘i‘,‘my‘,‘that‘,‘of‘,‘have‘,‘and‘,‘had‘,‘it‘,‘are‘forcinrep:str=text.rep 查看详情