综合练习:词频统计

综合练习

词频统计预处理

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

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

将所有大写转换为小写

生成单词列表

生成词频统计

排序

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

输出词频最大TOP20

将分析对象存为utf-8编码的文件,通过文件读取的方式获得词频分析内容。

# 从记事本长读取文件
f = open('news.txt','r',encoding='UTF-8')#打开文件
news = f.read()#读取文件
f.close()#关闭文件
print(news)

s = ''',.;:'"!?”、‘;:,。!’“'''
# 定义一个不需要的单词列表,即需删除的单词,例如代词、冠词、连词等
exclude = {'to', 'and', 'a', 'of', 'on'}
for i in s:
    news = news.lower().replace(i,' ')

newsList = news.split()
for i in newsList:
    print(i)


#方法1:通过遍历集合创建字典
newsDict = {}
print('方法1:')
# set集合,去除了重复的键
#去除不必要的单词,直接用列表减去需要去除的列表名
newsSet = set(newsList)-exclude
for w in newsSet:
    newsDict[w] = newsList.count(w)

for w in newsDict:
    print(w, newsDict[w])

# 方法2:通过遍历列表创建字典
newsDict2 = {}
print('方法2:')
for n in newsList:
    newsDict2[n] = newsDict2.get(n, 0)+1
# 减去不必要的单词
for e in exclude:
    del(newsDict2[e])

for n in newsDict2:
    print(n, newsDict2[n])

print(newsDict2)

# 按歌词出现的次数进行排序
# 将newsDict转变成列表
# newsDict.keys()#获取到newsDict的key值
# newsDict.values()获取到newsDict的values值
# newsDict.items()获取newsDict的key和values的值
dictList = list(newsDict.items())
dictList.sort(key=lambda x:x[1],reverse=True)

print(dictList)

# 输出前20个数据
for i in range(20):
    print(dictList[i])

# 保存文件,
f = open('newscount.txt','a')
for i in range(20):
    f.write(dictList[i][0]+' '+str(dictList[i][1])+'
')
f.close()

  

2.中文词频统计

下载一长篇中文文章。

从文件读取待分析文本。

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

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

pip install jieba

import jieba

list(jieba.lcut(news))

生成词频统计

排序

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

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

import jieba

f = open('asdf.txt','r', encoding = 'utf-8')
news = f.read()
f.close()
print(list(jieba.cut(news)))

s = ''',.;:'"!?”、‘;:,。!’“''?":“。”.!'《》'''
exclude = [' ','了','的','我','
','他','道', '你','也','是','又','着','去','来', '在','都','不','虽','为','却','那','亦','之','将','这','便','则','只','但','乃','再','因','得','此','与']
for i in s:
    news = news.lower().replace(i,' ')

newsList = news.split()
for i in newsList:
    print(i)
news = list(jieba.cut(news))
#方法1:通过遍历集合创建字典
newsDict = {}
print('方法1:')

# set集合,去除了重复的键
#去除不必要的单词,直接用列表减去需要去除的列表名
newsSet = set(newsList)-exclude
for w in newsSet:
    newsDict[w] = newsList.count(w)

for w in newsDict:
    print(w, newsDict[w])

# 按歌词出现的次数进行排序
# 将newsDict转变成列表
# newsDict.keys()#获取到newsDict的key值
# newsDict.values()获取到newsDict的values值
# newsDict.items()获取newsDict的key和values的值
dictList = list(newsDict.items())
dictList.sort(key=lambda x:x[1],reverse=True)

print(dictList)


# 保存文件,
f = open('newszxcf.txt','a')
for i in range(20):
    f.write(dictList[i][0]+' '+str(dictList[i][1])+'
')
f.close()

  

原文地址:https://www.cnblogs.com/2647409627qq/p/8658564.html