【问题标题】:Word VBA Script for Shannon entropy of the alphabetic characters字母字符的香农熵的 Word VBA 脚本
【发布时间】:2014-11-28 20:14:58
【问题描述】:

我有一个文档,想要获取 Word VBA 中每个字符的香农熵。我已经计算了每个字符的香农熵,但不知道如何将这些信息放在一个单词 VBA 中并进行总结。


每个字符的香农熵


(来源: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

【问题讨论】:

  • 更多信息。你有一些代码吗?

标签: vba ms-word entropy


【解决方案1】:

嗯,据我所知,您总共有 27 个字符,每个字符都有一个预定义的值,您想简单地总结它们吗?

让我们从变量开始:

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

现在,如果您想变得更疯狂,请告诉我。例如,可以将其写入到将读取一个充满数据的表然后计算值的方式。

对于输出,它实际上取决于您希望输出显示的位置。直接在文档中显示:

ActiveDocument.Paragraphs.add
ActiveDocument.Paragraphs(ActiveDocument.Paragraphs.Count).range.text = "The Shannon entropy of the characters in this document is" & total

如果您已经有了所有答案,我更想知道为什么需要 VBA。好像一个计算器就够了?

【讨论】:

  • 我试过这个,但我只是得到 character(27) 作为答案
猜你喜欢
  • 1970-01-01
  • 2021-12-02
  • 2014-03-31
  • 2023-03-27
  • 2012-10-31
  • 1970-01-01
  • 1970-01-01
  • 2013-05-14
  • 1970-01-01
相关资源
最近更新 更多