编程语言
首页 > 编程语言> > 音译从西里尔语到拉丁语ICU4j java [复制]

音译从西里尔语到拉丁语ICU4j java [复制]

作者:互联网

参见英文答案 > icu4j cyrillic to latin                                    3个
我需要做一些相当简单但没有哈希映射硬编码的东西.

我有一个字符串s,它是在西里尔语中我需要一些关于如何使用自定义类型的过滤器将其转换为拉丁字符的示例(给出一个纯粹的拉丁语示例,以便在String s = sniff时不会混淆任何人;我希望它查找嗅闻并将其更改为其他内容(可能还有组合).

我可以看到ICU4j可以做这种事情,但我不知道如何实现它,因为我找不到任何有用的例子(或者我只是愚蠢).

任何帮助表示赞赏.

谢谢

最好的祝福,

PS我需要批量翻译.我不关心样式或动态音译只是关于ICU4j批量音译的样子的一些基本例子.

我实际上得到了它.

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);

    }

}

也是基于规则的音译的最后一次编辑(如果您不希望使用预先存在的一次或只是想要定制的东西)

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);

    }

}

解决方法:

我写了一个方法来将西里尔文音译成拉丁语,也许这对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();
}

标签:transliteration,java
来源: https://codeday.me/bug/20190927/1824359.html