コード例 #1
0
        public void TestChat()
        {
            var serviceProvider = new ServiceCollection()
                                  .AddLogging()
                                  .BuildServiceProvider();

            var factory = serviceProvider.GetService <ILoggerFactory>();
            var logger  = factory.CreateLogger <Bot>();
            int userId  = 1;

            var mockUserService = new Mock <IBotUserService>();

            mockUserService.Setup(x => x.GetTopic(It.IsAny <int>())).Returns("");

            var mockResultService = new Mock <IUserResultService>();

            mockResultService.Setup(x => x.GetLastOutput(It.IsAny <int>())).Returns("");

            var mockPredicateService = new Mock <IUserPredicateService>();

            var mockRequestService = new Mock <IUserRequestService>();


            IBotConfigurationLoader configurationLoader = new ConfigurationLoader();
            IBotUserService         botUserService      = mockUserService.Object;
            IUserResultService      resultService       = mockResultService.Object;
            IUserPredicateService   predicateService    = mockPredicateService.Object;
            IUserRequestService     requestService      = mockRequestService.Object;
            IApplicationService     applicationService  = GetMockAppService();

            SettingsDictionary substitutions = new SettingsDictionary();
            string             inputString   = "";

            Normalize.ApplySubstitutions substitutor = new Normalize.ApplySubstitutions(substitutions, inputString);
            Regex strippers = new Regex("[^0-9a-zA-Z]");

            Normalize.StripIllegalCharacters stripper = new Normalize.StripIllegalCharacters(strippers);
            IAimlLoader loader = new AIMLLoader(logger, substitutor, stripper, true, 1000);
            Bot         bot    = new Bot(configurationLoader, logger, botUserService, resultService, loader, predicateService, requestService, applicationService);

            bot.Initialize();
            string expectedResult = "Because I am a piece of software.";
            var    result         = bot.Chat(new Request("Why do you live in a computer?", userId));

            Assert.NotNull(result);
            Assert.Equal(expectedResult, result.Output);
        }
コード例 #2
0
        /// <summary>
        /// Given an input, provide a normalized output
        /// </summary>
        /// <param name="input">The string to be normalized</param>
        /// <param name="isUserInput">True if the string being normalized is part of the user input path -
        /// flags that we need to normalize out * and _ chars</param>
        /// <returns>The normalized string</returns>
        public string Normalize(string input, bool isUserInput)
        {
            StringBuilder result = new StringBuilder();

            // objects for normalization of the input
            Normalize.ApplySubstitutions     substitutor = new Normalize.ApplySubstitutions(bot);
            Normalize.StripIllegalCharacters stripper    = new Normalize.StripIllegalCharacters(bot);

            string substitutedInput = substitutor.Transform(input);

            // split the pattern into it's component words
            string[] substitutedWords = substitutedInput.Split(" \r\n\t".ToCharArray());

            // Normalize all words unless they're the AIML wildcards "*" and "_" during AIML loading
            foreach (string word in substitutedWords)
            {
                string normalizedWord;
                if (isUserInput)
                {
                    normalizedWord = stripper.Transform(word);
                }
                else
                {
                    if ((word == "*") || (word == "_"))
                    {
                        normalizedWord = word;
                    }
                    else
                    {
                        normalizedWord = stripper.Transform(word);
                    }
                }
                result.Append(normalizedWord.Trim() + " ");
            }

            return(result.ToString().Replace("  ", " ")); // make sure the whitespace is neat
        }