6 ms·
Unicode sorting is hard and why browsers added special emoji matching to regexp
- smasher164 5y ago> While WASM languages could shell out to JavaScript browser APIs for collation, I suspect they won’t due to the lack of guarantees around those APIs. That's just it, right? Unicode support has always been a tradeoff between precise coverage/larger binaries and loose coverage/smaller binaries. I'd be excited to see the data size for collation+normalization get under 1MB.
- lifthrasiir 5y agoIf you only ship the default collation it is already well under 1MB. DUCET [1] for example is 406 KB in a naive format [2], which gzips (-9) into 164 KB and Brotli (-q 11) brings it down to 76 KB. Other Unicode tables combined, even in a directly-accessible form, do not exceed 200 KB with an appropriate encoding in my experience. [1] https://www.unicode.org/Public/UCA/13.0.0/allkeys.txt https://www.unicode.org/Public/UCA/13.0.0/allkeys.txt [2] Encodes `XXXX; [.AAAA.BBBB.CCCC] [*DDDD.EEEE.FFFF] ...` into three-byte code point and seven bytes per each collation element (`.` encoded as 0x01, `*` encoded as 0x02, the end of elements signalled with 0x00); everything is in big endian and sorted by code point. This is not suitable for direct access as is but close. (The only reason I've used a naive encoding here is that, I have never tried to compress DUCET in a directly-accessible form so I couldn't give an educated guess at all.)
- slimsag 5y agoAuthor here, I hinted at the end of the article that I am exploring exactly that: a compression algorithm for the DUCET specifically. My early findings suggest that I can bring it to under 50K and beat gzip/brotli compressions, but with those on top can go further https://github.com/jecolon/ziglyph/issues/3 https://github.com/jecolon/ziglyph/issues/3
- lifthrasiir 5y ago28 KB with Brotli! I'd like to see the number with LZ4 (or similarly simple compression algorithms) so that the dependency can be minimized, but that sounds way better than I imagined.
- josephg 5y agoYeah its true but disappointing. I ported some email parsing code from C to wasm a couple of years ago and ran into this problem. Emails are encoded using all sorts of different character encodings. Canonicalizing them to utf-8 is important for consumers, but the libicu database for character encoding is several megabytes. So I'm left with a sad set of choices. I could: 1) Not support character encodings outside of ASCII, utf-8 and utf-16 2) Ship a huge wasm bundle or 3) Call back into javascript from my wasm module and use the javascript wrappers around libicu. This would make it harder to use the library outside of javascript. I'm not sure what the right solution is here. If the character conversion databases can be shrunk to <500kb then I could just ship them with the module. Or if wasm had a better standard library, I could use that. But right now the whole situation is unfortunately awkward.
- jrochkind1 5y agoThe takeaway is not "unicode is overly complex", it's that an algorithm for sorting strings consisting of any possible human language characters is super convoluted, and it's pretty amazing that unicode gives us a standard collation algorithm including localizations, that works remarkably well. But... is complicated enough to implement that many environments apparently haven't felt there was enough demand to do so?
- nabla9 5y agoUnicode is overly complex, but sorting is not the reason. Unicode is overly complex at grapheme cluster level.
- pilif 5y ago> algorithm for sorting strings consisting of any possible human language characters is super convoluted not only that: it's different depending on locale and sometimes context (in Germany there's a distinction of whether you sort for a phone book listing or not). Doing it right requires basically shipping a huge database with locale specific information which is seen as "bloat" by many projects and thus made optional or not available at all. >and it's pretty amazing that unicode gives us a standard collation algorithm including localizations, that works remarkably well. it's not good enough in many cases. Thankfully there's ICU, but many environments frown upon that because it's seen as bloated. If sorting works "fine" for US-ASCII, why link to a 60MB library?
- jrochkind1 5y agoI meant to include ICU in what I was saying works well when I said "unicode gives us a standard collation algorithm including localizations". ICU stands for "International Components for Unicode." ICU collation is an implementation of the Unicode Collation Algorithm (which is really a suite of algorithms, or an algorithm with various parameterized branches), using the Unicode Common Locale Data Repository for localization -- that's also unicode, the CLDR is a unicode thing too, part of unicode. It's all unicode. There are other implementations than ICU -- although maybe none as complete, because it is a lot/expensive, indeed. But ICU is an implementation of unicode algorithms. But for instance, unicode collation is also implemented -- in a way that will be the same as ICU modulo bugs or details the standard leaves up to implementations to vary -- in pure ruby by https://github.com/twitter/twitter-cldr-rb#sorting-collation https://github.com/twitter/twitter-cldr-rb#sorting-collation
- est 5y agoIt's not Unicode sorting is hard, it's mostly because CJK, ideograms, emojis, and many other non-alphabetical languages do not have order. It's not a bug, it's by design.
- lifthrasiir 5y ago> [...] CJK, ideograms, emojis, and many other non-alphabetical languages do not have order. Uh, no? Most if not all scripts do have letter order, CJK(V) "ideographs" included. The actual problem is twofold: the letter order might not be unique, and the word order might be completely different from the letter order. CJK(V) "ideographs" exhibit both problems---their word order is phonetic, while letter order is based on shapes and different dictionaries use different rules---and it is very common in Chinese and Japanese that the string meant to be sortable has pronunciation attached (e.g. Wikipedia). Also, believe it or not, even emojis do have order too [1], albeit arbitrary one. [1] https://unicode.org/emoji/charts/emoji-ordering.html https://unicode.org/emoji/charts/emoji-ordering.html
- naniwaduni 5y agoEven English a few edge cases where collation order doesn't match letter order: titles are typically ordered ignoring initial A/The, names are often sorted by surname (even when written with given name first); famously, M'/Mc are are traditionally sorted with Mac, along with a host of special cases that get gleefully thrown out in this era of doing very dumb very fast.
- gumby 5y agoIt's not sensible to call out non-alphabetic (by which I imagine you exclude all phonemic systems including abjads, syllabaries, etc all of which have full character order. Even in Latin scripts, sorting order is language-specific and can even be domain specific (e.g. some languages sort phone books differently from dictionaries). For "CJKV" systems there are ordering methods such as radicals and, as in European languages, ordering can depend on what you want to accomplish.
- minikomi 5y ago
- 3np 5y ago> The preferred alphabetical sorting would be [ "Ähnlich", "Äpfel", "Bären", "Käfer", "küssen" ] As an example of a conflicting locale, Swedish would have Äpfel after Käsen!
- Svip 5y agoLike "Aalborg" being after "Zaragoza" in a Danish sorting. And that's more tricky with "aa" being treated as a single character in Danish.
- 3np 5y agoAalborg is one of very few exceptions that even in Danish can commonly be spelled with “aa” as a replacement for “å” (the single character). Just like ae for æ.
- Svip 5y agoUnfortunately, the exceptions only appear in names and proper nouns, which are where you generally want sorting the most.
- noctune 5y agoThis leads to fun things like the regex [a-z][a-z] not matching the string "aa" in SQL Server when using the Danish collation.
- yangminded 5y agoYes, that is an interesting case! "Ä" is a separate letter in the Swedish alphabet: https://en.wikipedia.org/wiki/Swedish_alphabet https://en.wikipedia.org/wiki/Swedish_alphabet In German on the other hand, "Ä" is part of the language but not part of the alphabet and it is simply expanded to "AE" as in the previous comment.
- detaro 5y agoEDIT: following comment is specifically about German German, Swiss German etc do not always work the same > In German on the other hand, "Ä" is part of the language but not part of the alphabet Some people claim that, but I wouldn't take that as an authoritative statement. Plenty language authorities disagree, since "ä" and "ae" are not interchangeable. > it is simply expanded to "AE" as in the previous comment Only in some contexts (lists of names predominantly), generally "ä" is sorted after "a". E.g. the Duden dictionary goes "zahlen", "zählen", "Zahlenangabe".
- phendrenad2 5y agoDoes sorting words from different languages even make sense? Do all languages even define a sort order for their letters? What about languages that aren't based around letters? Frankly I think that the unicode consortium is talking out of school here.
- setr 5y agoThere definitely needs to be a definitive sort order across languages. That it needs to be sensible is highly suspect. Emojis alone should signal that there fundamentally cannot be. The best they can do is define a sensible cross-language sort within a language family (e.g. Latin based) but even that’s highly suspect
- a1369209993 5y ago> The best they can do is define a sensible cross-language sort within a language family [...] but even that's highly suspect. How would you sort ["О", "Е", "Ζ", "Р", "Η", "С", "Κ"], for example?
- deleted 5y ago[deleted]
- tgv 5y agoYou can sort according to the orthography of one and precisely one language. Words from another language would simply follow that, also if that language would sort them differently.
- phendrenad2 5y agoWell you certainly can sort any language's glyphs, using strategies such as that one. But will it be useful outside of that language? People who speak German will be aware of their sort order, and people who speak Korean will be aware of their sort order, but only a small subset of the population who speak German and Korean fluently will care that you went the extra mile to sort properly when sorting a list containing German and Korean words. It almost seems as though showing a globally-sorted list containing multiple languages is an anti-pattern, and you should group by character set first. However, that might be a political boondoggle if German/Anglo names are always displayed before or after the Korean names. Hmmm...
- twic 5y agoFWIW: List<String> sorted = Stream.of("Bären", "Käfer", "küssen", "Ähnlich", "Äpfel") .sorted(Collator.getInstance(Locale.ROOT)) .collect(Collectors.toList()); // [Ähnlich, Äpfel, Bären, Käfer, küssen] In Java, String's default comparison is lexicographic over UTF-16 code units. Whilst this isn't precisely what you'd choose today, i do think it makes sense for the default comparison not to be locale-sensitive.
- emilfihlman 5y ago>The preferred alphabetical sorting would be [ "Ähnlich", "Äpfel", "Bären", "Käfer", "küssen" ] - Array.sort doesn’t do that. All my literal wat. This is absolutely not the expected nor the preferred behaviour.