Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert optically equivalent unicode strings to ASCII in Java?

I run a social network that requires unicode usernames to be unique (as expected).

Some creative users have started using Cyrillic (and other) unicode characters to create optically equivalent (but unicode distinct) usernames.

For example, they'll use the Cyrillic small letter a 'а', which looks identical to the roman one.

Does anyone know of a way to convert these optically equivalent characters automatically in Java? I'd rather not have to create a conversion table by hand if a mechanism already exists.

like image 775
OnesAndZeroes Avatar asked Sep 15 '26 15:09

OnesAndZeroes


2 Answers

You can try Unicode normalization - basically, indistinguishable code points have a 'canonical' code point designated, and normalization is the process of replacing each character with its canonical form.

Java seems to support Unicode normalization via java.text.Normalizer - more info here.

However, I'm not sure that latin A and cyrillic A are marked as equivalent in Unicode - you'd have to try.

This will also not help you when your users start using very similar instead of identical characters - humans are very inventive and a technical solution might not work 100% here, so you will probably have to resort to human moderation anyway.

There are also some other solutions - limiting the usernames to latin alphanumerics, for example.

like image 86
Jakub Wasilewski Avatar answered Sep 18 '26 06:09

Jakub Wasilewski


Why don't you try to apply an OCR library.

like image 25
Andyz Smith Avatar answered Sep 18 '26 05:09

Andyz Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!