Advertisement
Guest User

Pronoun generator

a guest
Sep 17th, 2019
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.08 KB | None | 0 0
  1. #!/usr/bin/php
  2. <?php
  3.  
  4. $stems = [
  5.     'b' => 2,
  6.     'br' => 2,
  7.     'c' => 3,
  8.     'ch' => 4,
  9.     'cl' => 2,
  10.     'd' => 2,
  11.     'dr' => 1,
  12.     'f' => 2,
  13.     'g' => 1,
  14.     'gh' => 3,
  15.     'h' => 5,
  16.     'j' => 1,
  17.     'jh' => 1,
  18.     'k' => 2,
  19.     'kl' => 2,
  20.     'l' => 1,
  21.     'm' => 2,
  22.     'mn' => 1,
  23.     'n' => 2,
  24.     'ng' => 1,
  25.     'p' => 1,
  26.     'q' => 1,
  27.     'r' => 2,
  28.     's' => 4,
  29.     'sc' => 1,
  30.     'sh' => 3,
  31.     'sp' => 2,
  32.     'st' => 3,
  33.     't' => 4,
  34.     'th' => 4,
  35.     'w' => 1,
  36.     'wr' => 1,
  37.     'x' => 4,
  38.     'xh' => 1,
  39.     'y' => 2,
  40.     'z' => 5,
  41.     'zh' => 5,
  42. ];
  43.  
  44. $vowels = [
  45.     'a' => 3,
  46.     'ae' => 2,
  47.     'ai' => 2,
  48.     'ao' => 1,
  49.     'au' => 1,
  50.     'ay' => 1,
  51.     'e' => 4,
  52.     'ea' => 1,
  53.     'ei' => 1,
  54.     'eo' => 1,
  55.     'eu' => 2,
  56.     'ey' => 1,
  57.     'i' => 3,
  58.     'ia' => 1,
  59.     'ie' => 1,
  60.     'io' => 1,
  61.     'iu' => 1,
  62.     'o' => 3,
  63.     'oa' => 1,
  64.     'oe' => 1,
  65.     'oi' => 1,
  66.     'ou' => 2,
  67.     'oy' => 2,
  68.     'u' => 2,
  69.     'ua' => 1,
  70.     'ui' => 1,
  71.     'uo' => 1,
  72.     'y' => 2,
  73.     'ya' => 1,
  74.     'ye' => 1,
  75.     'yi' => 1,
  76.     'yo' => 1,
  77.     'yu' => 1,
  78. ];
  79.  
  80. $altVowelChance = 30;
  81.  
  82. $prefixes = [
  83.     '' => 100,
  84.     'b' => 8,
  85.     'd' => 10,
  86.     'g' => 10,
  87.     'k' => 10,
  88.     'm' => 6,
  89.     'n' => 6,
  90.     's' => 10,
  91.     't' => 10,
  92.     'z' => 10,
  93. ];
  94.  
  95. $subjectiveSuffixes = [
  96.     '' => 100,
  97.     'h' => 10,
  98.     'n' => 5,
  99.     'w' => 10,
  100.     'y' => 20,
  101. ];
  102.  
  103. $objectiveSuffixes = [
  104.     '' => 1,
  105.     'b' => 1,
  106.     'j' => 5,
  107.     'l' => 2,
  108.     'm' => 10,
  109.     'n' => 10,
  110.     'p' => 2,
  111.     'r' => 10,
  112.     't' => 2,
  113.     'v' => 1,
  114.     'w' => 1,
  115.     'x' => 1,
  116.     'z' => 2,
  117. ];
  118.  
  119. $possessiveSuffixes = [
  120.     'l' => 2,
  121.     'm' => 3,
  122.     'mn' => 2,
  123.     'n' => 3,
  124.     'r' => 10,
  125.     'rl' => 4,
  126.     's' => 10,
  127.     'sz' => 7,
  128.     'v' => 3,
  129.     'w' => 2,
  130.     'z' => 10,
  131. ];
  132.  
  133. $cull = [
  134.     'kc',
  135.     'kd',
  136.     'uy',
  137.     'wyi',
  138.     'yy',
  139.     'sx',
  140.     'zx',
  141. ];
  142.  
  143. do {
  144.     $stem = r($stems);
  145.     $mainVowel = r($vowels);
  146.  
  147.     $vowel = (rand(1, 100) <= $altVowelChance) ? r($vowels) : $mainVowel;
  148.     $subjective = $stem . $vowel;
  149.     $suffix = r($subjectiveSuffixes);
  150.     if ($suffix && !endsWith($subjective, $suffix)) {
  151.         $subjective .= $suffix;
  152.     }
  153.     if (strlen($stem) === 1) {
  154.         $prefix = r($prefixes);
  155.         if ($prefix && $prefix !== $stem) {
  156.             $subjective = $prefix . $subjective;
  157.         }
  158.     }
  159.  
  160.     $vowel = (rand(1, 100) <= $altVowelChance) ? r($vowels) : $mainVowel;
  161.     $objective = $stem . $vowel;
  162.     $suffix = r($objectiveSuffixes);
  163.     if ($suffix && !endsWith($objective, $suffix)) {
  164.         $objective .= $suffix;
  165.     }
  166.     if (strlen($stem) === 1) {
  167.         $prefix = r($prefixes);
  168.         if ($prefix && $prefix !== $stem) {
  169.             $objective = $prefix . $objective;
  170.         }
  171.     }
  172.  
  173.     $vowel = (rand(1, 100) <= $altVowelChance) ? r($vowels) : $mainVowel;
  174.     $possessive = $stem . $vowel;
  175.     $suffix = r($possessiveSuffixes);
  176.     if ($suffix && !endsWith($possessive, $suffix)) {
  177.         $possessive .= $suffix;
  178.     }
  179.     if (strlen($stem) === 1) {
  180.         $prefix = r($prefixes);
  181.         if ($prefix && $prefix !== $stem) {
  182.             $possessive = $prefix . $possessive;
  183.         }
  184.     }
  185.  
  186. } while(
  187.     $subjective === $objective
  188.     || $subjective === $possessive
  189.     || $objective === $possessive
  190.     || has($subjective, $cull)
  191.     || has($objective, $cull)
  192.     || has($possessive, $cull)
  193. );
  194.  
  195. echo "$subjective / $objective / $possessive\n";
  196.  
  197. function r(array $l) {
  198.     $total = 0;
  199.     foreach ($l as $w) {
  200.         $total += $w;
  201.     }
  202.     $s = rand(0, $total);
  203.     $p = 0;
  204.     foreach ($l as $v => $w) {
  205.         $p += $w;
  206.         if ($p >= $s) {
  207.             return $v;
  208.         }
  209.     }
  210.     throw new Exception('should be unreachable');
  211. }
  212.  
  213. function endsWith(string $s, string $e) {
  214.     return substr($s, strlen($s) - strlen($e), strlen($e)) === $e;
  215. }
  216.  
  217. function has(string $s, array $l) {
  218.     foreach ($l as $m) {
  219.         if (strpos($s, $m) !== false) {
  220.             return true;
  221.         }
  222.     }
  223.     return false;
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement