Example #1
0
        public string TransliterateFinal(string word, bool addDash = false)
        {
            if (word == string.Empty)
            {
                return(string.Empty);
            }

            word  = TransliteratorHelper.AddBreaks(word);
            word += " ";
            string trans = TransliterateWithJokersAndWordCapitals(word, addDash);

            trans = trans.Substring(0, trans.Length - 1);
            trans = TransliteratorHelper.RemoveBreaks(trans);
            if (addDash && trans.Last() == DASH)
            {
                trans = trans.Substring(0, trans.Length - 1);
            }
            return(trans);
        }
Example #2
0
        internal string TransliterateWithJokersAndWordCapitals(string text, bool addDash)
        {
            bool          inTheWord            = false;
            string        lastWord             = string.Empty;
            StringBuilder ret                  = new StringBuilder();
            int           lastDestWordStartPos = -1;
            string        lowercaseText        = text.ToLower();

            while (text.Length > 0)
            {
                // Empty string indicates no rule found so far
                string key = string.Empty;

                if (!char.IsLetter(text, 0))
                {
                    if (inTheWord)
                    {
                        while (lastDestWordStartPos < ret.Length && ret[lastDestWordStartPos] == '|')
                        {
                            lastDestWordStartPos++;
                        }

                        switch (TransliteratorHelper.Determine(lastWord.Trim('|')))
                        {
                        case WordCapitals.CapitalLetter:
                            if (lastDestWordStartPos < ret.Length)
                            {
                                ret[lastDestWordStartPos] = ret[lastDestWordStartPos].ToString().ToUpper()[0];
                            }
                            break;

                        case WordCapitals.UpperCase:
                            for (int pos = lastDestWordStartPos; pos < ret.Length; pos++)
                            {
                                ret[pos] = ret[pos].ToString().ToUpper()[0];
                            }
                            break;
                        }
                    }
                    else
                    {
                        lastWord             = string.Empty;
                        lastDestWordStartPos = ret.Length;
                    }

                    inTheWord = !inTheWord;
                }

                //Try to find the longest rule with minimum number of jokers
                foreach (var rule in rules)
                {
                    if (Match(rule.Key, lowercaseText) && CompareRules(rule.Key, key))
                    {
                        key = rule.Key;
                    }
                }

                // If no rule was found, then paste one char from input into output
                if (key == string.Empty)
                {
                    ret.Append(text[0]);
                    if (addDash)
                    {
                        ret.Append(DASH);
                    }
                    lastWord     += text[0];
                    text          = text.Substring(1);
                    lowercaseText = lowercaseText.Substring(1);
                }
                else
                {
                    string value = rules[key];

                    string add = string.Empty;

                    //Iterate for each character within the value
                    foreach (char c in value)
                    {
                        if (jokers.ContainsKey(c))
                        {
                            // Find position of the joker in the key
                            int pos = key.IndexOf(c);

                            #if DEBUG
                            // If the joker is not found in the key
                            if (pos == -1)
                            {
                                continue;
                            }
                            //throw new ArgumentException("No joker '" + c + "' in key found!");
                            #endif

                            // Transliterate the character masked by joker and add it into the ret
                            add += this[lowercaseText[pos].ToString()];
                        }
                        else
                        {
                            add += c;
                        }
                    }
                    ret.Append(add);
                    if (addDash && ret[ret.Length - 1] != DASH)
                    {
                        ret.Append(DASH);
                    }

                    lastWord     += text.Substring(0, key.Length);
                    text          = text.Substring(key.Length);
                    lowercaseText = lowercaseText.Substring(key.Length);
                }
            }

            if (addDash && ret[ret.Length - 1] == DASH)
            {
                ret.Remove(ret.Length - 1, 1);
            }

            return(ret.ToString());
        }