/// <summary> /// Takes in the words of a mnemonic sentence and it rebuilds the word index, having the valid index allows us to hot swap between languages/word lists :) /// </summary> /// <param name="wordsInMnemonicSentence"> a string array containing each word in the mnemonic sentence</param> /// <returns>The word index that can be used to build the mnemonic sentence</returns> private List <int> pRebuildWordIndexes(string[] wordsInMnemonicSentence) { List <int> wordIndexList = new List <int>(); string langName = cEmptyString; Wordlists.Wordlist wordlist; switch (_language) { case Language.English: wordlist = new Wordlists.English(); langName = "English"; break; case Language.Japanese: wordlist = new Wordlists.Japanese(); langName = "Japanese"; break; case Language.Spanish: wordlist = new Wordlists.Spanish(); langName = "Spanish"; break; case Language.ChineseSimplified: wordlist = new Wordlists.ChineseSimplified(); langName = "Chinese Simplified"; break; case Language.ChineseTraditional: wordlist = new Wordlists.ChineseTraditional(); langName = "Chinese Traditional"; break; case Language.French: wordlist = new Wordlists.French(); langName = "French"; break; default: wordlist = new Wordlists.English(); langName = "English"; break; } foreach (string s in wordsInMnemonicSentence) { int idx = -1; if (!wordlist.WordExists(s, out idx)) { throw new Exception("Word " + s + " is not in the wordlist for language " + langName + " cannot continue to rebuild entropy from wordlist"); } wordIndexList.Add(idx); } return(wordIndexList); }
/// <summary> /// Takes in a string[] of words and detects the language that has the highest number of matching words. /// </summary> /// <param name="words">The words of which you wish to derive a language</param> /// <returns>The best attempt at a guessed Language</returns> public static Language AutoDetectLanguageOfWords(string[] words) { Wordlists.English eng = new Wordlists.English(); Wordlists.Japanese jp = new Wordlists.Japanese(); Wordlists.Spanish es = new Wordlists.Spanish(); Wordlists.French fr = new Wordlists.French(); Wordlists.ChineseSimplified cnS = new Wordlists.ChineseSimplified(); Wordlists.ChineseTraditional cnT = new Wordlists.ChineseTraditional(); List <int> languageCount = new List <int>(new int[] { 0, 0, 0, 0, 0, 0 }); int index; foreach (string s in words) { if (eng.WordExists(s, out index)) { //english is at 0 languageCount[0]++; } if (jp.WordExists(s, out index)) { //japanese is at 1 languageCount[1]++; } if (es.WordExists(s, out index)) { //spanish is at 2 languageCount[2]++; } if (cnS.WordExists(s, out index)) { //chinese simplified is at 3 languageCount[3]++; } if (cnT.WordExists(s, out index) && !cnS.WordExists(s, out index)) { //chinese traditional is at 4 languageCount[4]++; } if (fr.WordExists(s, out index)) { //french is at 5 languageCount[5]++; } } //no hits found for any language unknown if (languageCount.Max() == 0) { return(Language.Unknown); } if (languageCount.IndexOf(languageCount.Max()) == 0) { return(Language.English); } else if (languageCount.IndexOf(languageCount.Max()) == 1) { return(Language.Japanese); } else if (languageCount.IndexOf(languageCount.Max()) == 2) { return(Language.Spanish); } else if (languageCount.IndexOf(languageCount.Max()) == 3) { if (languageCount[4] > 0) { //has traditional characters so not simplified but instead traditional return(Language.ChineseTraditional); } return(Language.ChineseSimplified); } else if (languageCount.IndexOf(languageCount.Max()) == 4) { return(Language.ChineseTraditional); } else if (languageCount.IndexOf(languageCount.Max()) == 5) { return(Language.French); } return(Language.Unknown); }