Example #1
0
 public void AddBackLink(ZZLink <ZZNode <T>, ZZNode <T> > backLink)
 {
     foreach (ZZLink <ZZNode <T>, ZZNode <T> > l in backLinks)
     {
         if (l.Equals(backLink))
         {
             l.Strength++;
             return;
         }
     }
     backLinks.Add(backLink);
 }
Example #2
0
 public void AddNextLink(ZZLink <ZZNode <T>, ZZNode <T> > nextLink)
 {
     foreach (ZZLink <ZZNode <T>, ZZNode <T> > l in nextLinks)
     {
         if (l.Equals(nextLink))
         {
             l.Strength++;
             return;
         }
     }
     nextLinks.Add(nextLink);
 }
Example #3
0
 public void AddSideLink(ZZLink <ZZNode <T>, ZZNode <List <ZZNode <T> > > > sideLink)
 {
     foreach (ZZLink <ZZNode <T>, ZZNode <List <ZZNode <T> > > > l in sideLinks)
     {
         if (l.Equals(sideLink))
         {
             l.Strength++;
             return;
         }
     }
     sideLinks.Add(sideLink);
 }
Example #4
0
        public ZZLink <ZZNode <T>, ZZNode <List <ZZNode <T> > > > GetStrongestSideLink()
        {
            ZZLink <ZZNode <T>, ZZNode <List <ZZNode <T> > > > ret = null;
            int strongest = int.MinValue;

            foreach (ZZLink <ZZNode <T>, ZZNode <List <ZZNode <T> > > > link in sideLinks)
            {
                if (link.Strength > strongest)
                {
                    ret       = link;
                    strongest = link.Strength;
                }
            }
            return(ret);
        }
Example #5
0
        public ZZLink <ZZNode <T>, ZZNode <List <ZZNode <T> > > > GetRandomSideLink()
        {
            int total = 0;
            ZZLink <ZZNode <T>, ZZNode <List <ZZNode <T> > > > ret = null;

            foreach (ZZLink <ZZNode <T>, ZZNode <List <ZZNode <T> > > > link in sideLinks)
            {
                total += link.Strength;
            }
            int position = 0;
            int rand     = new Random((int)DateTime.Now.Ticks).Next(total);

            foreach (ZZLink <ZZNode <T>, ZZNode <List <ZZNode <T> > > > link in sideLinks)
            {
                position += link.Strength;
                if (position > rand)
                {
                    ret = link;
                    break;
                }
            }
            return(ret);
        }
Example #6
0
 public bool Equals(ZZLink <T, F> other)
 {
     return(to.Equals(other.to) && from.Equals(other.from));
 }
Example #7
0
 public int CompareTo(ZZLink <T, F> other)
 {
     return(strength.CompareTo(other.strength));
 }
