Crazy Talk String Funkification Using UTF-8

Written by: NetworkError, on 03-04-2009 12:55
Last update: 01-05-2009 08:45
Published in: Public, Technical Wootness
Views: 8480


I recently wrote a ASCII to UTF-8 randomizing program called Crazy Talk that would take a standard ASCII string and randomly funkify it using UTF-8 characters.

Example: Tɥis is a ŧypical střing thåŧ has ʙeeʼn Ƭunʞified uƨing Crazȳ Ʈalƙ.

I wrote it for a few reasons:

  • I wanted to make my FaceBook posts a lot more annoying.
  • I wanted to play with UTF-8 character encoding a bit more.
  • I wanted to write an OpenSearch service (and this seemed like a good candidate).

At the heart of this little program is a class that contains all the case-sensitive character mappings and such for ASCII -> UTF-8 conversion. I assembled these by hand and they are by no means complete. The class also contains methods for getting the full character map, combo character maps, setting your input string, setting the % of characters to make funky, and of course - converting your input string to a funky string.

The core class is simple in design, but rather flexible in implementation. You can implement it in a number of ways:

Note- The Open Search is the standard search box in the top-right corner of most browser clients. A lot of big-name search engines use it. You can see what Crazy Talk looks like in its Open Search personification below. For those of you interested in the Open Search standard:

Screenshot of Crazy Talk over Open Search (great for whipping out funky one liners.)

My Open Search XML:



Crazy Talk
Make your ASCII more interesting with NetworkError's Crazy Talk.




http://networkerror.org/favicon.ico

Here is the source code for my Crazy Talk class:


 **/

class crazy_talk
{

        /**
         * We need an array of valid swap-out characters for each standard ASCII character.
         * Each character is represented by it's unicode number.
         **/
        protected static $char_map = array(
                'a' => array(224,225,226,227,228,229,257,259,261,462,479,481,507,513,515,551,592,593,594,940,945),
                'A' => array(193,194,195,196,197,256,258,260,461,478,480,506,512,514,550,902,916,923),
                'b' => array(384,386,388,389,595,665),
                'B' => array(223,385,946,976),
                'c' => array(231,263,265,267,269,392,596,663,962,987),
                'C' => array(199,262,264,266,268,390,391,950,986),
                'd' => array(271,273,395,396,545),
                'D' => array(208,270,272,393,394,598,599),
                'e' => array(232,233,234,235,275,277,279,281,283,399,477,517,519,553,601,602,941,949),
                'E' => array(200,201,202,203,274,276,278,280,282,398,400,425,516,518,552,603,604,904,917,926,931,958),
                'f' => array(402,607,644,428),
                'F' => array(401,988,989),
                'g' => array(285,287,289,291,485,487,501,609,667),
                'G' => array(284,286,288,290,403,484,486,500,608),
                'h' => array(293,295,405,543,613,614,615,688,689),
                'H' => array(292,294,502,542,668),
                'i' => array(236,237,238,239,297,299,301,303,305,383,464,521,523,618,619,620,943),
                'I' => array(204,205,206,207,296,298,300,302,304,407,463,522,520,906,938,970),
                'j' => array(309,496,669,690),
                'J' => array(308),
                'k' => array(311,409,489,670,983),
                'K' => array(310,312,408,488),
                'l' => array(314,316,318,320,322,406,410,426,564,645,646),
                'L' => array(313,315,317,319,321,621),
                'm' => array(653),
                'M' => array(625),
                'n' => array(241,324,326,328,329,331,414,505,565,942),
                'N' => array(209,323,325,327,330,413,504,544,626,627,628,911,928,951),
                'o' => array(240,242,243,244,245,246,333,335,337,417,419,466,525,527,555,557,559,561,629,937,948,963,985),
                'O' => array(210,211,212,213,214,248,332,334,336,416,418,465,524,526,554,556,558,560,664,908,920,934,952,972,984),
                'p' => array(222,421,447,503,961),
                'P' => array(254,420,929),
                'q' => array(397,491,493,672),
                'Q' => array(397,490,492),
                'r' => array(341,343,345,529,531,638,640,691),
                'R' => array(340,342,344,422,528,530,636),
                's' => array(347,349,351,353,424,537),
                'S' => array(346,348,350,352,423,536),
                't' => array(355,357,359,427,429,450,539,566,616,648,964),
                'T' => array(354,356,358,430,538),
                'u' => array(249,250,251,252,361,363,365,367,369,371,432,468,470,472,474,476,533,535,649,650,651,944,965),
                'U' => array(217,218,219,220,360,362,364,366,368,370,431,433,434,467,469,471,473,475,532,534,971,973),
                'v' => array(411),
                'V' => array(221,404),
                'w' => array(373,623,624,695,969),
                'W' => array(372,412,974),
                'x' => array(215,739),
                'X' => array(935,967),
                'y' => array(253,255,375,563,696),
                'Y' => array(374,376,435,436,562,611,655,910,936,939,978,979,980),
                'z' => array(378,380,382,438,549,656,657,991),
                'Z' => array(377,379,381,437,548,990),
                '0' => array(216,248,415,510,511,632,977,981),
                //'1' => array(),
                '2' => array(443),
                '3' => array(439,440,441,442,494,495,540,541,658,960),
                //'4' => array(),
                '5' => array(444,445),
                '6' => array(597),
                //'7' => array(),
                '8' => array(546,547),
                //'9' => array(),
                '-' => array(247),
                '?' => array(660,661,662,673,674),
        );

