Beispiel #1
0
        //communicate with perceptrons
        private String forwardPropagateData(float word, float lastWordSet)
        {
            float value = 0.0F;

            //iterate over neural net
            float[] inputs = new float[] { word, lastWordSet };
            for (int index = 1; index < perceptrons.Count; index++)
            {
                List <Perceptron> layer        = perceptrons[index];
                float[]           inputsBuffer = new float[layer.Count];

                int perceptronIndex = 0;
                foreach (Perceptron p in layer)
                {
                    inputsBuffer[perceptronIndex] = p.output(inputs);

                    perceptronIndex++;
                }

                //feed forward
                inputs = inputsBuffer;
            }

            value = (float)Math.Round(inputs[0] / 100, 2);
            value = Map(value, 0, 5, 0.00F, ChatbotCore.loadedDataBase.Count * ChatbotCore.dataBaseWordOffset);
            value = (float)Math.Round(inputs[0], 2);
            Console.WriteLine(value);
            return(ChatbotCore.NumberToWord(value));
        }
Beispiel #2
0
        //output data
        public String OutputDataFromNetwork(String data)
        {
            List <String> returnMessage = new List <String>();

            //parse words
            String        wordToAddToList = "";
            List <String> wordList        = new List <String>();

            for (int index = 0; index < data.Length; index++)
            {
                String c = data.Substring(index, 1);

                if (c.Equals(" "))
                {
                    wordList.Add(wordToAddToList);
                    wordToAddToList = "";
                }
                else
                {
                    wordToAddToList += c;
                }
            }
            wordList.Add(wordToAddToList);

            //add words that to database that aren't in it
            foreach (String word in wordList)
            {
                if (!ChatbotCore.loadedDataBase.ContainsKey(word))
                {
                    ChatbotCore.AddWordToDataBase(word);
                }

                if (returnMessage.Count > 0)
                {
                    returnMessage.Add(forwardPropagateData(ChatbotCore.WordToNumber(word), ChatbotCore.WordToNumber(returnMessage[returnMessage.Count - 1])));
                }
                else
                {
                    returnMessage.Add(forwardPropagateData(ChatbotCore.WordToNumber(word), -1));
                }
            }

            //return text
            String r = "";

            foreach (String word in returnMessage)
            {
                r += word + " ";
            }

            //return text
            return(r);
        }
Beispiel #3
0
        //train perceptrons
        public void trainPerceptrons(String user1Words, String user2Words)
        {
            //iterate over words
            List <String> inputWords  = new List <String>();
            List <String> targetWords = new List <String>();
            String        currentWord = "";

            for (int index = 0; index < user1Words.Length; index++)
            {
                String c = user1Words.Substring(index, 1);

                if (c.Equals(" "))
                {
                    inputWords.Add(currentWord);
                    currentWord = "";
                }
                else
                {
                    currentWord += c;
                }
            }

            for (int index = 0; index < user2Words.Length; index++)
            {
                String c = user2Words.Substring(index, 1);

                if (c.Equals(" "))
                {
                    targetWords.Add(currentWord);
                    currentWord = "";
                }
                else
                {
                    currentWord += c;
                }
            }

            //iterate over longer list of words for training
            for (int index = 0; index < (inputWords.Count > targetWords.Count ? targetWords.Count : inputWords.Count); index++)
            {
                float inputWord  = ChatbotCore.WordToNumber(inputWords[index]);
                float targetWord = ChatbotCore.WordToNumber(targetWords[index]);

                backPropagateData(inputWord, targetWord);
            }
        }