Skip to content

Commit b3e63f6

Browse files
committed
Implemented new resources management for language resources (Mozilla models and Tatoeba)
1 parent abd9f17 commit b3e63f6

6 files changed

Lines changed: 641 additions & 232 deletions

File tree

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

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -577,9 +577,11 @@ public void setUseTatoeba(boolean useTatoeba) {
577577
editor.putBoolean("useTatoeba", useTatoeba);
578578
editor.apply();
579579
// loading of tatoeba
580-
translator.loadTatoeba(getFirstTextLanguage(true), getSecondTextLanguage(true), RTranslatorMode.TEXT_TRANSLATION_MODE, null);
581-
translator.loadTatoeba(getFirstLanguage(true), getSecondLanguage(true), RTranslatorMode.WALKIE_TALKIE_MODE, null);
582-
//todo: add loading for Conversation mode
580+
if(useTatoeba) {
581+
translator.loadAllTatoebaResources(null);
582+
}else{
583+
translator.unloadAllTatoebaResources();
584+
}
583585
}
584586

585587
public String getName() {
@@ -723,24 +725,7 @@ public boolean isNetworkOnWifi() {
723725
}
724726

725727
private void loadLanguagesResources(CustomLocale firstLanguage, CustomLocale secondLanguage, RTranslatorMode mode, Translator.GeneralListener listener){
726-
if(isUseTatoeba()) {
727-
translator.loadTatoeba(firstLanguage, secondLanguage, mode, new Translator.GeneralListener() {
728-
@Override
729-
public void onSuccess() {
730-
if (translator.getMode() == Translator.MOZILLA) {
731-
translator.loadMozillaModels(firstLanguage, secondLanguage, mode, listener);
732-
} else {
733-
if (listener != null) listener.onSuccess();
734-
}
735-
}
736-
});
737-
}else{
738-
if (translator.getMode() == Translator.MOZILLA) {
739-
translator.loadMozillaModels(firstLanguage, secondLanguage, mode, listener);
740-
} else {
741-
if (listener != null) listener.onSuccess();
742-
}
743-
}
728+
translator.loadLanguageResources(firstLanguage, secondLanguage, mode, listener);
744729
}
745730
}
746731

app/src/main/java/nie/translator/rtranslator/databases/tatoeba/TatoebaDbWrapper.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.database.sqlite.SQLiteDatabase;
55
import android.util.Log;
66

7+
import androidx.annotation.NonNull;
78
import androidx.annotation.Nullable;
89

910
import com.niedev.sqlite4java.SQLiteBlob;
@@ -46,7 +47,7 @@ public String getSentence(int id) {
4647
return out;
4748
}
4849

