|
| 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