/// <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> /// Uses the Wordlist Index to create a scentence ow words provided by the wordlist of this objects language attribute /// </summary> /// <returns>A scentence of words</returns> private string pGetMnemonicSentence() { //trap for words that were not in the word list when built. If custom words were used, we will not support the rebuild as we don't have the words if (_wordIndexList.Contains(-1)) { throw new Exception("the wordlist index contains -1 which means words were used in the mnemonic sentence that cannot be found in the wordlist and the index to sentence feature cannot be used. Perhaps a different language wordlist is needed?"); } string mSentence = cEmptyString; Wordlists.Wordlist wordlist; switch (_language) { case Language.English: wordlist = new Wordlists.English(); break; case Language.Japanese: wordlist = new Wordlists.Japanese(); break; case Language.Spanish: wordlist = new Wordlists.Spanish(); break; case Language.ChineseSimplified: wordlist = new Wordlists.ChineseSimplified(); break; case Language.ChineseTraditional: wordlist = new Wordlists.ChineseTraditional(); break; case Language.French: wordlist = new Wordlists.French(); break; default: wordlist = new Wordlists.English(); break; } for (int i = 0; i < _wordIndexList.Count; i++) { mSentence += wordlist.GetWordAtIndex(_wordIndexList[i]); if (i + 1 < _wordIndexList.Count) { mSentence += " "; } } return(mSentence); }
/// <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); }