IPA/Dekvu Converter edit

Goal edit

convert Dekvu transliteration into IPA

Output edit

(expected)

[lɪ nokjəθ~ðou~u:ʃ (.) jəjək͡xʷəbu θ~ðɛ k͡xʷəbutoz]
[k͡xʷəvu]
[h dogɪk k͡xʷəbu jək͡xʷə kou~u:ftotaɪ]

Source Code edit

Main.java

public class Main{
    public static void main(String[] args){
        Dekvu d = new Dekvu("li nokyathoush, yayakwabu the kwabutoz. kwavu.");//In our house, all food must be eaten. Hello.
        System.out.println(d.toIPA());
        Dekvu d2 = new Dekvu("h dogik kwabu yakwa kouftotï.");//The dogs eat well tonight.
        System.out.println(d2.toIPA());
    }
}


Dekvu.java

import java.util.ArrayList;
public class Dekvu {
	private final String[][] tokens = {
		{},
		{" ","'",",",".","a","b","d","e","f","g","h","i","k","l","m","n","o","p","r","s","t","u","v","w","y","z","ä","ë","ï","ü"},
		{"sh","th","ou"},
		{"kwa"}
	};
	private final String[] vowels = {"a","e","i","o","u","ä","ë","ï","ou","ü"};
	private String str;
	
	public Dekvu(String str){
		while(str.contains(". ")){
		    int i = str.indexOf(". ")+1;
		    str = str.substring(0,i) + str.substring(i+1);
		}
		this.str = str;
	}
	
	//maybe add syllable recognition?
	public String toIPA(){
		String inp = this.str;
		ArrayList<String> tokenised = new ArrayList<String>();
		
		int i = inp.length();
		while(i >= 0){//loop through inp
			boolean found = false;//detects if a token was found
			if(!found){
				for(int len = 3; len >= 1; len--){//starts with length 3 tokens, working down to length 1 tokens
					if(i>=len){//prevents negative indexes
						String sample = inp.substring(i-len,i);
						for(int j=0; j<tokens[len].length; j++){//loop through all tokens of a given length
							if(tokens[len][j].equals(sample)){
								tokenised.add(0,sample);
								i-=len-1;
								inp=inp.substring(0,i);
								found=true;
							}
							if(found) break;
						}
					}
					if(found) break;
				}
			}
			i--;
		}
		String tokenString="";
		for(String s : tokenised) tokenString+=s+"_";
		tokenString = tokenString.substring(0,tokenString.length()-1);
		String[] tokenArr = tokenString.split("_");
		return toIPA(tokenArr);
	}
	
	private String toIPA(String[] tokens){
		String IPA = "[";
		
		for(int i=0; i<tokens.length; i++){
			switch(tokens[i]){
			    case " ": IPA += " "; break;
				case "'": IPA += "ʔ"; break;
				case ",": IPA += " (.)"; break;
				case ".": IPA += "]\n["; break;
				case "a": IPA += "ə"; break;
				case "b": IPA += "b"; break;
				case "d": IPA += "d"; break;
				case "e": IPA += "ɛ"; break;
				case "f": IPA += "f"; break;
				case "g": IPA += "g"; break;
				case "h": IPA += "h"; break;
				case "i": IPA += "ɪ"; break;
				case "k": IPA += "k"; break;
				case "l": IPA += "l"; break;
				case "m": IPA += "m"; break;
				case "n": IPA += "n"; break;
				case "o": IPA += "o"; break;
				case "p": IPA += "p"; break;
				case "r": IPA += "ɾ"; break;
				case "s": IPA += "s"; break;
				case "t": IPA += "t"; break;
				case "u": IPA += "u"; break;
				case "v": IPA += "v"; break;
				case "w": IPA += "w"; break;
				case "y": IPA += "j"; break;
				case "z": IPA += "z"; break;
				case "ä": IPA += "eɪ"; break;
				case "ë": IPA += "i"; break;
				case "ï": IPA += "aɪ"; break;
				case "ü": IPA += "ju"; break;
				case "sh": IPA += "ʃ"; break;
				case "th": IPA += "θ~ð"; break;
				case "ou": IPA += "ou~u:"; break;
				case "kwa": IPA += "k͡xʷə"+(find(tokens[i+1],vowels)?".":""); break;
			}
		}
		return IPA.substring(0,IPA.length()-(IPA.contains("]\n[")?3:0))+"]";
	}
	
	private boolean find(String item, String[] arr){
		for(int i=0; i<arr.length; i++) if(arr[i].equals(item)) return true; else return false;
	}
}