Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. $length = $_GET['length'];
  2. $proto = $_GET['proto'];
  3. $dead = $_GET['dead'];
  4.  
  5. $dict = file("wordlist/" . $length . "_letters.txt", FILE_IGNORE_NEW_LINES);
  6.  
  7. $exp = '/^' . str_replace('*', '\w{1}', $proto) . '$/u';
  8. $words = array_filter($dict, function ($v) use ($exp, $dead) {
  9. $dw = true;
  10. foreach (array_diff(preg_split('//u', $dead), ['']) as $d) {
  11. if (preg_match('/' . $d . '/u', $v)) {
  12. $dw = false;
  13. break;
  14. };
  15. }
  16. return (preg_match($exp, $v) && $dw);
  17. });
  18.  
  19. $letters = [];
  20. foreach ($words as $v) {
  21. $imp = implode('|', preg_split('//u', str_replace('*', '', $proto)));
  22. foreach (array_diff(preg_split('//u', preg_replace('/(' . $imp . ')/u', '', $v)), ['']) as $w) {
  23. $letters[] = $w;
  24. }
  25. }
  26.  
  27. $percentageLetters = [];
  28. $count = count($words);
  29. foreach (array_unique($letters) as $v) {
  30. $c = 0;
  31. foreach ($words as $w) {
  32. if (preg_match('/' . $v . '/u', $w)) $c++;
  33. }
  34. $percentageLetters[] = ['letter' => $v, 'percent' => round(($c / $count * 100), 1) . '%'];
  35. }
  36. uasort($percentageLetters, function ($a, $b) {
  37. return ((float)$a['percent'] > (float)$b['percent']) ? -1 : 1;
  38. });
  39.  
  40. /* массив букв со 100% вероятностью */
  41. $arrPercent100 = array_values(array_filter($percentageLetters, function($data) {
  42. return ((float)$data['percent'] == 100);
  43. }));
  44. /* буква с самым большим процентом */
  45. $mostLetter = (!count($arrPercent100)) ? array_shift($percentageLetters) : [];
  46.  
  47. $ret = ['words' => $words, 'letters' => array_values($percentageLetters)];
  48. print_r($ret);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement