Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            // Some example documents.
            //string[] documents =
            //{
            //    //"The sun in the Stars sky is bright.",
            //    //"We can see the shining sun, the bright sun."
            //    "How much is my balance",
            //    "I need my balance",
            //    "balance for account",
            //    "payment for my account",
            //    "hello need my balance"
            //};

            //// Apply TF*IDF to the documents and get the resulting vectors.
            //double[][] inputs = TFIDF.Transform(documents, 0);
            //inputs = TFIDF.Normalize(inputs);

            //double[] result = inputs[inputs.Length - 1];

            //for (int index = 0; index < inputs.Length-1; index++)
            //{
            //    Console.WriteLine(documents[index]);
            //    int counter = 0;
            //    double cosineValue = 0;
            //    foreach (double value in inputs[index])
            //    {
            //        cosineValue = cosineValue + (value * result[counter]);
            //        counter++;
            //    }
            //    Console.WriteLine(cosineValue);
            //    Console.WriteLine("\n");
            //}

            //Console.WriteLine("------------------------------");

            //for (int index = 0; index < inputs.Length; index++)
            //{
            //    Console.WriteLine(documents[index]);

            //    foreach (double value in inputs[index])
            //    {
            //        Console.Write(value + ", ");
            //    }

            //    Console.WriteLine("\n");
            //}

            Console.WriteLine("Levinstine Algorithm");
            Console.WriteLine("----------------------------------");

            //Console.WriteLine(LevenshteinDistance.Compute("Payments for Account", "Pyment for Account"));
            //Console.WriteLine(LevenshteinDistance.Compute("Payments for Account", "Balance for Account"));

            Console.WriteLine("TF IDF Cosine Similarity");
            Console.WriteLine("----------------------------------");
            int threshold = 0;

            try
            {
                // Create instance of calculator
                SimilarityCalculator sc = new SimilarityCalculator();

                sc.CompareString("Payments for Account", "Payment Balance for Account", vocabularyThreshold: threshold);
                sc.CompareString("Payments for Account", "Balance for Account", vocabularyThreshold: threshold);

                Console.WriteLine("Press any key...");
                Console.ReadKey();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            Console.ReadKey();
        }
