简体   繁体   中英

Java code to translate using a particular language dictionary

I am not that good at java program, i am trying to do something like this please help.

I want to make a simple translator for one particular language, say korean .

Can i download a korean - english dictionary from internet and use it as a hash map or a dictionary datastructure to search for korean word based on the english input i give?

You can use the Google Translate API , its very easy to implement. For sure you will need an internet connection for it to work.

Translator translate = Translator.getInstance();
String text = translate.translate("Good morning", Language.ENGLISH, Language.KOREAN)

Using a map is one possible & easy way (as shown in the short example). Though you still would need to do a big effort for problems like upper/lower case of words, punctuation, verbs etc.

    Map<String, String> dictionary = new HashMap<String, String>();
    dictionary.put("this", "das");
    dictionary.put("is", "ist");
    dictionary.put("my", "mein");
    dictionary.put("house", "haus");

    String translate = "this is my new house";
    StringBuilder result = new StringBuilder();

    StringTokenizer st = new StringTokenizer(translate, " ");
    while (st.hasMoreTokens()) {
        String key = st.nextToken();
        String translatedWord = dictionary.get(key);
        if (translatedWord != null) {
            result.append(translatedWord);
        } else {
            result.append("*" + key + "*"); // Unknown word
        }
        result.append(" ");
    }

    System.out.println(result.toString());

[ { "logtime": "2021/12/27 15:14:17", "logdata": "[string "game/yule/updown/src/views/layer/GameViewLayer.luac"]:1425: 尝试执行本地 'oldscore'(一个 nil 值)的算术\\n堆栈回溯:\\n\\t[string "game/yule/updown/src/views/GameLayer.luac"]:368: 在函数 <[string "game/yule/ updown/src/views/GameLayer.luac"]:365>" } ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM