Converting Symbols, Accent Letters to English Alphabet
Solution 1
Reposting my post from How do I remove diacritics (accents) from a string in .NET?
This method works fine in java (purely for the purpose of removing diacritical marks aka accents).
It basically converts all accented characters into their deAccented counterparts followed by their combining diacritics. Now you can use a regex to strip off the diacritics.
import java.text.Normalizer;
import java.util.regex.Pattern;
public String deAccent(String str) {
String nfdNormalizedString = Normalizer.normalize(str, Normalizer.Form.NFD);
Pattern pattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
return pattern.matcher(nfdNormalizedString).replaceAll("");
}
Solution 2
It's a part of Apache Commons Lang as of ver. 3.0.
org.apache.commons.lang3.StringUtils.stripAccents("Añ");
returns An
Also see http://www.drillio.com/en/software-development/java/removing-accents-diacritics-in-any-language/
Solution 3
Attempting to "convert them all" is the wrong approach to the problem.
Firstly, you need to understand the limitations of what you are trying to do. As others have pointed out, diacritics are there for a reason: they are essentially unique letters in the alphabet of that language with their own meaning / sound etc.: removing those marks is just the same as replacing random letters in an English word. This is before you even go onto consider the Cyrillic languages and other script based texts such as Arabic, which simply cannot be "converted" to English.
If you must, for whatever reason, convert characters, then the only sensible way to approach this it to firstly reduce the scope of the task at hand. Consider the source of the input - if you are coding an application for "the Western world" (to use as good a phrase as any), it would be unlikely that you would ever need to parse Arabic characters. Similarly, the Unicode character set contains hundreds of mathematical and pictorial symbols: there is no (easy) way for users to directly enter these, so you can assume they can be ignored.
By taking these logical steps you can reduce the number of possible characters to parse to the point where a dictionary based lookup / replace operation is feasible. It then becomes a small amount of slightly boring work creating the dictionaries, and a trivial task to perform the replacement. If your language supports native Unicode characters (as Java does) and optimises static structures correctly, such find and replaces tend to be blindingly quick.
This comes from experience of having worked on an application that was required to allow end users to search bibliographic data that included diacritic characters. The lookup arrays (as it was in our case) took perhaps 1 man day to produce, to cover all diacritic marks for all Western European languages.
Solution 4
Since the encoding that turns "the Family" into "tђє Ŧค๓เℓy" is effectively random and not following any algorithm that can be explained by the information of the Unicode codepoints involved, there's no general way to solve this algorithmically.
You will need to build the mapping of Unicode characters into latin characters which they resemble. You could probably do this with some smart machine learning on the actual glyphs representing the Unicode codepoints. But I think the effort for this would be greater than manually building that mapping. Especially if you have a good amount of examples from which you can build your mapping.
To clarify: a few of the substitutions can actually be solved via the Unicode data (as the other answers demonstrate), but some letters simply have no reasonable association with the latin characters which they resemble.
Examples:
- "ђ" (U+0452 CYRILLIC SMALL LETTER DJE) is more related to "d" than to "h", but is used to represent "h".
- "Ŧ" (U+0166 LATIN CAPITAL LETTER T WITH STROKE) is somewhat related to "T" (as the name suggests) but is used to represent "F".
- "ค" (U+0E04 THAI CHARACTER KHO KHWAI) is not related to any latin character at all and in your example is used to represent "a"
Solution 5
String tested : ÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝß
Tested :
- Output from Apache Commons Lang3 : AAAAAÆCEEEEIIIIÐNOOOOOØUUUUYß
- Output from ICU4j : AAAAAÆCEEEEIIIIÐNOOOOOØUUUUYß
- Output from JUnidecode : AAAAAAECEEEEIIIIDNOOOOOOUUUUUss (problem with Ý and another issue)
- Output from Unidecode : AAAAAAECEEEEIIIIDNOOOOOOUUUUYss
The last choice is the best.
ahmet alp balkan
I am a software engineer on Twitter compute infrastructure team. Previously I've worked at Google Cloud on Kubernetes, Cloud Run and Knative, and at Microsoft Azure on various parts of the Docker open source ecosystem. Find me on my: (blog | twitter | github)
Updated on April 11, 2020Comments
-
ahmet alp balkan about 4 years
The problem is that, as you know, there are thousands of characters in the Unicode chart and I want to convert all the similar characters to the letters which are in English alphabet.
For instance here are a few conversions:
ҥ->H Ѷ->V Ȳ->Y Ǭ->O Ƈ->C tђє Ŧค๓เℓy --> the Family ...
and I saw that there are more than 20 versions of letter A/a. and I don't know how to classify them. They look like needles in the haystack.
The complete list of unicode chars is at http://www.ssec.wisc.edu/~tomw/java/unicode.html or http://unicode.org/charts/charindex.html . Just try scrolling down and see the variations of letters.
How can I convert all these with Java? Please help me :(
-
Roberto Bonvallet almost 15 yearsI agree. You should create a dictionary of conversions specifically for your application and expected audience. For example, for a Spanish-speaking audience I would only translate ÁÉÍÓÚÜÑáéíóúü¿¡
-
ahmet alp balkan almost 15 yearsRoberto there are thousands of characters and I can't do this manual.
-
Dour High Arch almost 15 yearsWhat human language are you using that has "thousands" of characters? Japanese? What would you expect どうしようとしていますか to be converted to?
-
ahmet alp balkan almost 15 yearsiAn thanks for answering. Actually I'm not working with arabic languages or something like that. You know some people use the diacritics as funny characters and I have to remove that as much as I can do. For instance, I said "tђє Ŧค๓เℓy --> the Family" conversion in the example but it seems difficult convert it completely. However, we can make the conversion "òéışöç->oeisoc" in a simple way. But what is the exact way to do this. Creating arrays and replacing manually? Or does this language have native functions about this issue?
-
Kathy Van Stone almost 15 yearsYou might be able to get a lookup table from one of these.
-
Dour High Arch almost 15 yearsUnfortunately that will not handle ligatures like Æ.
-
Dour High Arch almost 15 yearsThis is an amazing package, but it transliterates the sound of the character, for example it converts "北" to "Bei" because that is what the character sounds like in Mandarin. I think the questioner wants to convert glyphs to what they visually resemble in English.
-
Bruce almost 15 yearsIt does do that for latin characters, though. â becomes a, et al. @ahmetalpbalkan I agree with Kathy, you could use it as a resource to build your own lookup table, the logic should be pretty simple. Unfortuantely there doesn't seem to be a java version.
-
Jonik almost 15 years+1. Here's a Java version of the 'remove diacritics' question: stackoverflow.com/questions/1016955/…; see Michael Borgwardt's and devio's answers
-
Joachim Sauer over 14 yearsThe example you've given is not ideal: U+00DF LATIN SMALL LETTER SHARP S "ß" is not the same Unicode letter as U+03B2 GREEK SMALL LETTER BETA "β".
-
iwein about 14 yearsInCombiningDiacriticalMarks doesn't convert all cyrillics. For example Општина Богомила is untouched. It would be nice if one could convert it to Opstina Bogomila or something
-
MSalters almost 14 yearsIt doesn't transliterate at all. It merely removes decomposed diacritical marks ("accents"). The previous step (Form.NFD) breaks down á in a + ', i.e. decomposing the accented character into an unaccented character plus a diacritical mark. This would convert cyrillic Ѽ into Ѡ but not further.
-
ATorras about 12 yearsGeorge posted that it could be better use the \\p{IsM} instead of \\p{InCombiningDiacriticalMarks} at glaforge.appspot.com/article/… Note that I have not tested it.
-
Loic about 11 years\\p{IsM} does not seem to work for spanish accents like á ó ú ñ é í . On the contrary, "\\p{InCombiningDiacriticalMarks}+ is working good for this
-
Tom over 9 yearsThis solution is amazing. It works with Greek too! Thank you.
-
Jakub Jirutka almost 9 years@ahmetalpbalkan Here is unidecode for Java.
-
Michał Tajchert over 8 yearsIt doesn't work for all special characters - I submitted a wrong issue for Android for that to learn that -> code.google.com/p/android/issues/detail?id=189515 Anybody know correct way to do this?
-
Karol S over 8 years@Tajchert Your issue was invalid, since Ł cannot be decomposed. What's wrong is not the normalizer, but using it to strip accents.
-
Michał Tajchert over 8 years@KarolS that is why I had wrote " I submitted a wrong issue" as I know this was not a bug itself but using not correct class to this function. Which is done in this answer.
-
Buddy almost 8 yearsthis can't convert
ı
toi
in Turkish -
Robert over 7 yearsIt's not perfect for Polish characters translation from ł and Ł is missing: input: ŚŻÓŁĄĆĘŹąółęąćńŃ output: SZOŁACEZaołeacnN
-
rafalmag over 7 yearsSmall warning - it removes U+00DF LATIN SMALL LETTER SHARP S "ß"
-
polaretto over 7 yearsNice utility but since its code is exactly the same as the one showed in the accepted answer, and you don't want to add a dependency on Commons Lang, you can just use the aforementioned snippet.
-
cactuschibre about 7 yearsAnd also Æ... To bad.
-
Hoang over 6 yearswith apache common in my case: Đ not convert to D
-
febot over 6 years@Hoang, Robert maybe a chance to send a pull request :)
-
Na Pro over 6 yearsIt doesn't work for all characters bro. For example, my input is "Xin chào, chúng ta sẽ đi tới Việt Nam" then output is "Xin chao, chung ta se đi toi Viet Nam", pls pay attention to letter "đ"
-
Parker almost 6 yearsThis method is particularly useful if you need to detect and handle classes of diacritics differently (i.e., escaping special characters in LaTeX).
-
cactuschibre over 5 years@mehmet Just follow the readme at github.com/xuender/unidecode. It should be something like Unidecode.decode("ÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝß") after importing the dependency.
-
Lii over 2 yearsThis is an interesting test. But it would be even better if you wrote out which methods from the different libraries you are using!