Simple calculation of source entropy with PHP

Source: Internet
Author: User

Requirements: For each of the source count of each letter and the number of spaces appear, probability, source entropy!

first of all in the brain to conceive of the editing process to use the specific functions of function and so on, and then draw on their own paper now, good according to the source entropy formula
E (X) =-p (xi) Log2 P (xi) (i=1,2,.. N) The following encoding process can be obtained!
<?php
//The given letter
$String = ' qwertyuioplkjhgfdsazxcvbnmdgjdoqoiaznicguyfgvfpqopq llxkzmjscnjd nvhfhuwrty ';
//need to match 26 letters in English
$ZhiMu = ' abcdefghijklmnopqrstuvwxyz ';
//Match letters
Preg_match_all ("/[a-za-z]{1}/", $String, $arrAl);
$p =0;
$xin =0;
Substr_count ($String, ""); Number of spaces
for ($i = 0; $i < strlen ($ZhiMu); $i + +) {//Loop through 26 letters, that is, one letter to the other
$num =0;
$s = $ZhiMu {$i}; Get the letters that are right
$b = 0; Initialize the number of letters to 0
$gai = 0; Initialization probability is 0
$res =0;
count ($arrAl [0]);
For ($j = 0; $j < strlen ($String); $j + +) {//loops through the string to be compared, that is, a comparison from the beginning
If ($s = = $String {$j}) {//If two characters are the same
$b + = 1; Number plus 1
        }
    }
$num =count ($arrAl [0]) +substr_count ($String, "");
$gai = $b/$num; The probability of each letter appearing
//Output Results
Echo ' </br> ', $ZhiMu {$i}, ' The number of occurrences is: ', $b, ' &nbsp&nbsp&nbsp probability: ', $res =substr ($gai, 0,6);
}
$bla =0;
Echo ' </br> ', ' &nbsp&nbsp&nbsp ' number of spaces: ', Substr_count ($String, ""), ' &nbsp&nbsp& nbsp Probability: ', substr (Substr_count ($String, "")/$num, 0,6);
$bla =substr (Substr_count ($String, "")/$num, 0,6);
$p =-(($gai) * (log ($gai)/log (2)) +log ($bla)/log (2));
Echo ' </br> ', "sum of letters and spaces:", $num, "<br/>", "Source entropy: '," E (X) = ", $xin =substr ($p, 0,6);
Programming Pictures:
    650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M00/82/93/wKioL1db1MOTFLHrAABwiJjzZHs881.png-wh_500x0-wm_3 -wmp_4-s_3145086664.png "title=" qq picture 20160611164111.png "alt=" Wkiol1db1motflhraabwijjzzhs881.png-wh_50 "/>

Results:

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M01/82/95/wKiom1db093DI_ciAABCBbm9ZsM304.png-wh_500x0-wm_3 -wmp_4-s_3560060691.png "title=" 123.png "alt=" Wkiom1db093di_ciaabcbbm9zsm304.png-wh_50 "/>

The above is a simple program, you can also join the database, punctuation, kanji, special characters to calculate, can also be set to form input!
My demo is here, what is not enough to ask you!

This article is from the "My First Internship" blog, please make sure to keep this source http://10314260.blog.51cto.com/10304260/1787952

Simple calculation of source entropy with PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.