37 lines
1.5 KiB
Java
37 lines
1.5 KiB
Java
|
package io.gitlab.jfronny.translater;
|
||
|
|
||
|
import com.github.vbauer.yta.model.Direction;
|
||
|
import com.github.vbauer.yta.model.Language;
|
||
|
import com.github.vbauer.yta.model.Languages;
|
||
|
import com.github.vbauer.yta.service.YTranslateApiImpl;
|
||
|
|
||
|
import java.util.*;
|
||
|
|
||
|
public class YnTransformer extends CachingTransformer {
|
||
|
private static final int rounds = 10;
|
||
|
private static final boolean breakFully = true;
|
||
|
private static final String key = "trnsl.1.1.20200118T180605Z.654f2ec649458c36.107c6ad38dc02937f25e660aa1f8f4097d6561a8";
|
||
|
|
||
|
private static final YTranslateApiImpl api = new YTranslateApiImpl(key);
|
||
|
private static final Set<Language> languages = api.languageApi().all().languages();
|
||
|
private static final Random rnd = new Random();
|
||
|
|
||
|
@Override
|
||
|
public String getTransformed(String str) {
|
||
|
ModInit.Log("Transforming: " + str);
|
||
|
Language startLang = api.detectionApi().detect(str).get();
|
||
|
String currentState = str;
|
||
|
Language currentLang = startLang;
|
||
|
for (int i = 0; i < rounds; i++) {
|
||
|
Language newLang = selectRandom();
|
||
|
currentState = api.translationApi().translate(currentState, Direction.of(breakFully ? selectRandom() : currentLang, newLang)).text();
|
||
|
currentLang = newLang;
|
||
|
}
|
||
|
return api.translationApi().translate(currentState, startLang).text();
|
||
|
}
|
||
|
|
||
|
private Language selectRandom() {
|
||
|
return languages.stream().skip(rnd.nextInt(languages.size())).findFirst().get();
|
||
|
}
|
||
|
}
|