Ejemplo n.º 2
0
        //ChaT Bot Reponse Main Entry
        public ChatIntent ChatResponseMain()
        {
            string responseMessage = contentManager.NoIntentMatchedResponse;
            TFIDF  getVocab        = new TFIDF();
            Dictionary <string, string> reponseDict   = new Dictionary <string, string>();
            List <ChatIntent>           intentListAll = db.ChatIntent.ToList();

            ChatIntent responseIntent = intentListAll.Where(x => x.ChatIntentId == 0).FirstOrDefault();

            #region 1.CheckIntentGreetingOrGoodbye
            if (hiBye.Greet())
            {
                return(UpdateIntent(Node, contentManager.GreetResponse, responseIntent));
            }
            else if (hiBye.GoodBye())
            {
                return(UpdateIntent(Node, contentManager.GoodbyeResponse, responseIntent));
            }
            #endregion

            List <ChatIntent> intentList = (from intention in intentListAll
                                            where intention.ChatIntentId > 2 && intention.ParentId == Node
                                            select intention).ToList();

            #region 2.CheckIntentFullMatchbySuggestion
            KeyValuePair <int, bool> fullMatch = suggestionMatch.FullSuggestionMatch(intentList);
            if (fullMatch.Value)
            {
                ChatIntent fullMatchIntent = intentList.Where(x => x.ChatIntentId == fullMatch.Key).FirstOrDefault();
                responseMessage = fullMatchIntent.Response;
                var hasEntity = (from ent in db.ChatEntity where ent.ChatIntentId == fullMatchIntent.ChatIntentId
                                 select ent);
                if (hasEntity.Any())
                {
                    AskMeEntityExtraction entity = new AskMeEntityExtraction(Message, fullMatchIntent.ChatIntentId, SessionId);
                    return(entity.GetEntityforIntentfromNLP(fullMatchIntent));
                }
                return(fullMatchIntent);
            }

            KeyValuePair <int, bool> partialMatch = suggestionMatch.PartialSuggestionMatch(intentList);
            if (partialMatch.Value)
            {
                ChatIntent partialMatchIntent = intentList.Where(x => x.ChatIntentId == partialMatch.Key).FirstOrDefault();
                responseMessage = partialMatchIntent.Response;
                var hasEntity = (from ent in db.ChatEntity
                                 where ent.ChatIntentId == partialMatchIntent.ChatIntentId
                                 select ent);
                if (hasEntity.Any())
                {
                    AskMeEntityExtraction entity = new AskMeEntityExtraction(Message, partialMatchIntent.ChatIntentId, SessionId);
                    return(entity.GetEntityforIntentfromNLP(partialMatchIntent));
                }
                return(partialMatchIntent);
            }
            #endregion


            List <string> vocabList = getVocab.GetVocabulary(Message);
            if (vocabList.Count == 0)
            {
                return(UpdateIntent(Node, contentManager.NoIntentMatchedResponse, responseIntent));
            }

            if (Message.ToLower() == "yes" || Message.ToLower() == "no")
            {
                return(UpdateIntent(Node, contentManager.NoIntentMatchedResponse, responseIntent));
            }

            #region 3.TFIDF Match Process
            SimilarityCalculator      similarityCalculator = new SimilarityCalculator();
            List <ChatIntentQuestion> questionList         = db.ChatIntentQuestion.ToList();
            Dictionary <int, double>  scoreDict            = new Dictionary <int, double>();
            foreach (ChatIntentQuestion question in questionList)
            {
                double compare = similarityCalculator.CompareString(Message, question.QuestionDesc, 1);
                KeyValuePair <int, double> score = new KeyValuePair <int, double>(question.ChatIntentId, compare);
                if (scoreDict.ContainsKey(score.Key))
                {
                    if (scoreDict[score.Key] < compare)
                    {
                        scoreDict[score.Key] = compare;
                    }
                }
                else
                {
                    scoreDict.Add(score.Key, score.Value);
                }
            }

            if (scoreDict.Where(x => x.Value > 0.45).Any())
            {
                int        maxScoreChatIntentId = scoreDict.OrderByDescending(x => x.Value).Select(y => y.Key).FirstOrDefault();
                ChatIntent maxIntent            = intentListAll.Where(x => x.ChatIntentId == maxScoreChatIntentId).FirstOrDefault();
                Node = maxScoreChatIntentId;

                var hasEntity = (from ent in db.ChatEntity
                                 where ent.ChatIntentId == maxIntent.ChatIntentId
                                 select ent);
                if (hasEntity.Any())
                {
                    AskMeEntityExtraction entity = new AskMeEntityExtraction(Message, maxIntent.ChatIntentId, SessionId);
                    return(entity.GetEntityforIntentfromNLP(maxIntent));
                }

                //KeyValuePair<int, string> responseIntent = GetEntityforIntent(Node, maxIntent.Response);
                return(maxIntent);
            }
            else if (scoreDict.Where(x => x.Value >= 0.23).Any())
            {
                List <int> possibeMatch = scoreDict.OrderByDescending(x => x.Value).Where(x => x.Value >= 0.23).Select(y => y.Key).ToList();
                responseMessage = contentManager.IntentPossibleMatchedResponse;
                foreach (int match in possibeMatch)
                {
                    responseMessage = responseMessage + ", ";
                    string suggestion = intentListAll.Where(x => x.ChatIntentId == match).Select(y => y.IntentDescription).FirstOrDefault();
                    responseMessage = responseMessage + suggestion;
                }
                responseMessage = responseMessage + ", " + contentManager.IntentSuggestionResponse;
                return(UpdateIntent(Node, responseMessage, responseIntent));
            }
            #endregion

            #region 4.Probable Match Process
            KeyValuePair <string, bool> probableMatchCorrect = zPossibleMatch.ProbableMatchCorrectSpelling(vocabList, intentListAll);
            if (probableMatchCorrect.Value)
            {
                common.LogFailureResponse();
                responseMessage = probableMatchCorrect.Key;
                return(UpdateIntent(Node, responseMessage, responseIntent));
            }

            KeyValuePair <string, bool> probableMatchTypo = zPossibleMatch.ProbableMatchTypoError(vocabList, intentListAll);
            if (probableMatchTypo.Value)
            {
                common.LogFailureResponse();
                responseMessage = probableMatchTypo.Key;
                return(UpdateIntent(Node, responseMessage, responseIntent));
            }
            #endregion

            #region 4.Synonym Match Process
            KeyValuePair <string, bool> synMatch = synonymMatch.SynonymMatch(vocabList, intentListAll);
            if (synMatch.Value)
            {
                common.LogFailureResponse();
                responseMessage = synMatch.Key;
                return(UpdateIntent(Node, responseMessage, responseIntent));
            }
            #endregion

            if (responseIntent != null)
            {
                responseIntent.ChatIntentId = Node;
                responseIntent.Response     = responseMessage;
            }
            else
            {
                responseIntent = new ChatIntent();
                responseIntent.ChatIntentId = Node;
                responseIntent.Response     = "Sorry I did not understand, Please enter one of the suggestions";
            }
            return(responseIntent);
        }