        /**
         * There are some nifty combo chars we can use to replace things like "AE".
         **/
        protected static $combo_map = array(
                'ae' => array(198,230,482,483,508,509),
                'ij' => array(306,307),
                'ce' => array(338,339),
                'dz' => array(452,453,454,675,676,677),
                'lj' => array(455,456,457),
                'nj' => array(458,459,460),
                'dz' => array(497,498,499),
                '13' => array(622),
                'ce' => array(630),
                'ts' => array(678),
                'tf' => array(679),
                'fn' => array(681),
                'ls' => array(682),
        );

        /**
         * Out input string.
         * @var string
         **/
        protected $string;

        /**
         * What percentage of characters should we funkify?
         * @var int
         **/
        protected $percent = 50;

        /**
         * Set the string to be converted.
         * @param string $string        Input string to be converted.
         *
         * @return void
         **/
        public function setString($string)
        {
                $this->string = $string;
        }

        /**
         * Get the char_map array.
         *
         * @return array        char map (array of chars => array(int, int, int))
         **/
        public function getCharMap()
        {
                return self::$char_map;
        }

        /**
         * Get the combo_map array.
         *
         * @return array        The map of character combos to utf-8 ints.
         **/
        public function getComboMap()
        {
                return self::$combo_map;
        }

        /**
         * Set the % of funky characters you want in your output string.
         * @param int $percent
         *
         * @return void
         **/
        public function setFunkyPercentage($percent)
        {
                // Sanitize.
                $percent = (int)$percent;
                if ($percent > 100) {
                        $percent = 100;
                } elseif ($percent < 0) {
                        $percent = 0;
                }

                $this->percent = $percent;
        }

        /**
         * Converts you string.  If you specify a string here, it will pass it to setString().
         * @param string [$string]      Input string to be converte.
         *
         * return string                Your crazy string.
         **/
        public function convertString($string = null)
        {
                if ($string != null) {
                        $this->setString($string);
                }

                // Setup working string.
                $string = $this->string;

                // First, look for character combinations to replace.
                foreach (self::$combo_map as $pair => $chars) {
                        // While there are pairs to be replaced, randomly replace them.
                        // (Must case $pair to string so we don't replace carriage returns (ascii 13)).
                        while (($location = stripos($string, (string)$pair)) !== false) {
                                $rand_char = $chars[(array_rand($chars))];

                                // Splice.
                                $temp = substr($string, 0, $location);
                                $temp .= html_entity_decode('&#'.$rand_char.';', ENT_NOQUOTES, 'UTF-8');
                                $temp .= substr($string, ($location + strlen($pair)));
                                $string = $temp;
                        }
                }

                // Next, go through the string 1 character at a time and randomly replace chars.
                for ($i = 0, $len = strlen($string), $new_string = ''; $i < $len; $i++) {
                        $char = $string[$i];
                        // Check percentage chance and existince of replacement characters.
                        if (rand(1, 100) < $this->percent && array_key_exists($char, self::$char_map)) {
                                $new_char = self::$char_map[$char][(array_rand(self::$char_map[$char]))];
                                $new_char = html_entity_decode('&#'.$new_char.';', ENT_NOQUOTES, 'UTF-8');
                                $new_string .= $new_char;
                        } else {
                                $new_string .= $char;
                        }
                }

                return $new_string;
        }

}

Read more... Be first to comment this article   |   Print   |   Send to friend

Print