完整的中英文词频统计

步骤:

1.准备utf-8编码的文本文件file

2.通过文件读取字符串 str

3.对文本进行预处理

4.分解提取单词 list

5.单词计数字典 set , dict

6.按词频排序 list.sort(key=)

7.排除语法型词汇,代词、冠词、连词等无语义词

8.输出TOP(20)

一、.英文歌曲 词频统计

str2='''I will run, I will climb, I will soar
I'm undefeated
Jumpiing out of my skin, pull the chord
Yeah I believe it
The past, is everything we were
don't make us who we are
So I'll dream, until I make it real,
and all I see is stars
Its not until you fall that you fly
When your dreams come alive you're unstoppable
Take a shot, chase the sun, find the beautiful
We will glow in the dark turning dust to gold
And we'll dream it possible
possible
And we'll dream it possible
I will chase, I will reach, I will fly
Until I'm breaking, until I'm breaking
Out of my cage, like a bird in the night
I know I'm changing, I know I'm changing
In, into something big, better than before
And if it takes, takes a thousand lives
Then it's worth fighting for
Its not until you fall that you fly
When your dreams come alive you're unstoppable
Take a shot, chase the sun, find the beautiful
We will glow in the dark turning dust to gold
And we'll dream it possible
it possible
From the bottom to the top
We're sparking wild fire's
Never quit and never stop
The rest of our lives
From the bottom to the top
We're sparking wild fire's
Never quit and never stop
Its not until you fall that you fly
When your dreams come alive you're unstoppable
Take a shot, chase the sun, find the beautiful
We will glow in the dark turning dust to gold
And we'll dream it possible
possible
And we'll dream it possible'''.lower()
#aa = '''."?!'''
#for word in aa:
#   str2 =str2.replace('word','')
str2 =str2.replace('\n',' ')
str2 =str2.replace(',',' ')
print(str2)#去除特殊符号
 
str2 = str2.strip()#去掉首尾空格
str2 = str2.split()#通过指定分隔符对字符串进行切片
print(str2)
 
print('统计每个单词出现的次数为:')
for word in str2:
   print(word,str2.count(word))
 
strSet=set(str2)
newSet={'a','will','it','out','of','my','the','i','in','to','when','and'}
strSet1=strSet-newSet#去除介词和其他
print(strSet1)
 
 
strdict={}          #单词计数字典
for word in strSet1:
    strdict[word] = str2.count(word)
print(len(strdict),strdict)
 
strList = list(strdict.items())
def takesecond(elem):#定义函数
        return elem[1]
#strList.sort(key=lambda x:x[1],reverse=True)#匿名函数
strList.sort(key=takesecond,reverse=True)#按照数值大小进行排序
print(strList)
 
 
for i in range(20):
    print (strList[i])#前二十

  

2.中文小说 词频统计

import jieba;
#准备utf-8编码的文本文件file
f = open('doupo.txt','r',encoding='utf-8')
#通过文件读取字符串 str,对文本进行预处理
fo=f.read()
f.close()
print(fo)
 
#用字典形式统计每个词的字数
doupols = jieba.lcut(fo)
doupodict = {}
for word in doupols:
    if len(word)==1:
        continue
    else:
        doupodict[word]=doupodict.get(word,0)+1
print(doupodict)
#cut
print(list(jieba.cut(fo)))      #精确模式,将句子最精确的分开,适合文本分析
print(list(jieba.cut(fo,cut_all=True)))  #全模式,把句子中所有的可以成词的词语都扫描出来
print(list(jieba.cut_for_search(fo)))    #搜索引擎模式,在精确模式的基础上,对长词再次切分,提高召回率,适合用于搜索引擎分词
 
#以列表返回可遍历的(键, 值) 元组数组
wcList = list(doupodict.items())
wcList.sort(key = lambda x:x[1],reverse=True)   #出现词汇次数由高到低排序
print(wcList)
 
#第一个词循环遍历输出5次
for i in range(5):
    print(wcList[1])

  

 

posted @ 2018-10-19 21:14  张震34  阅读(239)  评论(0编辑  收藏  举报