示例#1
0
        public static Language AutoDetectLanguage(string[] words)
        {
            var languageCount = new List <int>(new[] { 0, 0, 0, 0, 0, 0, 0 });
            int index;

            foreach (var s in words)
            {
                if (English.WordExists(s, out index))
                {
                    //english is at 0
                    languageCount[0]++;
                }

                if (Japanese.WordExists(s, out index))
                {
                    //japanese is at 1
                    languageCount[1]++;
                }

                if (Spanish.WordExists(s, out index))
                {
                    //spanish is at 2
                    languageCount[2]++;
                }

                if (ChineseSimplified.WordExists(s, out index))
                {
                    //chinese simplified is at 3
                    languageCount[3]++;
                }

                if (ChineseTraditional.WordExists(s, out index) && !ChineseSimplified.WordExists(s, out index))
                {
                    //chinese traditional is at 4
                    languageCount[4]++;
                }

                if (French.WordExists(s, out index))
                {
                    languageCount[5]++;
                }

                if (PortugueseBrazil.WordExists(s, out index))
                {
                    //portuguese_brazil is at 6
                    languageCount[6]++;
                }
            }

            //no hits found for any language unknown
            if (languageCount.Max() == 0)
            {
                return(Language.Unknown);
            }

            if (languageCount.IndexOf(languageCount.Max()) == 0)
            {
                return(Language.English);
            }

            if (languageCount.IndexOf(languageCount.Max()) == 1)
            {
                return(Language.Japanese);
            }

            if (languageCount.IndexOf(languageCount.Max()) == 2)
            {
                return(Language.Spanish);
            }

            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);
            }

            if (languageCount.IndexOf(languageCount.Max()) == 4)
            {
                return(Language.ChineseTraditional);
            }

            if (languageCount.IndexOf(languageCount.Max()) == 5)
            {
                return(Language.French);
            }

            if (languageCount.IndexOf(languageCount.Max()) == 6)
            {
                return(Language.PortugueseBrazil);
            }

            return(Language.Unknown);
        }
示例#2
0
        /////// <summary>
        /////// An asynchronous static method to create a new BIP39 from random entropy. The random entropy creation is CPU intensive so is run in its own Task and we await as per async pattern.
        /////// </summary>
        /////// <param name="entropySize">The size in bits of the entropy to be created</param>
        /////// <param name="passphrase">The optional passphrase. Please ensure NFKD Normalized, Empty string will be used if not provided as per spec</param>
        /////// <param name="language">The optional language. If no language is provided English will be used</param>
        /////// <returns>A BIP39 object</returns>
        ////public static async Task<BIP39> GetBIP39Async(int entropySize = cMinimumEntropyBits, string passphrase = cEmptyString, Language language = Language.English)
        ////{
        ////    byte[] entropyBytes = await Utilities.GetRandomBytesAsync(entropySize / cBitsInByte);
        ////    return new BIP39(entropyBytes, passphrase, language);
        ////}

        /// <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)
        {
            English            eng = new English();
            Japanese           jp  = new Japanese();
            Spanish            es  = new Spanish();
            French             fr  = new French();
            ChineseSimplified  cnS = new ChineseSimplified();
            ChineseTraditional cnT = new ChineseTraditional();

            List <int> languageCount = new List <int>(new[] { 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);
            }

            if (languageCount.IndexOf(languageCount.Max()) == 1)
            {
                return(Language.Japanese);
            }

            if (languageCount.IndexOf(languageCount.Max()) == 2)
            {
                return(Language.Spanish);
            }

            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);
            }

            if (languageCount.IndexOf(languageCount.Max()) == 4)
            {
                return(Language.ChineseTraditional);
            }

            if (languageCount.IndexOf(languageCount.Max()) == 5)
            {
                return(Language.French);
            }

            return(Language.Unknown);
        }
示例#3
0
        public static Language AutoDetectLanguage(string[] words)
        {
            List <int> languageCount = new List <int>(new int[] { 0, 0, 0, 0, 0, 0, 0 });
            int        index;

            foreach (string s in words)
            {
                if (Japanese.WordExists(s, out index))
                {
                    languageCount[1]++;
                }
                if (Spanish.WordExists(s, out index))
                {
                    //spanish is at 2
                    languageCount[2]++;
                }
                if (ChineseSimplified.WordExists(s, out index))
                {
                    languageCount[3]++;
                }
                if (French.WordExists(s, out index))
                {
                    languageCount[5]++;
                }
            }
            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)
                {
                    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);
            }
            else if (languageCount.IndexOf(languageCount.Max()) == 6)
            {
                return(Language.PortugueseBrazil);
            }
            return(Language.Unknown);
        }