Aide mémoire : traduction automatique

Aide mémoire (Développement) / Uncategorized
Aide mémoire sur l’utiliation d’une API de traduction automagique…

En se basant sur https://code.google.com/p/java-google-translate-text-to-speech/

Voici un bout de code permettant de traduire un fichier TXT de russe à anglais sans trop de difficulté :

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import com.gtranslate.Language;
import com.gtranslate.Translator;

/** @author pseudo555 */
public class MainTranslator {

    private static final int space = 32;
    private static final int point = 462;
    private static final Translator translate = Translator.getInstance();

    /**
     * Ask only for russian part to be translated. 
     * @param txtOri Original
     * russian line to translate 
     * @return Translated version of the given line
     */
    private static String translate(String txtOri) {
        boolean isEn = true;
        StringBuilder recomposed = new StringBuilder();
        StringBuilder toTranslate = new StringBuilder();
        for (int idx = 0; idx  1000 || ((chPos == space) && !isEn)) {
                isEn = false;
                toTranslate.append(c);
            } else {
                isEn = true;
                if (toTranslate.length() > 0) {
                    recomposed.append(translate.translate(toTranslate.toString(), Language.RUSSIAN, Language.ENGLISH));
                }
                //uncomment if " must be escaped 				
                //if(c == '"' && ! isFile) recomposed.append("\\"); 				
                recomposed.append(c);
                toTranslate = new StringBuilder();
            }
        }
        if (toTranslate.length() > 0) {
            recomposed.append(translate.translate(toTranslate.toString(), Language.RUSSIAN, Language.ENGLISH));
        }
        return recomposed.toString();
    }

    public static void main(String[] args) throws IOException {
        String path = "C:\\mon fichier.txt";
        for (String txtOri : Files.readAllLines(new File(path).toPath(), Charset.forName("UTF-8"))) {
            System.out.println(translate(txtOri));
        }
    }
}