Ejemplo n.º 1
0
 public void AddToMap(string key, BaseIntentions value)
 {
     try
     {
         _map.Add(key, value);
     }
     catch (Exception)
     {
         throw;
     }
 }
Ejemplo n.º 2
0
 public void AddToMap(Type key, BaseIntentions value)
 {
     if (typeof(ILex).IsAssignableFrom(key))
     {
         try
         {
             _map.Add(key, value);
         }
         catch (Exception)
         {
             throw;
         }
     }
     else
     {
         throw new ArgumentException("ERROR: Key is not of a type which implements ILex");
     }
 }
 public IBaseIntention Build(BaseIntentions baseIntentionEnum)
 {
     return((IBaseIntention)Activator.CreateInstance(_mapperRepo.intentToIntentBuilderMap[baseIntentionEnum], _commandAggregatesRepo, _mapperRepo, _gameLexesRepo));
 }
Ejemplo n.º 4
0
        public bool BootstrapCommandDetermination(string stringInput)
        {
            var nlpResultModel = _runtime.Container.Resolve <NlpResult>();

            nlpResultModel = _coreNlpProcessor.DeserializeInput(_pipeline, nlpResultModel, stringInput);

            var jsonContentProvider = _runtime.Container.Resolve <IContentProvider>();
            var currentAssemblyName = GetType().GetTypeInfo().Assembly.GetName().Name;
            var errorDisplayables   = _runtime.Container.Resolve <ErrorDisplayables>();

            jsonContentProvider.Populate <ErrorDisplayables>(errorDisplayables, currentAssemblyName);

            var basicValidatorHndlr = _runtime.Container.Resolve <BasicValidatorHandler>();

            var rootValidator = basicValidatorHndlr.FetchModel <NlpResult>(nlpResultModel);

            rootValidator.RuleFor(result => result.sentences).SetValidator(new ListMustContainOnlyOneItem <Sentence>(errorDisplayables));
            rootValidator.RuleFor(result => result.sentences).SetValidator(new ListMustNotBeEmpty <Sentence>(errorDisplayables));

            var rootValidatorResult = basicValidatorHndlr.ValidateModel(rootValidator);

            if (!rootValidatorResult.IsValid)
            {
                foreach (var failure in rootValidatorResult.Errors)
                {
                    Console.WriteLine(failure.ErrorMessage);
                }
                return(false);
            }

            var nerTypes = new List <string>
            {
                "PERSON",
                "LOCATION",
                "ORGANIZATION",
                "MISC",
                "MONEY",
                "NUMBER",
                "ORDINAL",
                "PERCENT"
            };

            stringInput = "";

            foreach (var token in nlpResultModel.sentences[0].tokens)
            {
                if (nerTypes.Contains(token.ner))
                {
                    stringInput += token.word;
                    stringInput += token.after;
                }
                else
                {
                    stringInput += token.word.ToLower();
                    stringInput += token.after.ToLower();
                }
            }

            nlpResultModel = _runtime.Container.Resolve <NlpResult>();
            nlpResultModel = _coreNlpProcessor.DeserializeInput(_pipeline, nlpResultModel, stringInput);



            var sentenceModelToValidate = basicValidatorHndlr.FetchModel <Sentence>(nlpResultModel.sentences[0]);

            sentenceModelToValidate.RuleFor(result => result.tokens).SetValidator(new CheckAllWordsIsInGameLex(_lexRepo));

            basicValidatorHndlr.ValidateModel(sentenceModelToValidate);


            var checkAllWordsIsInGameLex = basicValidatorHndlr.GetAValidator <CheckAllWordsIsInGameLex>(sentenceModelToValidate, "tokens");



            var truncatedTokensToValidate = basicValidatorHndlr.FetchModel <IList <Token> >(checkAllWordsIsInGameLex.GetTokensNotFoundInGameLexes());

            truncatedTokensToValidate.RuleFor(result => result).SetValidator(new CheckAllWordsIsInNlpLex(_lexicalizedParser));

            var truncatedTokensToValidateResult = basicValidatorHndlr.ValidateModel(truncatedTokensToValidate);

            var checkAllWordsIsInNlpLex = basicValidatorHndlr.GetAValidatorOfEnumerable <CheckAllWordsIsInNlpLex, Token>(truncatedTokensToValidate, 0);


            if (!truncatedTokensToValidateResult.IsValid)
            {
                foreach (var tokenNotFound in checkAllWordsIsInNlpLex.GetTokensNotFoundInNlpLex())
                {
                    Console.WriteLine("I do not understand the word ", tokenNotFound.word);
                }
                return(false);
            }


            var commandAggregatesRepo = _runtime.Container.Resolve <CommandAggregatesRepo>();

            var allTokensIterator  = _runtime.Container.Resolve <IListIterator <Token> >();
            var allTokensAggregate = _runtime.Container.Resolve <IListAggregate <Token> >(new TypedParameter(typeof(IListIterator <>), allTokensIterator));

            foreach (var token in nlpResultModel.sentences[0].tokens)
            {
                allTokensAggregate.Add(token);
            }

            var allBasicDependenciesIterator  = _runtime.Container.Resolve <IListIterator <BasicDependency> >();
            var allBasicDependenciesAggregate = _runtime.Container.Resolve <IListAggregate <BasicDependency> >(new TypedParameter(typeof(IListIterator <>), allBasicDependenciesIterator));

            foreach (var basicDependency in nlpResultModel.sentences[0].basicDependencies)
            {
                allBasicDependenciesAggregate.Add(basicDependency);
            }

            var allEntityMentionsIterator  = _runtime.Container.Resolve <IListIterator <Entitymention> >();
            var allEntityMentionsAggregate = _runtime.Container.Resolve <IListAggregate <Entitymention> >(new TypedParameter(typeof(IListIterator <>), allEntityMentionsIterator));

            foreach (var entityMention in nlpResultModel.sentences[0].entitymentions)
            {
                allEntityMentionsAggregate.Add(entityMention);
            }

            var wordsFoundInGameLexesIterator  = _runtime.Container.Resolve <IDictionaryIterator <string, dynamic> >();
            var wordsFoundInGameLexesAggregate = _runtime.Container.Resolve <IDictionaryAggregate <string, dynamic> >(new TypedParameter(typeof(IDictionaryIterator <,>), wordsFoundInGameLexesIterator));


            foreach (var wordsFoundInGameLex in checkAllWordsIsInGameLex.GetWordsFoundInGameLexes())
            {
                wordsFoundInGameLexesAggregate.AddOrReplace(wordsFoundInGameLex.Key, wordsFoundInGameLex.Value);
            }


            var tokensNotFoundInGameLexesIterator  = _runtime.Container.Resolve <IListIterator <Token> >();
            var tokensNotFoundInGameLexesAggregate = _runtime.Container.Resolve <IListAggregate <Token> >(new TypedParameter(typeof(IListIterator <>), tokensNotFoundInGameLexesIterator));

            foreach (var tokenNotFoundInGameLexes in checkAllWordsIsInGameLex.GetTokensNotFoundInGameLexes())
            {
                tokensNotFoundInGameLexesAggregate.Add(tokenNotFoundInGameLexes);
            }

            var tokensFoundInNlpLexIterator  = _runtime.Container.Resolve <IListIterator <Token> >();
            var tokensFoundInNlpLexAggregate = _runtime.Container.Resolve <IListAggregate <Token> >(new TypedParameter(typeof(IListIterator <>), tokensFoundInNlpLexIterator));

            foreach (var tokenFoundInNlpLex in checkAllWordsIsInNlpLex.GetTokensFoundInNlpLex())
            {
                tokensFoundInNlpLexAggregate.Add(tokenFoundInNlpLex);
            }

            var tokensNotFoundInNlpLexIterator  = _runtime.Container.Resolve <IListIterator <Token> >();
            var tokensNotFoundInNlpLexAggregate = _runtime.Container.Resolve <IListAggregate <Token> >(new TypedParameter(typeof(IListIterator <>), tokensNotFoundInNlpLexIterator));

            foreach (var tokenNotFoundInNlpLex in checkAllWordsIsInNlpLex.GetTokensNotFoundInNlpLex())
            {
                tokensNotFoundInNlpLexAggregate.Add(tokenNotFoundInNlpLex);
            }

            commandAggregatesRepo.Set(
                allTokensAggregate,
                allBasicDependenciesAggregate,
                allEntityMentionsAggregate,
                wordsFoundInGameLexesAggregate,
                tokensNotFoundInGameLexesAggregate,
                tokensFoundInNlpLexAggregate,
                tokensNotFoundInNlpLexAggregate
                );

            // BUG - for an unknown reason, cannot use Autofac to resolve IntentionBuilderFactory, as it does not adhere to InstancePerDependency registration. Using New instead as a workaround.
            _intentionBuilderFactory = new IntentionBuilderFactory(commandAggregatesRepo, _mapperRepo, _lexRepo);


            _baseIntentionResult = _intentionBuilderFactory.DetermineBaseIntention();


            // **TEMP**
            switch (_baseIntentionResult)
            {
            case BaseIntentions.Adverb:
                Console.WriteLine("I'm not sure what you're trying to do?");
                break;

            case BaseIntentions.AdverbMod:
                Console.WriteLine("What?");
                break;

            case BaseIntentions.Adjective:
                Console.WriteLine("I'm not sure what you're trying to describe?");
                break;

            case BaseIntentions.Inventory:
                Console.WriteLine("Sorry, I don't have any inventory functionality programmed in me");
                break;

            case BaseIntentions.Take:
                Console.WriteLine("Sorry, I don't yet know how to take things");
                break;

            case BaseIntentions.ObjectOfInterest:
                Console.WriteLine("What about it?");
                break;

            case BaseIntentions.MiscObject:
                Console.WriteLine("What about it?");
                break;

            case BaseIntentions.Direction:
                Console.WriteLine("What about that direction?");
                break;

            case BaseIntentions.Location:
                Console.WriteLine("What about that location?");
                break;

            case BaseIntentions.NerPerson:
                Console.WriteLine("I know that person!");
                break;

            case BaseIntentions.NerLocation:
                Console.WriteLine("I know that place! I think it's very far away from here");
                break;

            case BaseIntentions.NerOrganization:
                Console.WriteLine("What about them?");
                break;

            case BaseIntentions.NerMisc:
                Console.WriteLine("What about it?");
                break;

            case BaseIntentions.NerMoney:
                Console.WriteLine("Money won't help me now");
                break;

            case BaseIntentions.NerNumber:
                Console.WriteLine("What about the number?");
                break;

            case BaseIntentions.NerOrdinal:
                Console.WriteLine("Ordinal for what?");
                break;

            case BaseIntentions.NerPercent:
                Console.WriteLine("Percentage of what?");
                break;

            case BaseIntentions.NerUnknown:
                Console.WriteLine("I don't recognise that");
                break;

            case BaseIntentions.UnknownVerb:
                Console.WriteLine("What are you trying to do");
                break;

            case BaseIntentions.UnknownNoun:
                Console.WriteLine("What about it?");
                break;

            case BaseIntentions.UnknownNounPlural:
                Console.WriteLine("What about them?");
                break;

            case BaseIntentions.UnknownAdjective:
                Console.WriteLine("What are you trying to describe?");
                break;

            case BaseIntentions.UnknownAdverb:
                Console.WriteLine("I'm not sure what you're trying to do");
                break;

            case BaseIntentions.EntirelyUnknown:
                Console.WriteLine("I don't understand");
                break;
            }


            _resultModelHandler = _runtime.Container.Resolve <CommandResultModelHandler>();

            return(true);
        }