{"id":2148,"date":"2022-06-08T07:41:36","date_gmt":"2022-06-08T11:41:36","guid":{"rendered":"https:\/\/www.michelecoscia.com\/?p=2148"},"modified":"2022-06-08T07:41:36","modified_gmt":"2022-06-08T11:41:36","slug":"give-me-some-space-or-the-slow-descent-into-unicode-madness","status":"publish","type":"post","link":"https:\/\/www.michelecoscia.com\/?p=2148","title":{"rendered":"Give Me Some Space!, or: the Slow Descent into Unicode Madness"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><a href=\"http:\/\/www.claravdw.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">My wife<\/a> is the literal embodiment of this comic strip:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/imgs.xkcd.com\/comics\/laptop_issues.png\" alt=\"Laptop Issues\" width=\"577\" height=\"228\" title=\"Hang on, we got a call from the feds. They say we can do whatever with him, but the EPA doesn't want that laptop in the ocean. They're sending a team.\"\/><figcaption>Source: https:\/\/xkcd.com\/2083\/<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">She has to overcome the weirdest <a rel=\"noreferrer noopener\" href=\"https:\/\/en.wikipedia.org\/wiki\/Code_golf\" target=\"_blank\">code golfing<\/a> issues to get through her research. The other day her issue was: &#8220;I have a bunch of text and I want to surround every unicode emoticon with spaces&#8221; &#8212; the idea being to treat each emoji like a full word.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Sounds easy enough until you get to the word &#8220;unicode&#8221; and you start hearing helicopters in the background, a Doors song comes on, and you flash memories of going upstream in a jungle river on a barge.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>My<\/strong> problem is that, instead, I am the embodiment of the victim in <strong>this<\/strong> comic strip:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/imgs.xkcd.com\/comics\/nerd_sniping.png\" alt=\"Nerd Sniping\" width=\"642\" height=\"322\" title=\"I first saw this problem on the Google Labs Aptitude Test.  A professor and I filled a blackboard without getting anywhere.  Have fun.\"\/><figcaption>Source: https:\/\/xkcd.com\/356\/<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">When confronted with something seemingly simple but with an interesting level of complexity behind it, I just have to drop everything I&#8217;m doing for my actual job for an hour, until I solve it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">At first glance, it seems easy enough: find a dictionary of emoticons and use it as the basis of a regular expression. The <a href=\"https:\/\/pypi.org\/project\/emoji\/\" target=\"_blank\" rel=\"noreferrer noopener\">emoji<\/a> package can take care of the first part. A naive solution could be the following:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import re\nfrom emoji import unicode_codes\n\nallemojis = \"\".join(unicode_codes.UNICODE_EMOJI['en'])\nsearcher = re.compile(u'([%s])' % allemojis)\nspacemoji = {key: searcher.sub(r' \\1 ', texts[key]) for key in texts}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This assumes that &#8220;<code>texts<\/code>&#8221; is a dictionary with a collection of texts we&#8217;re interested in. The &#8220;<code>searcher<\/code>&#8221; wraps a bunch of characters in between square brackets, which means that any matching characters will be found. The the &#8220;<code>.sub<\/code>&#8221; method will replace whatever matched (&#8220;<code>\\1<\/code>&#8220;) with its content surrounded by spaces.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Easy enough. Let&#8217;s test it with some example strings:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.michelecoscia.com\/wp-content\/uploads\/2022\/06\/image.png\"><img loading=\"lazy\" decoding=\"async\" width=\"461\" height=\"39\" src=\"https:\/\/www.michelecoscia.com\/wp-content\/uploads\/2022\/06\/image.png\" alt=\"\" class=\"wp-image-2150\" srcset=\"https:\/\/www.michelecoscia.com\/wp-content\/uploads\/2022\/06\/image.png 461w, https:\/\/www.michelecoscia.com\/wp-content\/uploads\/2022\/06\/image-300x25.png 300w\" sizes=\"auto, (max-width: 461px) 85vw, 461px\" \/><\/a><figcaption>Wonderful<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.michelecoscia.com\/wp-content\/uploads\/2022\/06\/image-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"459\" height=\"38\" src=\"https:\/\/www.michelecoscia.com\/wp-content\/uploads\/2022\/06\/image-1.png\" alt=\"\" class=\"wp-image-2151\" srcset=\"https:\/\/www.michelecoscia.com\/wp-content\/uploads\/2022\/06\/image-1.png 459w, https:\/\/www.michelecoscia.com\/wp-content\/uploads\/2022\/06\/image-1-300x25.png 300w\" sizes=\"auto, (max-width: 459px) 85vw, 459px\" \/><\/a><figcaption>Everything works as it should and is awesome<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.michelecoscia.com\/wp-content\/uploads\/2022\/06\/image-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"457\" height=\"39\" src=\"https:\/\/www.michelecoscia.com\/wp-content\/uploads\/2022\/06\/image-2.png\" alt=\"\" class=\"wp-image-2152\" srcset=\"https:\/\/www.michelecoscia.com\/wp-content\/uploads\/2022\/06\/image-2.png 457w, https:\/\/www.michelecoscia.com\/wp-content\/uploads\/2022\/06\/image-2-300x26.png 300w\" sizes=\"auto, (max-width: 457px) 85vw, 457px\" \/><\/a><figcaption>Say what now?<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.michelecoscia.com\/wp-content\/uploads\/2022\/06\/image-3.png\"><img loading=\"lazy\" decoding=\"async\" width=\"455\" height=\"37\" src=\"https:\/\/www.michelecoscia.com\/wp-content\/uploads\/2022\/06\/image-3.png\" alt=\"\" class=\"wp-image-2153\" srcset=\"https:\/\/www.michelecoscia.com\/wp-content\/uploads\/2022\/06\/image-3.png 455w, https:\/\/www.michelecoscia.com\/wp-content\/uploads\/2022\/06\/image-3-300x24.png 300w\" sizes=\"auto, (max-width: 455px) 85vw, 455px\" \/><\/a><figcaption>I wonder if they still hire at McDonald&#8217;s<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">A passing knowledge of <a href=\"https:\/\/en.wikipedia.org\/wiki\/Combining_character\" target=\"_blank\" rel=\"noreferrer noopener\">unicode<\/a>, or a quick Google search about the mysterious <a href=\"https:\/\/emojipedia.org\/zero-width-joiner\/\" target=\"_blank\" rel=\"noreferrer noopener\">\\u200d<\/a> code popping out of nowhere in example #3, leads to a relatively quick diagnosis. Emoticons are not single characters: they can combine multiple unicode characters to modify the appearance of a single glyph. In my case, the baseline turban emoticon is of a male with the baseline yellow emoticon skin tone. To obtain a white lady with a turban you need to combine the turban emoticon with the white color and the woman symbol.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Same goes for numbers: some emoticons contain raw digit characters, and thus those will match even when not &#8220;inside&#8221; an emoticon.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So here&#8217;s a step-by-step cure for our unicode woes:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Don&#8217;t work with string or unicode string objects. Always work with <a rel=\"noreferrer noopener\" href=\"https:\/\/en.wiktionary.org\/wiki\/bytestring\" target=\"_blank\">bytestrings<\/a> by calling &#8220;<code>.encode(\"utf-8\")<\/code>&#8220;. This way you can see what the machine actually sees. It&#8217;s not &#8220;<img loading=\"lazy\" decoding=\"async\" width=\"20\" height=\"20\" class=\"wp-image-2154\" style=\"width: 20px;\" src=\"https:\/\/www.michelecoscia.com\/wp-content\/uploads\/2022\/06\/image-4.png\" alt=\"\">&#8220;. it&#8217;s &#8220;<code>\\xf0\\x9f\\x91\\xb3\\xf0\\x9f\\x8f\\xbb\\xe2\\x80\\x8d\\xe2\\x99\\x80<\/code>&#8221; (easy, right?).<\/li><li>Don&#8217;t use square brackets for your regular expression, because it will only match one character. Emoticons aren&#8217;t characters, they are words. Use the pipe, which allows for matching groups of characters.<\/li><li>Store your emoticons in a list and sort by descending length. The regular expression will stop at the first match, and <img loading=\"lazy\" decoding=\"async\" width=\"20\" height=\"20\" class=\"wp-image-2154\" style=\"width: 20px;\" src=\"https:\/\/www.michelecoscia.com\/wp-content\/uploads\/2022\/06\/image-4.png\" alt=\"\"> is longer than <img loading=\"lazy\" decoding=\"async\" width=\"20\" height=\"20\" class=\"wp-image-2155\" style=\"width: 20px;\" src=\"https:\/\/www.michelecoscia.com\/wp-content\/uploads\/2022\/06\/image-5.png\" alt=\"\">, because the first one is a modified version. The simple turban emoticon is actually &#8220;<code>\\xf0\\x9f\\x91\\xb3<\/code>&#8221; (note how these are the first four bytes of the previous emoticon). So the regular expression will not match the &#8220;man with a turban&#8221; inside the &#8220;white wo<em>man with a turban<\/em>&#8220;.<\/li><li>Escape your regular expression&#8217;s special characters. Some emoticons contain the raw character &#8220;<code>*<\/code>&#8220;, which will make your compiler scream offensive things at you.<\/li><li>Remember to encode your input text and then to decode your output, so that you obtain back a string and not a byte sequence &#8212; assuming you&#8217;re one of those basic people who want to be able to read the outputs of their pile of code.<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">If we put all of this together, here&#8217;s the result of my hour of swearing at the screen in Italian &#x1f90c;:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import re\nfrom emoji import unicode_codes\n\nallemojis = [x.encode(\"utf-8\") for x in unicode_codes.UNICODE_EMOJI['en']]\nallemojis = sorted(allemojis, key = len, reverse = True)\nallemojis = b\"|\".join(allemojis)\n\nsearcher = re.compile(b'(%s)' % allemojis.replace(b'*', b'\\*'))\nspacemoji = {key: searcher.sub(rb' \\1 ', texts[key].encode(\"utf-8\")).decode(\"utf-8\") for key in texts}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Which restores some of my sanity:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.michelecoscia.com\/wp-content\/uploads\/2022\/06\/image-6.png\"><img loading=\"lazy\" decoding=\"async\" width=\"766\" height=\"40\" src=\"https:\/\/www.michelecoscia.com\/wp-content\/uploads\/2022\/06\/image-6.png\" alt=\"\" class=\"wp-image-2156\" srcset=\"https:\/\/www.michelecoscia.com\/wp-content\/uploads\/2022\/06\/image-6.png 766w, https:\/\/www.michelecoscia.com\/wp-content\/uploads\/2022\/06\/image-6-300x16.png 300w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.michelecoscia.com\/wp-content\/uploads\/2022\/06\/image-8.png\"><img loading=\"lazy\" decoding=\"async\" width=\"761\" height=\"39\" src=\"https:\/\/www.michelecoscia.com\/wp-content\/uploads\/2022\/06\/image-8.png\" alt=\"\" class=\"wp-image-2158\" srcset=\"https:\/\/www.michelecoscia.com\/wp-content\/uploads\/2022\/06\/image-8.png 761w, https:\/\/www.michelecoscia.com\/wp-content\/uploads\/2022\/06\/image-8-300x15.png 300w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.michelecoscia.com\/wp-content\/uploads\/2022\/06\/image-9.png\"><img loading=\"lazy\" decoding=\"async\" width=\"758\" height=\"36\" src=\"https:\/\/www.michelecoscia.com\/wp-content\/uploads\/2022\/06\/image-9.png\" alt=\"\" class=\"wp-image-2159\" srcset=\"https:\/\/www.michelecoscia.com\/wp-content\/uploads\/2022\/06\/image-9.png 758w, https:\/\/www.michelecoscia.com\/wp-content\/uploads\/2022\/06\/image-9-300x14.png 300w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">I&#8217;m sure better programmers than me can find better solutions (in less time) and that there are infinite edge cases I&#8217;m not considering here, but this was good enough for me. At least I can now go back and do the stuff my employer is actually paying me to do, and I don&#8217;t feel I&#8217;m in a Francis Ford Coppola masterpiece any more.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>My wife is the literal embodiment of this comic strip: She has to overcome the weirdest code golfing issues to get through her research. The other day her issue was: &#8220;I have a bunch of text and I want to surround every unicode emoticon with spaces&#8221; &#8212; the idea being to treat each emoji like &hellip; <a href=\"https:\/\/www.michelecoscia.com\/?p=2148\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Give Me Some Space!, or: the Slow Descent into Unicode Madness&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[241],"tags":[238,239,240,108,237],"class_list":["post-2148","post","type-post","status-publish","format-standard","hentry","category-coding","tag-code-golf","tag-emoji","tag-emoticons","tag-horror-tales","tag-unicode"],"_links":{"self":[{"href":"https:\/\/www.michelecoscia.com\/index.php?rest_route=\/wp\/v2\/posts\/2148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.michelecoscia.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.michelecoscia.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.michelecoscia.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.michelecoscia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2148"}],"version-history":[{"count":8,"href":"https:\/\/www.michelecoscia.com\/index.php?rest_route=\/wp\/v2\/posts\/2148\/revisions"}],"predecessor-version":[{"id":2166,"href":"https:\/\/www.michelecoscia.com\/index.php?rest_route=\/wp\/v2\/posts\/2148\/revisions\/2166"}],"wp:attachment":[{"href":"https:\/\/www.michelecoscia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.michelecoscia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.michelecoscia.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}