示例#1
0
 public ZZStructure()
 {
     //words.Add(string.Empty, new ZZNode<string>(string.Empty));
     wordList.Add(new ZZNode <string>(string.Empty));
     lastSentence = new ZZNode <List <ZZNode <string> > >(new List <ZZNode <string> >());
     lines.Add(lastSentence);
     regex = new Regex("\\A[a-z0-9]+\\Z");
 }
示例#2
0
 bool WordEquals(ZZNode <string> other)
 {
     if (wordNode.GetData() == other.GetData())
     {
         wordNode = other;
         return(true);
     }
     return(false);
 }
示例#3
0
 bool ListEquals(ZZNode <List <ZZNode <string> > > other)
 {
     if (sentenceNode.GetData().Count != other.GetData().Count)
     {
         return(false);
     }
     for (int x = 0; x < sentenceNode.GetData().Count; x++)
     {
         if (sentenceNode.GetData()[x] != other.GetData()[x])
         {
             return(false);
         }
     }
     sentenceNode = other;
     return(true);
 }
示例#4
0
 public bool Equals(ZZNode <T> other)
 {
     return(data.Equals(other.data));
 }
示例#5
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));
                    }
                }
            }
        }
示例#6
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);
        }