49-
@Nullable
50+
@NonNull
5051
public String[] getSentences(int[] ids) {
5152
String[] out = new String[ids.length];
5253
if (ids.length == 0) return out;
@@ -84,7 +85,7 @@ public String[] getSentences(int[] ids) {
8485
}
8586
} catch (SQLiteException e) {
8687
e.printStackTrace();
87-
out = null;
88+
out = new String[0];
8889
} finally {
8990
if (st != null) st.dispose();
9091
database.dispose();

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

Lines changed: 0 additions & 54 deletions
This file was deleted.
Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
package nie.translator.rtranslator.voice_translation.neural_networks.translation;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
import javax.annotation.Nullable;
9+
10+
import nie.translator.rtranslator.Global;
11+
import nie.translator.rtranslator.bluetooth.Peer;
12+
import nie.translator.rtranslator.tools.CustomLocale;
13+
14+
public class LanguageResourcesIndicator {
15+
public enum ResourceType {
16+
MOZILLA,
17+
TATOEBA,
18+
DICTIONARY
19+
}
20+
public CustomLocale[] textTranslationResources = new CustomLocale[2];
21+
public CustomLocale[] walkieTalkieResources = new CustomLocale[2];
22+
@Nullable
23+
public CustomLocale conversationTgtResource = null; //the target model of conversation translation is always our language for every peer
24+
public HashMap<String, CustomLocale> conversationSrcResources = new HashMap<>(); // key: peer uniqueName, value: srcLanguage
25+
private ArrayList<ResourceType> textTranslationResourcesLoaded = new ArrayList<>(3);
26+
private ArrayList<ResourceType> walkieTalkieResourcesLoaded = new ArrayList<>(3);
27+
private ArrayList<ResourceType> conversationResourcesLoaded = new ArrayList<>(3);
28+
29+
public ArrayList<CustomLocale> getAllUniqueResources(){
30+
ArrayList<CustomLocale> resources = new ArrayList<>();
31+
for (CustomLocale resource: textTranslationResources) {
32+
if(resource != null && !resources.contains(resource)){
33+
resources.add(resource);
34+
}
35+
}
36+
for (CustomLocale resource: walkieTalkieResources) {
37+
if(resource != null && !resources.contains(resource)){
38+
resources.add(resource);
39+
}
40+
}
41+
for (CustomLocale resource : conversationSrcResources.values()) {
42+
if (resource != null && !resources.contains(resource)) {
43+
resources.add(resource);
44+
}
45+
}
46+
if(conversationTgtResource != null && resources.contains(conversationTgtResource)){
47+
resources.add(conversationTgtResource);
48+
}
49+
return resources;
50+
}
51+
52+
public ArrayList<String> getAllUniqueResourcePairs(){
53+
ArrayList<String> pairs = new ArrayList<>();
54+
if(textTranslationResources[0] != null && textTranslationResources[1] != null){
55+
String pairCode = textTranslationResources[0].getISO3Language()+"-"+textTranslationResources[1].getISO3Language();
56+
if(!pairs.contains(pairCode)){
57+
pairs.add(pairCode);
58+
}
59+
}
60+
if(walkieTalkieResources[0] != null && walkieTalkieResources[1] != null){
61+
String pairCode = walkieTalkieResources[0].getISO3Language()+"-"+walkieTalkieResources[1].getISO3Language();
62+
if(!pairs.contains(pairCode)){
63+
pairs.add(pairCode);
64+
}
65+
}
66+
if(conversationTgtResource != null) {
67+
for (CustomLocale srcResource : conversationSrcResources.values()) {
68+
if (srcResource != null) {
69+
String pairCode = srcResource.getISO3Language()+"-"+conversationTgtResource.getISO3Language();
70+
if(!pairs.contains(pairCode)){
71+
pairs.add(pairCode);
72+
}
73+
}
74+
}
75+
}
76+
return pairs;
77+
}
78+
79+
public boolean isResourceContainedInOtherModes(CustomLocale lang, Global.RTranslatorMode currentMode){
80+
if(currentMode != Global.RTranslatorMode.TEXT_TRANSLATION_MODE && Arrays.asList(textTranslationResources).contains(lang)){
81+
return true;
82+
}
83+
if(currentMode != Global.RTranslatorMode.WALKIE_TALKIE_MODE && Arrays.asList(walkieTalkieResources).contains(lang)){
84+
return true;
85+
}
86+
if(currentMode != Global.RTranslatorMode.CONVERSATION_MODE){
87+
if(conversationSrcResources.containsValue(lang)){
88+
return true;
89+
}
90+
if(conversationTgtResource != null && conversationTgtResource.equals(lang)){
91+
return true;
92+
}
93+
}
94+
return false;
95+
}
96+
97+
public boolean isSrcResourceContainedInOtherPeers(CustomLocale lang, Peer peer){
98+
for (Map.Entry<String, CustomLocale> entry : conversationSrcResources.entrySet()) {
99+
String key = entry.getKey();
100+
CustomLocale value = entry.getValue();
101+
102+
if(!key.equals(peer.getUniqueName())){
103+
if(value.equals(lang)){
104+
return true;
105+
}
106+
}
107+
}
108+
return false;
109+
}
110+
111+
public boolean isResourcePairContainedInOtherModes(CustomLocale srcLang, CustomLocale tgtLang, Global.RTranslatorMode currentMode){
112+
if(currentMode != Global.RTranslatorMode.TEXT_TRANSLATION_MODE){
113+
if(textTranslationResources[0] != null && textTranslationResources[1] != null) {
114+
String pairCode = srcLang.getISO3Language() + "-" + tgtLang.getISO3Language();
115+
String pairCodeResource = textTranslationResources[0].getISO3Language() + "-" + textTranslationResources[1].getISO3Language();
116+
if(pairCode.equals(pairCodeResource)) {
117+
return true;
118+
}
119+
}
120+
}
121+
if(currentMode != Global.RTranslatorMode.WALKIE_TALKIE_MODE){
122+
if(walkieTalkieResources[0] != null && walkieTalkieResources[1] != null) {
123+
String pairCode = srcLang.getISO3Language() + "-" + tgtLang.getISO3Language();
124+
String pairCodeResource = walkieTalkieResources[0].getISO3Language() + "-" + walkieTalkieResources[1].getISO3Language();
125+
if(pairCode.equals(pairCodeResource)) {
126+
return true;
127+
}
128+
}
129+
}
130+
if(currentMode != Global.RTranslatorMode.CONVERSATION_MODE){
131+
for (CustomLocale srcResource : conversationSrcResources.values()) {
132+
if (srcResource != null && conversationTgtResource != null) {
133+
String pairCode = srcLang.getISO3Language() + "-" + tgtLang.getISO3Language();
134+
String pairCodeResource = srcResource.getISO3Language()+"-"+conversationTgtResource.getISO3Language();
135+
if(pairCode.equals(pairCodeResource)) {
136+
return true;
137+
}
138+
}
139+
}
140+
}
141+
return false;
142+
}
143+
144+
public boolean isResourcePairContainedInOtherPeers(CustomLocale srcLang, CustomLocale tgtLang, Peer peer){
145+
for (Map.Entry<String, CustomLocale> entry : conversationSrcResources.entrySet()) {
146+
String key = entry.getKey();
147+
CustomLocale value = entry.getValue();
148+
149+
if(!key.equals(peer.getUniqueName()) && value != null && conversationTgtResource != null){
150+
String pairCode = srcLang.getISO3Language() + "-" + tgtLang.getISO3Language();
151+
String pairCodeResource = value.getISO3Language()+"-"+conversationTgtResource.getISO3Language();
152+
if(pairCode.equals(pairCodeResource)) {
153+
return true;
154+
}
155+
}
156+
}
157+
return false;
158+
}
159+
160+
public boolean isResourceTypeLoaded(Global.RTranslatorMode rtranslatorMode, ResourceType resourceType){
161+
switch (rtranslatorMode){
162+
case TEXT_TRANSLATION_MODE:
163+
if(textTranslationResourcesLoaded.contains(resourceType)){
164+
return true;
165+
}
166+
break;
167+
case WALKIE_TALKIE_MODE:
168+
if(walkieTalkieResourcesLoaded.contains(resourceType)){
169+
return true;
170+
}
171+
break;
172+
case CONVERSATION_MODE:
173+
if(conversationResourcesLoaded.contains(resourceType)){
174+
return true;
175+
}
176+
break;
177+
}
178+
return false;
179+
}
180+
181+
public boolean isResourceTypeLoaded(ResourceType resourceType){
182+
return textTranslationResourcesLoaded.contains(resourceType) || walkieTalkieResourcesLoaded.contains(resourceType) || conversationResourcesLoaded.contains(resourceType);
183+
}
184+
185+
public void setResourceTypeLoadStatus(Global.RTranslatorMode rtranslatorMode, ResourceType resourceType, boolean loaded){
186+
switch (rtranslatorMode){
187+
case TEXT_TRANSLATION_MODE:
188+
if(textTranslationResourcesLoaded.contains(resourceType) && !loaded){
189+
textTranslationResourcesLoaded.remove(resourceType);
190+
}else if(!textTranslationResourcesLoaded.contains(resourceType) && loaded){
191+
textTranslationResourcesLoaded.add(resourceType);
192+
}
193+
break;
194+
case WALKIE_TALKIE_MODE:
195+
if(walkieTalkieResourcesLoaded.contains(resourceType) && !loaded){
196+
walkieTalkieResourcesLoaded.remove(resourceType);
197+
}else if(!walkieTalkieResourcesLoaded.contains(resourceType) && loaded){
198+
walkieTalkieResourcesLoaded.add(resourceType);
199+
}
200+
break;
201+
case CONVERSATION_MODE:
202+
if(conversationResourcesLoaded.contains(resourceType) && !loaded){
203+
conversationResourcesLoaded.remove(resourceType);
204+
}else if(!conversationResourcesLoaded.contains(resourceType) && loaded){
205+
conversationResourcesLoaded.add(resourceType);
206+
}
207+
break;
208+
}
209+
}
210+
211+
public void setResourceTypeLoadStatus(ResourceType resourceType, boolean loaded){
212+
if(textTranslationResourcesLoaded.contains(resourceType) && !loaded){
213+
textTranslationResourcesLoaded.remove(resourceType);
214+
}else if(!textTranslationResourcesLoaded.contains(resourceType) && loaded){
215+
textTranslationResourcesLoaded.add(resourceType);
216+
}
217+
218+
if(walkieTalkieResourcesLoaded.contains(resourceType) && !loaded){
219+
walkieTalkieResourcesLoaded.remove(resourceType);
220+
}else if(!walkieTalkieResourcesLoaded.contains(resourceType) && loaded){
221+
walkieTalkieResourcesLoaded.add(resourceType);
222+
}
223+
224+
if(conversationResourcesLoaded.contains(resourceType) && !loaded){
225+
conversationResourcesLoaded.remove(resourceType);
226+
}else if(!conversationResourcesLoaded.contains(resourceType) && loaded){
227+
conversationResourcesLoaded.add(resourceType);
228+
}
229+
}
230+
231+
public boolean areAllResourceTypeUnloaded(){
232+
return textTranslationResourcesLoaded.isEmpty() && walkieTalkieResourcesLoaded.isEmpty() && conversationResourcesLoaded.isEmpty();
233+
}
234+
235+
public void reset(){
236+
textTranslationResources = new CustomLocale[2];
237+
walkieTalkieResources = new CustomLocale[2];
238+
conversationSrcResources = new HashMap<>();
239+
conversationTgtResource = null;
240+
}
241+
}

0 commit comments

Comments
 (0)