从西里尔文到拉丁文的音译 ICU4j java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16273318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Transliteration from Cyrillic to Latin ICU4j java
提问by Boris Gaganelov
I need to do something rather simple but without hash mapping hard coding.
我需要做一些相当简单但没有哈希映射硬编码的事情。
I have a String s and it is in Cyrillic I need some sort of example on how to turn it into Latin characters using a custom filter of a sort (to give a purely Latin example as to not confuse anyone if String s = sniff; I want it to look up s-n-i-f-f and change them into something else (there might also be combinations).
我有一个 String s 并且它是西里尔文我需要一些关于如何使用某种自定义过滤器将它转换为拉丁字符的示例希望它查找嗅探并将它们更改为其他内容(也可能有组合)。
I can see that ICU4j can do this sort of thing but I have no idea how to achieve it as I can't find any working examples (or I'm just stupid).
我可以看到 ICU4j 可以做这种事情,但我不知道如何实现它,因为我找不到任何工作示例(或者我只是愚蠢)。
Any help is appreciated.
任何帮助表示赞赏。
Thanks
谢谢
Best Regards,
最好的祝福,
PS I need a batch translate. I don't care about styles or dynamic transliteration just some basic example on what a ICU4j batch transliterator would look like.
PS 我需要批量翻译。我不关心样式或动态音译,只是一些关于 ICU4j 批量音译器外观的基本示例。
K I actually got it.
KI实际上得到了它。
import com.ibm.icu.text.Transliterator;
public class BulgarianToLatin {
public static String BULGARIAN_TO_LATIN = "Bulgarian-Latin/BGN";
public static void main(String[] args) {
String bgString = "Джокович";
Transliterator bulgarianToLatin = Transliterator.getInstance(BULGARIAN_TO_LATIN);
String result1 = bulgarianToLatin.transliterate(bgString);
System.out.println("Bulgarian to Latin:" + result1);
}
}
Also one last edit for a rule based transliteration ( if you do not wish to use the pre-existing once or just want something custom made )
也是基于规则的音译的最后一次编辑(如果您不想使用预先存在的一次或只想定制一些东西)
import com.ibm.icu.text.Transliterator;
public class BulgarianToLatin {
public static String BULGARIAN_TO_LATIN = "Bulgarian-Latin/BGN";
public static void main(String[] args) {
String bgString = "а б в г д е ж з и й к л м н о п р с т у ф х ц ч ш щ ю я \n Юлиян Джокович";
String rules="::[А-ЪЬЮ-ъьюя????];" +
"Б > B;" +
"б > b;" +
"В > V;" +
"ТС > TS;" +
"Тс > Ts;" +
"ч > ch;" +
"ШТ > SHT;" +
"Шт > Sht;" +
"шт > sht;" +
"{Ш}[[б-джзй-нп-тф-щь][аеиоуъюя??]] > Sh;" +
"Я > YA;" +
"я > ya;";
Transliterator bulgarianToLatin = Transliterator.createFromRules("temp", rules, Transliterator.FORWARD);
String result1 = bulgarianToLatin.transliterate(bgString);
System.out.println("Bulgarian to Latin:" + result1);
}
}
回答by lxknvlk
I've wrote a method to transliterate cyrillic to latin, maybe this would be useful to smb.
我写了一种将西里尔字母音译为拉丁语的方法,也许这对 smb 有用。
public static String transliterate(String message){
char[] abcCyr = {' ','а','б','в','г','д','е','ё', 'ж','з','и','й','к','л','м','н','о','п','р','с','т','у','ф','х', 'ц','ч', 'ш','щ','ъ','ы','ь','э', 'ю','я','А','Б','В','Г','Д','Е','Ё', 'Ж','З','И','Й','К','Л','М','Н','О','П','Р','С','Т','У','Ф','Х', 'Ц', 'Ч','Ш', 'Щ','Ъ','Ы','Ь','Э','Ю','Я','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
String[] abcLat = {" ","a","b","v","g","d","e","e","zh","z","i","y","k","l","m","n","o","p","r","s","t","u","f","h","ts","ch","sh","sch", "","i", "","e","ju","ja","A","B","V","G","D","E","E","Zh","Z","I","Y","K","L","M","N","O","P","R","S","T","U","F","H","Ts","Ch","Sh","Sch", "","I", "","E","Ju","Ja","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
StringBuilder builder = new StringBuilder();
for (int i = 0; i < message.length(); i++) {
for (int x = 0; x < abcCyr.length; x++ ) {
if (message.charAt(i) == abcCyr[x]) {
builder.append(abcLat[x]);
}
}
}
return builder.toString();
}