0

I'm aware of possible duplicates How does the Google "Did you mean?" Algorithm work? However, my problem is not related to Translation, Transliteration, or spelling corrector.

I am developing an application in Java that maps the English keyboard keys to the same corresponding Arabic key.

The best working example is Google's "Did you mean" algorithm:

  • Write in Arabic "صاشف هس فاهس؟"
  • Google will give the result: "Did you mean: what is this?"

An example is given here.

I have done this solution manually by making Enum with a map as follows:

package com.something.commands.text.model.enums;

import lombok.Getter;

import java.util.HashMap;

@Getter
public enum EKeyAlphabet {

    Q("q", "ض"),
    W("w", "ص"),
    E("e", "ث"),
    R("r", "ق"),
    T("t", "ف"),
    Y("y", "غ"),
    U("u", "ع"),
    I("i", "ه"),
    O("o","خ"),
    P("p", "ح"),
    LEFT_CURLY("{", "ج"),
    RIGHT_CURLY("}", "د"),
    A("a", "ش"),
    S("s", "س"),
    D("d", "ي"),
    F("f", "ب"),
    G("g", "ل"),
    H("h", "ا"),
    J("j", "ت"),
    K("k", "ن"),
    L("l", "م"),
    SEMICOLON(";", "ك"),
    COLON("'", "ط"),
    Z("z", "ئ"),
    X("x", "ء"),
    C("c", "ؤ"),
    V("v", "ر"),
    B("b", "لا"),
    N("n", "ى"),
    M("m", "ة"),
    COMMA(",", "و"),
    DOT(".", "ز"),
    SLASH("//", "ظ"),
    TICK("`", "ذ")

    ;



    private String enAlpha;
    private String arAlpha;
    private static HashMap<String, String> map;

    EKeyAlphabet(String enAlpha, String arAlpha){
       this.enAlpha = enAlpha;
       this.arAlpha = arAlpha;
    }

    public static String getEnAlpha(String arAlpha){
        if(map == null){
            initializeMap();
        }
        if(map.containsKey(arAlpha)){
            return map.get(arAlpha);
        }
        return null;
    }

    private static void initializeMap(){
        map = new HashMap<>();
        for (EKeyAlphabet alphabet: EKeyAlphabet.values()){
            map.put(alphabet.getArAlpha(), alphabet.getEnAlpha());
        }
    }
}

However, this solution has drawbacks:

  1. There are some possibilities of ambiguous mapping between characters (i.e.: "لا" can be produced by pressing "g" then "h" keyboard keys in Arabic Windows language, or can be produced directly by pressing "b" ONLY).
  2. Mapping special characters like "إ", " ً", " ِ", etc. is overwhelming.

So, in conclusion:

  1. Is there any third-party library or API that does that?
  2. Is it possible to accomplish this solution by converting the Unicode?
  3. Is it possible to do it in most languages (i.e.: not only English -> Arabic).
  • This might be a silly question from me, but why use an Enum and not a HashMap? – Bob th Jul 12 '22 at 18:09
  • Thank you for your comment, your solution is valid but has the same drawbacks that I mentioned it in my question. Further, when implementing your solution, we have to add all characters manually, I thought of Enum to do it in a more abstract way. – Noble amar-ninja Gamer Jul 12 '22 at 19:07
  • One solution might be to create a text file with the conversion characters. Your code would read the text file and create the `Map`. Yes, you have to manually create the text files for each language, but once they're made, they can be reused. – Gilbert Le Blanc Jul 12 '22 at 20:27
  • Thank you for the idea, but your solution still will not solve drawback [1] which I mentioned. Just to clarify the drawback, suppose I input the text in Arabic "لامشؤن", it will be ambiguous because it has two different mappings: 1- black. 2- ghlack. And the program will output it as "ghlack". (As it goes character by character). Because as I mentioned, "لا" can be written in 2 ways using the keyboard. Hence this solution won't work with a language that has a letter to be written in two different ways. – Noble amar-ninja Gamer Jul 12 '22 at 21:37
  • You could just make the `HashMap` output an `ArrayList` value with the different possible outcomes. And I'm not quite sure why "mapping special characters is overwhelming"; you can just use `HashMap.getOrDefault` – Bob th Jul 12 '22 at 23:36
  • I'm wondering why the operating system's input method is not enough. On Windows you can switch between multiple input methods with the mouse or by pressing Shift-Alt. So why all this coding for only one application? – Queeg Jul 13 '22 at 05:06
  • Exactly @HiranChaudhuri, I need a way to do it directly by the OS, and not doing it manually like this. – Noble amar-ninja Gamer Jul 16 '22 at 06:45
  • @Nobleamar-ninjaGamer But what you are coding and asking for is how to program something similar in Java. You should then close this question and raise one how to switch the OS input method programmatically. – Queeg Jul 17 '22 at 09:22

0 Answers0