Skip to content

Commit d7fbece

Browse files
committed
Show synonyms in Text Translation GUI when a word is translated with a dictionary
1 parent 026c332 commit d7fbece

9 files changed

Lines changed: 212 additions & 71 deletions

File tree

app/src/main/java/nie/translator/rtranslator/Global.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public void onCreate() {
103103
createNotificationChannel();
104104
SharedPreferences sharedPreferences = getSharedPreferences("default", Context.MODE_PRIVATE);
105105
useTatoeba = sharedPreferences.getBoolean("useTatoeba", false);
106+
useTranslationDictionaries = sharedPreferences.getBoolean("useTranslationDictionaries", false);
106107
}
107108

108109
public void initializeTranslator(Translator.GeneralListener initListener){

app/src/main/java/nie/translator/rtranslator/settings/SettingsFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
204204
});
205205

206206
// useTranslationDict initialization
207-
Preference translationDictPreference = findPreference("useTranslationDict");
207+
Preference translationDictPreference = findPreference("useTranslationDictionaries");
208208
translationDictPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
209209
@Override
210210
public boolean onPreferenceChange(Preference preference, Object newValue) {

app/src/main/java/nie/translator/rtranslator/tools/gui/animations/CustomAnimator.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,73 @@ public void onAnimationRepeat(@NonNull Animator animator) {}
795795
return animationAppearance;
796796
}
797797