Example #8
0
        public void AddSentence(string sentence)
        {
            sentence     = sentence.ToLower();
            sentenceNode = null;

            List <ZZNode <string> > sentenceList = new List <ZZNode <string> >();

            string[] words = sentence.Split(' ');
            foreach (string word in words)
            {
                if (regex.Match(word).Success)
                {
                    wordNode = new ZZNode <string>(word);
                    if (!this.wordList.Exists(WordEquals))
                    {
                        wordNode = new ZZNode <string>(word);
                        this.wordList.Add(wordNode);
                    }
                    sentenceList.Add(wordNode);
                }
            }
            if (sentenceList.Count == 0)
            {
                return;
            }
            sentenceNode = new ZZNode <List <ZZNode <string> > >(sentenceList);
            if (!lines.Exists(ListEquals))
            {
                lines.Add(sentenceNode);
            }

            for (int x = 0; x < sentenceList.Count; x++)
            {
                ZZNode <string> w = sentenceList[x];
                w.AddSideLink(new ZZLink <ZZNode <string>, ZZNode <List <ZZNode <string> > > >(sentenceNode, w, 2));
            }

            if (!lastSentence.Equals(sentenceNode))
            {
                lastSentence.AddNextLink(new ZZLink <ZZNode <List <ZZNode <string> > >, ZZNode <List <ZZNode <string> > > >(lastSentence, sentenceNode, 3));
                sentenceNode.AddBackLink(new ZZLink <ZZNode <List <ZZNode <string> > >, ZZNode <List <ZZNode <string> > > >(sentenceNode, lastSentence, 3));
            }
            lastSentence = sentenceNode;

            for (int x = 0; x < sentenceList.Count; x++)
            {
                ZZNode <string> word = sentenceList[x];

                if (x == 0 && x == words.Length - 1)
                {
                    //add terminator to last and next
                    wordNode = new ZZNode <string>(string.Empty);
                    wordList.Exists(WordEquals);
                    ZZLink <ZZNode <string>, ZZNode <string> > backNode = new ZZLink <ZZNode <string>, ZZNode <string> >(wordNode, word, 1);
                    ZZLink <ZZNode <string>, ZZNode <string> > nextNode = new ZZLink <ZZNode <string>, ZZNode <string> >(word, wordNode, 1);
                    wordNode.AddNextLink(backNode);
                    wordNode.AddBackLink(backNode);
                    word.AddBackLink(nextNode);
                    word.AddNextLink(nextNode);
                }
                else if (x == 0)
                {
                    //add terminator to last
                    wordNode = new ZZNode <string>(string.Empty);
                    wordList.Exists(WordEquals);
                    wordNode.AddNextLink(new ZZLink <ZZNode <string>, ZZNode <string> >(wordNode, word, 1));
                    word.AddBackLink(new ZZLink <ZZNode <string>, ZZNode <string> >(word, wordNode, 1));
                    wordNode = new ZZNode <string>(words[x + 1]);
                    if (wordList.Exists(WordEquals))
                    {
                        word.AddNextLink(new ZZLink <ZZNode <string>, ZZNode <string> >(word, wordNode, 1));
                        wordNode.AddBackLink(new ZZLink <ZZNode <string>, ZZNode <string> >(wordNode, word, 1));
                    }
                }
                else if (x == words.Length - 1)
                {
                    //add terminator to next
                    wordNode = new ZZNode <string>(string.Empty);
                    wordList.Exists(WordEquals);
                    wordNode.AddBackLink(new ZZLink <ZZNode <string>, ZZNode <string> >(wordNode, word, 1));
                    word.AddNextLink(new ZZLink <ZZNode <string>, ZZNode <string> >(word, wordNode, 1));
                }
                else
                {
                    //treat it normally
                    wordNode = new ZZNode <string>(words[x + 1]);
                    if (wordList.Exists(WordEquals) && wordNode != word)
                    {
                        word.AddNextLink(new ZZLink <ZZNode <string>, ZZNode <string> >(word, wordNode, 1));
                        wordNode.AddBackLink(new ZZLink <ZZNode <string>, ZZNode <string> >(wordNode, word, 1));
                    }
                }
            }
        }
Example #9
0
        public string GenerateResponse(string input)
        {
            input = input.ToLower();
            ZZNode <string> popWord   = null;
            int             strongest = int.MinValue;

            foreach (string s in input.Split(' '))
            {
                wordNode = new ZZNode <string>(s);
                if (this.wordList.Exists(WordEquals))
                {
                    if (wordNode.GetSideLinkStrength() > strongest && wordNode.GetStrongestSideLink() != null)
                    {
                        popWord   = wordNode;
                        strongest = wordNode.GetSideLinkStrength();
                    }
                }
            }

            if (popWord == null)
            {
                return(null);
            }

            ZZNode <List <ZZNode <string> > > sent = null;

            while (sent == null)
            {
                sent = popWord.GetRandomSideLink().GetFrom();
            }
            if (sent.GetStrongestNextLink() != null)
            {
                sent = sent.GetStrongestNextLink().GetTo();
            }

            popWord = sent.GetData()[new Random().Next(sent.GetData().Count)];

            Stack <string> backWords = new Stack <string>();
            Queue <string> nextWords = new Queue <string>();
            ZZLink <ZZNode <string>, ZZNode <string> > tempLink = popWord.GetRandomBackLink();
            ZZNode <string> tempWord = popWord;

            while (tempLink != null && tempLink.GetTo() != null && !string.IsNullOrEmpty(tempLink.GetTo().GetData()) && !backWords.Contains(tempLink.GetTo().GetData()) && popWord.GetData() != tempLink.GetTo().GetData())
            {
                tempWord = tempLink.GetTo();
                backWords.Push(tempWord.GetData());
                tempLink = tempWord.GetRandomBackLink();
            }
            tempLink = popWord.GetRandomNextLink();
            tempWord = popWord;
            while (tempLink != null && tempLink.GetTo() != null && !string.IsNullOrEmpty(tempLink.GetTo().GetData()) && !nextWords.Contains(tempLink.GetTo().GetData()) && popWord.GetData() != tempLink.GetTo().GetData())
            {
                tempWord = tempLink.GetTo();
                nextWords.Enqueue(tempWord.GetData());
                tempLink = tempWord.GetRandomNextLink();
            }
            string ret = "";

            while (backWords.Count != 0)
            {
                ret += backWords.Pop() + " ";
            }
            ret += popWord.GetData();
            if (nextWords.Count != 0)
            {
                ret += " ";
            }
            while (nextWords.Count != 0)
            {
                ret += nextWords.Dequeue();
                if (nextWords.Count != 0)
                {
                    ret += " ";
                }
            }
            return(ret);
        }