-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from miamibc/flip-plugin
Flip plugin
- Loading branch information
Showing
6 changed files
with
188 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?php | ||
/** | ||
* Flip plugin for Joker | ||
* | ||
* Flips text upside-down and back | ||
* | ||
* @package joker-telegram-bot | ||
* @author Sergei Miami <[email protected]> | ||
*/ | ||
|
||
namespace Joker\Plugin; | ||
|
||
use Joker\Parser\Update; | ||
|
||
class Flip extends Base | ||
{ | ||
|
||
private static $table = [ | ||
"a" => "\u{0250}", | ||
"b" => "q", | ||
"c" => "\u{0254}", | ||
"d" => "p", | ||
"e" => "\u{01DD}", | ||
"f" => "\u{025F}", | ||
"g" => "\u{0183}", | ||
"h" => "\u{0265}", | ||
"i" => "\u{0131}", | ||
"j" => "\u{027E}", | ||
"k" => "\u{029E}", | ||
//l : '\u0283", | ||
"m" => "\u{026F}", | ||
"M" => "W", | ||
"n" => "u", | ||
"r" => "\u{0279}", | ||
"t" => "\u{0287}", | ||
"v" => "\u{028C}", | ||
"w" => "\u{028D}", | ||
"y" => "\u{028E}", | ||
"." => "\u{02D9}", | ||
"[" => "]", | ||
"(" => ")", | ||
"{" => "}", | ||
"?" => "\u{00BF}", | ||
"!" => "\u{00A1}", | ||
"'" => ",", | ||
"<" => ">", | ||
"_" => "\u{203E}", | ||
"\u203F" => "\u{2040}", | ||
"\u2045" => "\u{2046}", | ||
"\u2234" => "\u{2235}", | ||
"\r" => "\n", | ||
"а" => "ɐ", | ||
"б" => "ƍ", | ||
"в" => "ʚ", | ||
"г" => "ɹ", | ||
"д" => "ɓ", | ||
"ё" => "ǝ", | ||
"е" => "ǝ", | ||
"ж" => "ж", | ||
"з" => "ε", | ||
"и" => "и", | ||
"й" => "ņ", | ||
"к" => "ʞ", | ||
"л" => "v", | ||
"м" => "w", | ||
"н" => "н", | ||
"о" => "о", | ||
"п" => "u", | ||
"р" => "d", | ||
"с" => "ɔ", | ||
"т" => "ɯ", // ʟ ɯ ᅩ | ||
"у" => "ʎ", | ||
"ф" => "ф", | ||
"х" => "х", | ||
"ц" => "ǹ", | ||
"ч" => "Һ", | ||
"ш" => "m", | ||
"щ" => "m", | ||
"ъ" => "q", | ||
"ы" => "ıq", | ||
"ь" => "q", | ||
"э" => "є", | ||
"ю" => "oı", | ||
"я" => "ʁ", | ||
]; | ||
|
||
public function onPublicText( Update $update ) | ||
{ | ||
if ($update->message()->text()->trigger() === 'flip') | ||
{ | ||
$text = $update->message()->text()->token(1); | ||
$update->replyMessage( self::flip($text)); | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Flip text | ||
* @param string $text | ||
* | ||
* @return string | ||
*/ | ||
public static function flip($text) : string | ||
{ | ||
// split unicode string | ||
$text = preg_split('//u', mb_strtolower( $text )); | ||
|
||
// flip with normal table | ||
$result1 = array_map(function ($char){ | ||
return strtr($char, self::$table); | ||
}, $text ); | ||
|
||
// flip with reversed+flipped table | ||
$result2 = array_map(function ($char){ | ||
return strtr($char, array_reverse(array_flip(self::$table))); | ||
}, $text ); | ||
|
||
// find best result | ||
$best = count( array_diff($text, $result1) ) >= count(array_diff($text, $result2)) ? $result1 : $result2; | ||
|
||
// reverse and return sting | ||
return implode("", array_reverse($best)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
/** | ||
* | ||
* @package joker-telegram-bot | ||
* @author Sergei Miami <[email protected]> | ||
*/ | ||
|
||
namespace Tests\Plugin; | ||
|
||
use Joker\Plugin\Flip; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class FlipTest extends TestCase | ||
{ | ||
|
||
public function testFlip() | ||
{ | ||
$this->assertSame("ʁɔvʎнdǝʚǝdǝu dиw", Flip::flip("мир перевернулся")); | ||
$this->assertSame("мир перевернулся", Flip::flip("ʁɔvʎнdǝʚǝdǝu dиw")); | ||
|
||
$this->assertSame("˙ʇ,uop uoʎ ʇɐɥʍ uɹɐǝl ˙uɐɔ uoʎ ʇɐɥʍ ʍoɥs", Flip::flip("Show what you can. Learn what you don't.")); | ||
$this->assertSame("show whаt уoп сап. lеагп whаt уoп doп't.", Flip::flip("˙ʇ,uop uoʎ ʇɐɥʍ uɹɐǝl ˙uɐɔ uoʎ ʇɐɥʍ ʍoɥs")); | ||
|
||
// @TODO don't lowercase | ||
$this->assertSame('ʁɔvʎнdǝʚǝdǝu dиw', Flip::flip('Мир перевернулся')); | ||
|
||
|
||
} | ||
} |