798+
public Animator animateSynonymsTextEnlargement(Context context, TextView textView, int topMarginDp, Listener listener){
799+
AnimatorSet animatorSet= new AnimatorSet();
800+
int duration=context.getResources().getInteger(R.integer.durationStandard);
801+
int durationShort=context.getResources().getInteger(R.integer.durationShort);
802+
803+
textView.setVisibility(View.VISIBLE);
804+
805+
textView.measure(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
806+
final int textViewFinalHeight = textView.getMeasuredHeight();
807+
Animator textHeightAnimator = createAnimatorHeight(textView, textView.getHeight(), textViewFinalHeight, duration);
808+
Animator textMarginAnimator = createAnimatorTopMargin(textView, 0, Tools.convertDpToPixels(context, topMarginDp), duration);
809+
Animator textAlphaAnimator = createAnimatorAlpha(textView, textView.getAlpha(), 1, duration);
810+
animatorSet.play(textHeightAnimator).with(textMarginAnimator).with(textAlphaAnimator);
811+
animatorSet.addListener(new Animator.AnimatorListener() {
812+
@Override
813+
public void onAnimationStart(@NonNull Animator animator) {
814+
if (listener != null) {
815+
listener.onAnimationStart();
816+
}
817+
}
818+
@Override
819+
public void onAnimationEnd(@NonNull Animator animator) {
820+
if (listener != null) {
821+
listener.onAnimationEnd();
822+
}
823+
}
824+
@Override
825+
public void onAnimationCancel(@NonNull Animator animator) {}
826+
@Override
827+
public void onAnimationRepeat(@NonNull Animator animator) {}
828+
});
829+
animatorSet.start();
830+
return animatorSet;
831+
}
832+
833+
public Animator animateSynonymsTextReduction(Context context, TextView textView, int topMarginDp, Listener listener){
834+
AnimatorSet animatorSet= new AnimatorSet();
835+
int duration=context.getResources().getInteger(R.integer.durationStandard);
836+
int durationShort=context.getResources().getInteger(R.integer.durationShort);
837+
838+
Animator textHeightAnimator = createAnimatorHeight(textView, textView.getHeight(), 0, duration);
839+
Animator textMarginAnimator = createAnimatorTopMargin(textView, Tools.convertDpToPixels(context, topMarginDp), 0, duration);
840+
Animator textAlphaAnimator = createAnimatorAlpha(textView, textView.getAlpha(), 0, duration);
841+
animatorSet.play(textHeightAnimator).with(textMarginAnimator).with(textAlphaAnimator);
842+
animatorSet.addListener(new Animator.AnimatorListener() {
843+
@Override
844+
public void onAnimationStart(@NonNull Animator animator) {
845+
if (listener != null) {
846+
listener.onAnimationStart();
847+
}
848+
}
849+
@Override
850+
public void onAnimationEnd(@NonNull Animator animator) {
851+
textView.setVisibility(View.INVISIBLE);
852+
if (listener != null) {
853+
listener.onAnimationEnd();
854+
}
855+
}
856+
@Override
857+
public void onAnimationCancel(@NonNull Animator animator) {}
858+
@Override
859+
public void onAnimationRepeat(@NonNull Animator animator) {}
860+
});
861+
animatorSet.start();
862+
return animatorSet;
863+
}
864+
798865
public void animateSwitchLanguages(Context context, CardView firstLanguageContainer, CardView secondLanguageContainer, AppCompatImageButton invertLanguagesButton, Listener listener){
799866
int duration = context.getResources().getInteger(R.integer.durationStandard);
800867

app/src/main/java/nie/translator/rtranslator/voice_translation/_text_translation/TranslationFragment.java

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public class TranslationFragment extends Fragment {
9494
private FloatingActionButton ttsOutputButton;
9595
private ConstraintLayout outputContainer;
9696
private TextView resultTypeText;
97+
private TextView synonymsText;
9798
private CustomAnimator animator = new CustomAnimator();
9899
private Animator colorAnimator = null;
99100
private int activatedColor = R.color.primary;
@@ -103,8 +104,10 @@ public class TranslationFragment extends Fragment {
103104
private boolean isInputEmpty = true;
104105
private boolean isOutputEmpty = true;
105106
private boolean isOutputTypeVisible = false;
107+
private boolean isSynonymsTextVisible = false;
106108
ViewTreeObserver.OnGlobalLayoutListener layoutListener;
107109
private static final int REDUCED_GUI_THRESHOLD_DP = 550;
110+
private static final int synonymsTopMargin = 16;
108111

109112
//languageListDialog
110113
private LanguageListAdapter listView;
@@ -130,6 +133,8 @@ public class TranslationFragment extends Fragment {
130133
private Animator animationOutput;
131134
@Nullable
132135
private Animator animationResultType;
136+
@Nullable
137+
private Animator animationSynonyms;
133138

134139

135140
@Override
@@ -171,6 +176,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
171176
ttsOutputButton = view.findViewById(R.id.ttsButtonOutput);
172177
outputContainer = view.findViewById(R.id.outputContainer);
173178
resultTypeText = view.findViewById(R.id.resultTypeText);
179+
synonymsText = view.findViewById(R.id.synonymsText);
174180
//we set the initial tag for the tts buttons
175181
ttsInputButton.setTag(R.drawable.sound_icon);
176182
ttsOutputButton.setTag(R.drawable.sound_icon);
@@ -215,9 +221,10 @@ public void onClick(View view) {
215221
conversationButtonSmall.setOnClickListener(conversationButtonListener);
216222
translateListener = new Translator.TranslateListener() {
217223
@Override
218-
public void onTranslatedText(String textToTranslate, String text, long resultID, boolean isFinal, ResultType resultType, CustomLocale languageOfText) {
224+
public void onTranslatedText(String textToTranslate, String text, String[] synonyms, long resultID, boolean isFinal, ResultType resultType, CustomLocale languageOfText) {
219225
outputText.setText(text);
220226
if(isFinal){
227+
//eventually we show result type text
221228
if(resultType != ResultType.NORMAL){
222229
isOutputTypeVisible = true;
223230
if(animationResultType != null) {
@@ -237,6 +244,25 @@ public void onAnimationEnd() {
237244
}
238245
});
239246
}
247+
//eventually we show the synonyms of the translated word
248+
if(synonyms != null && synonyms.length > 0){
249+
isSynonymsTextVisible = true;
250+
if(animationSynonyms != null) {
251+
animationSynonyms.cancel();
252+
}
253+
String t = synonyms[0];
254+
for(int i=1; i<synonyms.length; i++){
255+
t = ", "+synonyms[i];
256+
}
257+
synonymsText.setText(t);
258+
animationSynonyms = animator.animateSynonymsTextEnlargement(activity, synonymsText, synonymsTopMargin, new CustomAnimator.Listener() {
259+
@Override
260+
public void onAnimationEnd() {
261+
super.onAnimationEnd();
262+
animationSynonyms = null;
263+
}
264+
});
265+
}
240266
activateTranslationButton();
241267
}
242268
}
@@ -355,7 +381,7 @@ public void afterTextChanged(Editable s) {
355381
}
356382
if(isInputEmpty != s.toString().isEmpty()){ //the input editText transitioned from empty to not empty or vice versa
357383
isInputEmpty = s.toString().isEmpty();
358-
if(animationInput != null){
384+
if(animationInput != null) {
359385
animationInput.cancel();
360386
}
361387
if(!s.toString().isEmpty()) {
@@ -403,6 +429,7 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
403429

404430
@Override
405431
public void afterTextChanged(Editable s) {
432+
//we eventually make the result type disappear
406433
if(isOutputTypeVisible) {
407434
isOutputTypeVisible = false;
408435
if (animationResultType != null) {
@@ -416,6 +443,21 @@ public void onAnimationEnd() {
416443
}
417444
});
418445
}
446+
//we eventually make the synonyms of the result disappear
447+
if(isSynonymsTextVisible) {
448+
isSynonymsTextVisible = false;
449+
if (animationSynonyms != null) {
450+
animationSynonyms.cancel();
451+
}
452+
animationSynonyms = animator.animateSynonymsTextReduction(activity, synonymsText, synonymsTopMargin, new CustomAnimator.Listener() {
453+
@Override
454+
public void onAnimationEnd() {
455+
super.onAnimationEnd();
456+
animationSynonyms = null;
457+
}
458+
});
459+
}
460+
//if the output text is empty we make it disappear
419461
if(isOutputEmpty != s.toString().isEmpty()){ //the output editText transitioned from empty to not empty or vice versa
420462
isOutputEmpty = s.toString().isEmpty();
421463
if(animationOutput != null) {

app/src/main/java/nie/translator/rtranslator/voice_translation/_walkie_talkie_mode/_walkie_talkie/WalkieTalkieService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ public void onError(int[] reasons, long value) {
289289
};
290290
firstResultTranslateListener = new Translator.TranslateListener() {
291291
@Override
292-
public void onTranslatedText(String textToTranslate, String text, long resultID, boolean isFinal, ResultType resultType, CustomLocale languageOfText) {
292+
public void onTranslatedText(String textToTranslate, String text, String[] synonyms, long resultID, boolean isFinal, ResultType resultType, CustomLocale languageOfText) {
293293
((Global) getApplication()).getTTSLanguages(true, new Global.GetLocalesListListener() {
294294
@Override
295295
public void onSuccess(ArrayList<CustomLocale> ttsLanguages) {
@@ -332,7 +332,7 @@ public void onFailure(int[] reasons, long value) {
332332
};
333333
secondResultTranslateListener = new Translator.TranslateListener() {
334334
@Override
335-
public void onTranslatedText(String textToTranslate, String text, long resultID, boolean isFinal, ResultType resultType, CustomLocale languageOfText) {
335+
public void onTranslatedText(String textToTranslate, String text, String[] synonyms, long resultID, boolean isFinal, ResultType resultType, CustomLocale languageOfText) {
336336
((Global) getApplication()).getTTSLanguages(true, new Global.GetLocalesListListener() {
337337
@Override
338338
public void onSuccess(ArrayList<CustomLocale> ttsLanguages) {

app/src/main/java/nie/translator/rtranslator/voice_translation/neural_networks/translation/LanguageResourcesManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public LanguageResourcesManager(@NonNull Global global, int modelMode, CustomLoc
3434
this.global = global;
3535
this.modelMode = modelMode;
3636
String tatoebaDbPath = Environment.getExternalStorageDirectory().getPath() + "/models/Translation/Tatoeba/tatoeba.db";
37-
String translationDictionariesDbPath = Environment.getExternalStorageDirectory().getPath() + "/models/Translation/TranslationDictionaries/translation_dict_redux.db";
37+
String translationDictionariesDbPath = Environment.getExternalStorageDirectory().getPath() + "/models/Translation/TranslationDictionaries/translation_dict_freedict.db";
3838
tatoebaDb = new TatoebaDbWrapper(tatoebaDbPath);
3939
BergamotTranslator.initializeService();
4040
DictionaryTranslator.initializeService(translationDictionariesDbPath);

0 commit comments

Comments
 (0)