Word VBA Script for Shannon entropy of the alphabetic characters

我们两清 提交于 2019-12-08 10:37:41

问题


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.


Shannon entropy for each character


(source: 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

What I am trying to get

Today was a good day

The Shannon entropy of the characters in this document is 1.2798542258337

回答1:


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?

Let's start with variables:

dim characters(1 to 27) as double
dim x as integer 'for looping
dim total as double 'The final value

Define the values:

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.

Once you have your array filled

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

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?



来源:https://stackoverflow.com/questions/27195372/word-vba-script-for-shannon-entropy-of-the-alphabetic-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!