用于字母字符的香农熵的 Word VBA 脚本 [英] Word VBA Script for Shannon entropy of the alphabetic characters
问题描述
我有一个文档,想在 Word VBA 中获取每个字符的香农熵.我已经计算了每个字符的香农熵,但不知道如何将这些信息放入一个单词 VBA 中并进行总结.
I have a document and want to take the Shannon entropy of each character in Word VBA. I have already calculated the Shannon entropy for each character but don't know how I would put this information in a word VBA and sum it up.
每个字符的香农熵
(来源:bearcave.com)子>
Space,0.1859
A 0.0856
B,0.0139
C,0.0279
D,0.0378
E,0.1304
F,0.0289
G,0.0199
H,0.0528
I,0.0627
J,0.0013
K,0.0042
L,0.0339
M,0.0249
N,0.0707
O,0.0797
P,0.0199
Q,0.0012
R,0.0677
S,0.0607
T,0.1045
U,0.0249
V,0.0092
W,0.0149
X,0.0017
Y,0.0199
Z,0.0008
<小时>
我想得到什么
今天是个好日子
The Shannon entropy of the characters in this document is 1.2798542258337
推荐答案
嗯,据我所知,您总共有 27 个字符,每个字符都有一个预定义的值,您想简单地总结一下?
Well, from what I can tell you have 27 characters total each with a pre-defined value and you want to simply sum them up?
让我们从变量开始:
dim characters(1 to 27) as double
dim x as integer 'for looping
dim total as double 'The final value
定义值:
characters(1) = 0.1859 'space
characters(2) = 0.0856 'A
characters(3) = 0.0139 'B
characters(4) = 0.0279 'C
characters(5) = 0.0378 'D
'ETC.
一旦你的数组被填满
for x = 1 to 27
total = total + characters(x)
next
现在,如果你想变得更疯狂,请告诉我.例如,可以将它写成读取一个充满数据的表然后计算值的方式.
Now, If you want to get crazier let me know. For instance, it's possible to write it in a way to where it will read a table full of data and then calculate the value.
对于输出,这实际上取决于您希望输出显示的位置.直接在文档中显示:
For output, it really depends on where you want the output to be displayed. To display it directly in the document:
ActiveDocument.Paragraphs.add
ActiveDocument.Paragraphs(ActiveDocument.Paragraphs.Count).range.text = "The Shannon entropy of the characters in this document is" & total
如果你已经有了所有的答案,我更想知道为什么你需要 VBA.好像计算器就够了?
I'm more curious to know why you need VBA for this if you already have all your answers. It seems like a calculator would suffice?
这篇关于用于字母字符的香农熵的 Word VBA 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!