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, '     probability: ', $res =substr ($gai, 0,6);
}
$bla =0;
Echo ' </br> ', '     ' number of spaces: ', Substr_count ($String, ""), '   & 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