赞
踩
import numpy as np
import jieba
import re
class xinxilunshiyan1(object):
def __init__(self):
self.fileName_China = '信息论实验一_中文'
self.fileName_English = '信息论实验一_英语'
self.content_readfile = None
self.jieba_cut_chinese = []
self.jieba_cut_english = []
self.content_analysis_chinese = {}
self.content_analysis_english = {}
self.content_cleandata_chinese = []
self.content_cleandata_english = []
self.r = "[]_.!+-=——,$%^,。?、~@#¥%……&*《》<>「」{}【】()/]"
#1. 编写自信息量计算的函数(可以使用任意编程语言)
def zixinxi(self,*x):
zxx = -(np.log2(x))
return zxx
#2. 编写计算信息熵的函数(要求:其中的自信息量的计算要调用前面定义的函数
def xinxishang(self,*x):
arr = np.array(x)
p = arr/sum(arr)
xxs = p*self.zixinxi(p)
ex = np.sum(xxs)
return ex
#3.读取英文文本文件的内容,统计文本中每个字符出现的频率,做为无记忆信源一的输出概率分布并保存
#4.读取中文文本文件的内容,统计文本中每个字符出现的频率,做为无记忆信源二的输出概率分布并保存.
def readFile(self,filename):
try:
with open(filename,'r',encoding='utf-8') as f:
self.content_readfile = f.read()
self.content_readfile = re.sub(self.r, '', self.content_readfile)
except Exception as e:
print(e)
return self.content_readfile
#4.读取中文文本文件的内容,统计文本中每个字符出现的频率,做为无记忆信源二的输出概率分布并保存.
def jiebafenci_chinese(self,x):
for line in self.readFile(x):
self.jieba_cut_chinese.extend(jieba.cut(line.strip()))
return self.jieba_cut_chinese
#3.读取英文文本文件的内容,统计文本中每个字符出现的频率,做为无记忆信源一的输出概率分布并保存
def jiebafenci_english(self,x):
for line in self.readFile(x):
self.jieba_cut_english.extend(jieba.cut(line.strip()))
return self.jieba_cut_english
#4.读取中文文本文件的内容,统计文本中每个字符出现的频率,做为无记忆信源二的输出概率分布并保存.
def pinlv_China(self,x):
for word in self.jiebafenci_chinese(x):
if word in self.content_analysis_chinese.keys():
self.content_analysis_chinese[word] += 1
else:
self.content_analysis_chinese[word] = 1
return self.content_analysis_chinese
#3.读取英文文本文件的内容,统计文本中每个字符出现的频率,做为无记忆信源一的输出概率分布并保存
def pinlv_English(self,x):
for word in self.jiebafenci_english(x):
if word in self.content_analysis_english.keys():
self.content_analysis_english[word] += 1
else:
self.content_analysis_english[word] = 1
return self.content_analysis_english
def data_clean_chinese(self,**data):
self.content_cleandata_chinese = list(data.values())
return self.content_cleandata_chinese
def data_clean_english(self,**data):
self.content_cleandata_english = list(data.values())
return self.content_cleandata_english
#测试代码
if __name__ == "__main__":
Demo = xinxilunshiyan1()
# a = [2,4]
# print(Demo.zixinxi(*a))
# print(Demo.xinxishang(*a))
# print(Demo.readFile('信息论实验一_中文.txt'))
print(Demo.pinlv_China('信息论实验一_中文.txt'))
# print(Demo.readFile('信息论实验一_英语.txt'))
print(Demo.pinlv_English('信息论实验一_英语.txt'))
print(Demo.data_clean_english(**Demo.content_analysis_english))
print(Demo.data_clean_chinese(**Demo.content_analysis_chinese))
chinese = Demo.xinxishang(*Demo.content_cleandata_chinese)
english = Demo.xinxishang(*Demo.content_cleandata_english)
if chinese > english :
print(f'中文的信息熵为:{chinese},他是信源二,他的不确定性高')
else:
print(f'英文的信息熵为:{english},他是信源一,他的不确定性高')
print('============================')
print(f'中文的信息熵为:{chinese}')
print(f'英文的信息熵为:{english}')
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。