Пример #1
0
        }//replaceSynonyms(string input, Dictionary<string, Synonym> synonymGroups)

        public static string ReplaceOutputSynonyms(string text, Dictionary <string, Synonym> synonyms)
        {
            int start = text.IndexOf("(");
            int end;

            while (start != -1)
            {
                if (!TextToolbox.IsEscaped(text, start))
                {
                    end = TextToolbox.FindNextMatchingChar(text, start);
                    if (end != -1)
                    {
                        string synonymName = text.Substring(start + 1, end - start - 1).Trim().ToLower();
                        if (synonyms.ContainsKey(synonymName) && !TextToolbox.IsInCommand(text, start, end - start))
                        {
                            Synonym syn = ((Synonym)synonyms[synonymName]);
                            if (syn.Phrases.Count > 0)
                            {
                                Random rand = new Random();
                                int    pick = rand.Next(syn.Phrases.Count);
                                if (end == text.Length - 1)                               //the end is the end
                                {
                                    text = text.Substring(0, start) + ((Phrase)syn.Phrases[pick]).Text;
                                }
                                else
                                {
                                    text = text.Substring(0, start) + ((Phrase)syn.Phrases[pick]).Text + text.Substring(end + 1);
                                }
                            }
                            else
                            {
                                text = text.Substring(0, start + 1) + synonymName + text.Substring(end);
                            }
                        }
                        start = text.IndexOf("(", start + 1);
                    }
                    else
                    {
                        //TODO: log an error somewhere?
                        return(text);
                    }
                }
                else                 //it's escaped \(..
                {
                    //remove the '\'
                    text = text.Remove(start - 1, 1);
                    //find the next start
                    if (start >= text.Length - 1)                   //the old start is now at the end
                    {
                        start = -1;
                    }
                    else
                    {
                        start = text.IndexOf("(", start);
                    }
                }
            }            //while
            return(text);
        }//replaceSynonyms(string input, Dictionary<string, Synonym> synonymGroups)
Пример #2
0
        }        //doTextReplacements(string text)

        public string DoAgentTextReplacements(string text)
        {
            foreach (Replacement r in this.replacements)
            {
                if (r.TextToFind != null && r.TextToFind != "")
                {
                    int pos = text.IndexOf(r.TextToFind);
                    while (pos != -1)
                    {
                        if (!TextToolbox.IsInCommand(text, pos, r.TextToFind.Length))
                        {
                            if (pos + r.TextToFind.Length < text.Length - 1)
                            {
                                text = text.Substring(0, pos)
                                       + r.TextForAgent
                                       + text.Substring(pos + r.TextToFind.Length);
                            }
                            else
                            {
                                text = text.Substring(0, pos)
                                       + r.TextForAgent;
                            }
                        }
                        if (pos < text.Length - 1)
                        {
                            pos = text.IndexOf(r.TextToFind, pos + 1);
                        }
                        else
                        {
                            pos = -1;
                        }
                    }    //while
                }        //if
            }            //foreach

            return(text);
        }        //doAgentTextReplacements(string text)