fork(1) download
  1. <?php
  2.  
  3. class AmericanBritishSpellings {
  4. public function __construct($args) {
  5. return TRUE;
  6. }
  7. public function SwapBritishSpellingsForAmericanSpellings($args) {
  8. $text = $args['text'];
  9.  
  10. $spelling_alternatives = $this->GetSpellingsAndReplacements(['language'=>'american']);
  11.  
  12. $american_spellings = $spelling_alternatives['american'];
  13. $british_spellings = $spelling_alternatives['british'];
  14.  
  15. $text = preg_replace($british_spellings, $american_spellings, $text);
  16.  
  17. return $text;
  18. }
  19.  
  20. public function SwapAmericanSpellingsForBritishSpellings($args) {
  21. $text = $args['text'];
  22.  
  23. $spelling_alternatives = $this->GetSpellingsAndReplacements(['language'=>'british']);
  24.  
  25. $american_spellings = $spelling_alternatives['american'];
  26. $british_spellings = $spelling_alternatives['british'];
  27.  
  28. $text = preg_replace($american_spellings, $british_spellings, $text);
  29.  
  30. return $text;
  31. }
  32.  
  33. public function GetSpellingsAndReplacements($args) {
  34. $language = $args['language'];
  35.  
  36. $replacements = $this->BuildSpellingReplacements();
  37.  
  38. $american_spellings = $replacements['american'];
  39. $british_spellings = $replacements['british'];
  40.  
  41. $alternates = $this->BuildSpellingAlternates([
  42. 'language'=>$language,
  43. 'american'=>$american_spellings,
  44. 'british'=>$british_spellings,
  45. ]);
  46.  
  47. $american_spellings = $alternates['american'];
  48. $british_spellings = $alternates['british'];
  49.  
  50. return [
  51. 'american'=>$american_spellings,
  52. 'british'=>$british_spellings,
  53. ];
  54. }
  55. public function BuildSpellingAlternates($args) {
  56. $language = $args['language'];
  57. $american_spellings = $args['american'];
  58. $british_spellings = $args['british'];
  59.  
  60. $american_spellings = $this->BuildSpellingAlternatesForLanguage(['spellings'=>$american_spellings]);
  61. $british_spellings = $this->BuildSpellingAlternatesForLanguage(['spellings'=>$british_spellings]);
  62.  
  63. if($language == 'american') {
  64. $british_spellings = $this->BuildSearchRegex(['spellings'=>$british_spellings]);
  65. } elseif($language == 'british') {
  66. $american_spellings = $this->BuildSearchRegex(['spellings'=>$american_spellings]);
  67. }
  68.  
  69. return [
  70. 'american'=>$american_spellings,
  71. 'british'=>$british_spellings,
  72. ];
  73. }
  74.  
  75. public function BuildSpellingAlternatesForLanguage($args) {
  76. $spellings = $args['spellings'];
  77.  
  78. $spellings_uppercase = array_map('strtoupper', $spellings);
  79. $spellings_ucfirst = array_map('ucfirst', $spellings);
  80.  
  81. $spellings_and_alternates = [];
  82.  
  83. foreach([$spellings, $spellings_uppercase, $spellings_ucfirst] as $spelling_group) {
  84. foreach($spelling_group as $spelling) {
  85. $spellings_and_alternates[] = $spelling;
  86. }
  87. }
  88.  
  89. return $spellings_and_alternates;
  90. }
  91.  
  92. public function BuildSearchRegex($args) {
  93. $spellings = $args['spellings'];
  94.  
  95. $spellings_count = count($spellings);
  96.  
  97. $spellings = array_map([$this, 'BuildSearchRegexForWord'], $spellings);
  98.  
  99. return $spellings;
  100. }
  101.  
  102. public function BuildSearchRegexForWord($args) {
  103. $text = $args;
  104.  
  105. return '/\b' . $text . '\b/';
  106. }
  107.  
  108. public function BuildSpellingReplacements() {
  109. $spelling_alternatives = $this->GetAmericanToBritishSpellings();
  110.  
  111. $american_spellings = array_keys($spelling_alternatives);
  112. $british_spellings = array_values($spelling_alternatives);
  113.  
  114. $british_spellings_count = count($british_spellings);
  115.  
  116. for($i = 0; $i < $british_spellings_count; $i++) {
  117. $british_spelling = $british_spellings[$i];
  118. if(is_array($british_spelling)) {
  119. $american_spelling = $american_spellings[$i];
  120.  
  121. foreach($british_spelling as $british_spelling_item) {
  122. $british_spellings[] = $british_spelling_item;
  123. $american_spellings[] = $american_spelling;
  124. }
  125.  
  126. unset($american_spellings[$i]);
  127. unset($british_spellings[$i]);
  128. }
  129. }
  130.  
  131. return [
  132. 'american'=>$american_spellings,
  133. 'british'=>$british_spellings,
  134. ];
  135. }
  136.  
  137.  
  138. public function GetAmericanToBritishSpellings() {
  139. // Sources:
  140. //
  141. // 8,000 Words: http://w...content-available-to-author-only...o.uk/docs/Words-Worldwide-Word-list-UK-US-2009.doc
  142. // 18,000 Words https://g...content-available-to-author-only...b.com/en-wl/wordlist/blob/master/varcon/varcon.txt
  143. //
  144. // These sources did more than provide the total sum of words. They cross-checked each other and found errors.
  145. //
  146. // Total After Removing Duplicates: 20,000
  147. return [
  148. 'labor'=>'labour',
  149. 'laborer'=>['labourer', 'labouerer',],
  150. 'laborers'=>['labourers', 'labouerers',],
  151. ];
  152. }
  153. }
  154.  
  155. $abs = new AmericanBritishSpellings([]);
  156.  
  157. $text = 'labour, as a Labourer, in the laborite\'s labourine cavern with my fellow LABOURERS';
  158.  
  159. $americanized = $abs->SwapBritishSpellingsForAmericanSpellings(['text'=>$text]);
  160. $britishized = $abs->SwapAmericanSpellingsForBritishSpellings(['text'=>$americanized]);
  161. print("American - Test : " . $americanized);
  162. print("\n");
  163. print("British - Test : " . $britishized);
  164. ?>
Success #stdin #stdout 0s 82624KB
stdin
Standard input is empty
stdout
American - Test : labor, as a Laborer, in the laborite's labourine cavern with my fellow LABORERS
British - Test : labour, as a Labourer, in the laborite's labourine cavern with my fellow LABOURERS