diff --git a/index.php b/index.php new file mode 100644 index 0000000..d7c6140 --- /dev/null +++ b/index.php @@ -0,0 +1,170 @@ +0) { + $end--; + } + return array( + 'prefix' => substr($text, 0, $start), + 'word' => substr($text, $start, $end - $start + 1), + 'suffix' => substr($text, $end + 1, strlen($text) - $end) + ); + } + + static protected function isLikelyPigLatin($text) { + // Gives a number between 0 and 1 on the likeliness + // of the text being pig latin + mb_internal_encoding('UTF-8'); + $upper = mb_strtoupper($text); + $words = explode(' ', $upper); + $totalwords = count($words); + $count = 0; + foreach ($words as $word) { + $split = self::splitWord($word); + if (mb_substr($split['word'], -2) == self::PigLatinSuffix) { + $count++; + } + } + return $count / $totalwords; + } + + static protected function getWordCaseType($text) { + // Helpful function to deal with different cases of text + mb_internal_encoding('UTF-8'); + $words = explode(' ', $text); + $firstword = $words[0]; + if ($firstword == mb_strtoupper($firstword)) { + return self::WordUppercase; + } + if ($firstword == mb_strtolower($firstword)) { + return self::WordLowercase; + } + if ($firstword == ucfirst($firstword)) { + return self::WordPropercase; + } + return self::WordUnknowncase; + } + + static protected function encodeWord($text) { + $split = self::splitWord($text); + $word = $split['word']; + $isconstant = self::isConstant($word); + if ($isconstant > 0) { + switch (self::getWordCaseType($word)) { + case self::WordUppercase: + $translated = mb_substr($word, $isconstant) . mb_substr($word, 0, $isconstant) . mb_strtoupper(self::PigLatinSuffix); + break; + case self::WordPropercase: + $lowerword = mb_strtolower($word); + $translated = ucfirst(mb_substr($lowerword, $isconstant)) . mb_substr($lowerword, 0, $isconstant) . mb_strtolower(self::PigLatinSuffix); + break; + default: + $lowerword = mb_strtolower($word); + $translated = mb_substr($lowerword, $isconstant) . mb_substr($lowerword, 0, $isconstant) . mb_strtolower(self::PigLatinSuffix); + break; + } + } else { + $translated = $word . mb_strtolower(self::PigLatinSuffix); + } + return $split['prefix'] . $translated . $split['suffix']; + } + + static public function encodeText($text) { + mb_internal_encoding('UTF-8'); + $words = explode(' ', $text); + $translated = ''; + foreach ($words as $word) { + if ($translated != '') { + $translated .= ' '; + } + $translated .= self::encodeWord($word); + } + return $translated; + } + + static public function decodeText($text) { + // Might not have started this project if I took + // the time to realise this is theoretically impossible + } + + static public function autocodeText($text) { + if (self::isLikelyPigLatin($text) >= self::PigLatinLikeliness) { + return self::decodeText($text); + } else { + return self::encodeText($text); + } + } + +} + +$text = 'This is a really \'good\' piece of text to start with. 12eyes34'; +if (isset($_POST['text'])) { + $text = PigLatin::autocodeText($_POST['text']); +} +?> + + + Piglatin translator + + +
+ +

+
+ +