コード例 #1
0
        public Ranked <IEnumerable <NodeReference> > GetRankedAnswer(string question, ContextPool pool)
        {
            //keep original pool non-modified
            pool = pool.Clone();

            var parsedQuestion = UtteranceParser.Parse(question);
            var covers         = FeatureCover.GetFeatureCovers(parsedQuestion, Graph);

            var interpretations = _mapping.GetRankedInterpretations(covers);
            var bestMatch       = interpretations.BestMatch;

            if (bestMatch.Value == null)
            {
                //no interpretation has been found
                return(new Ranked <IEnumerable <NodeReference> >(new NodeReference[0], 0.0));
            }

            var bestInterpretation         = bestMatch.Value.Item2.Interpretation;
            var bestCover                  = bestMatch.Value.Item1;
            var instantiatedInterpretation = bestInterpretation.InstantiateBy(bestCover, Graph);

            //run the interpretation on the pool
            Console.WriteLine("\n");
            foreach (var rule in instantiatedInterpretation.Rules)
            {
                rule.Execute(pool);
                Console.WriteLine(rule);
                Console.WriteLine("POOL: " + string.Join(" ", pool.ActiveNodes));
            }

            return(new Ranked <IEnumerable <NodeReference> >(pool.ActiveNodes, bestMatch.Rank));
        }
コード例 #2
0
        internal Interpretation GeneralizeBy(FeatureCover cover, ComposedGraph graph)
        {
            var mapping = cover.CreateNodeMapping(graph);

            var isContractable = _rules.Any(r => r is InsertPoolRule);

            if (mapping.IsEmpty)
            {
                //there is nothing to generalize
                if (isContractable)
                {
                    //contractable interpretation without generalization is trivial
                    return(null);
                }
                else
                {
                    return(this);
                }
            }

            mapping.IsGeneralizeMapping = true;
            var newRules = mapRules(mapping);

            if (!mapping.WasUsed && isContractable)
            {
                //mapping wasnt used, so the contractable interpretation is trivial
                return(null);
            }

            return(new Interpretation(newRules));
        }
コード例 #3
0
        internal IEnumerable <Ranked <ContextRuleMapping> > GetRankedMappings(FeatureCover cover)
        {
            var distributions = getDistributions(cover);

            var currentLayer = new List <RuleChain>();
            var newLayer     = new List <RuleChain>();

            //compute chains of distributions in layers
            currentLayer.Add(new RuleChain());
            foreach (var distribution in distributions)
            {
                for (var ruleIndex = 0; ruleIndex < distribution.Size; ++ruleIndex)
                {
                    if (distribution == null)
                    {
                        throw new NotImplementedException("What to do, when distribution is not available?");
                    }

                    var rule        = distribution.GetRule(ruleIndex);
                    var probability = distribution.GetProbability(ruleIndex);

                    foreach (var ruleChain in currentLayer)
                    {
                        newLayer.Add(ruleChain.ExtendBy(rule, probability));
                    }
                }

                //efficiently swap layers
                var swappedLayer = currentLayer;
                currentLayer = newLayer;
                newLayer     = swappedLayer;
                newLayer.Clear();
            }
            throw new NotImplementedException("Select only valid rules");
        }
コード例 #4
0
        /// <summary>
        /// Negation evidence on question's answer.
        /// </summary>
        /// <param name="question">The adviced question.</param>
        /// <param name="negatedAnswer">The negated answer.</param>
        public void Negate(FeatureCover question, NodeReference negatedAnswer)
        {
            var evidence = getQuestionEvidence(question);

            evidence.Negate(negatedAnswer);

            checkInterpretationUpdates();
        }
コード例 #5
0
        /// <summary>
        /// Gives advice about answer on the question.
        /// </summary>
        /// <param name="question">The adviced question.</param>
        /// <param name="answer">The adviced answer.</param>
        public void Advice(FeatureCover question, NodeReference answer)
        {
            var evidence = getQuestionEvidence(question);

            evidence.Advice(answer);

            checkInterpretationUpdates();
        }
コード例 #6
0
        internal void AddInterpretation(FeatureCover cover, RuledInterpretation interpretation, double rank)
        {
            double score;
            var    key = Tuple.Create(cover, interpretation);

            _interpretationScores.TryGetValue(key, out score);
            score += rank;
            _interpretationScores[key] = score;
        }
コード例 #7
0
        internal void AdviceAnswer(string question, NodeReference answer)
        {
            var parsedQuestion = UtteranceParser.Parse(question);

            foreach (var featureCover in FeatureCover.GetFeatureCovers(parsedQuestion, Graph))
            {
                register(featureCover, answer);
            }
        }
コード例 #8
0
        /// <summary>
        /// Gets evidence for given question.
        /// </summary>
        /// <param name="question">The question.</param>
        /// <returns>The evidence.</returns>
        private QuestionEvidence getQuestionEvidence(FeatureCover question)
        {
            QuestionEvidence evidence;

            if (!_questions.TryGetValue(question.OriginalUtterance, out evidence))
            {
                _questions[question.OriginalUtterance] = evidence = new QuestionEvidence(question);
            }
            return(evidence);
        }
コード例 #9
0
        internal Ranked <RuledInterpretation> GetRankedInterpretation(FeatureCover cover)
        {
            InterpretationCounter counter;

            if (!_coverIndex.TryGetValue(cover.FeatureKey, out counter))
            {
                return(null);
            }

            return(new Ranked <RuledInterpretation>(counter.BestInterpretation, counter.BestCount));
        }
コード例 #10
0
        private RulesDistribution[] getDistributions(FeatureCover cover)
        {
            var distributions = new List <RulesDistribution>();

            foreach (var instance in cover.FeatureInstances)
            {
                RulesDistribution distribution;
                _featureToDistributions.TryGetValue(instance.Feature, out distribution);
                distributions.Add(distribution);
            }

            return(distributions.ToArray());
        }
コード例 #11
0
        private FeatureEvidence getEvidence(FeatureCover cover)
        {
            FeatureEvidence evidence;
            var             key = cover.FeatureKey;

            if (!_featureEvidences.TryGetValue(key, out evidence))
            {
                _featureEvidences[key] = evidence = new FeatureEvidence(cover, Graph);
                _featureIndex.Add(key);
            }

            return(evidence);
        }
コード例 #12
0
        private FeatureCover getMatchingCover(string question, FeatureKey key)
        {
            var parsedQuestion = UtteranceParser.Parse(question);

            foreach (var cover in FeatureCover.GetFeatureCovers(parsedQuestion, Graph))
            {
                if (key.Equals(cover.FeatureKey))
                {
                    return(cover);
                }
            }

            return(null);
        }
コード例 #13
0
        internal NodeReference GetAnswer(ParsedUtterance expression)
        {
            var covers = FeatureCover.GetFeatureCovers(expression, Graph);

            var allRankedMappings = new List <Ranked <ContextRuleMapping> >();

            foreach (var cover in covers)
            {
                var rankedMappings = _mapping.GetRankedMappings(cover);
                allRankedMappings.AddRange(rankedMappings);
            }

            throw new NotImplementedException();
        }
コード例 #14
0
        protected override bool adviceAnswer(string question, bool isBasedOnContext, NodeReference correctAnswerNode, IEnumerable <NodeReference> context)
        {
            var parsedQuestion = UtteranceParser.Parse(question);

            var interpretationsFactory = getInterpretationsFactory(parsedQuestion, isBasedOnContext, correctAnswerNode, context);

            var covers = FeatureCover.GetFeatureCovers(parsedQuestion, Graph);

            _mapping.Add(interpretationsFactory, covers);

            //TODO decide whether it would be benefitial to report that
            //the advice is taken into account, however we don't believe it much.
            return(true);
        }
コード例 #15
0
        internal Interpretation InstantiateBy(FeatureCover cover, ComposedGraph graph)
        {
            var mapping = cover.CreateNodeMapping(graph);

            if (mapping.IsEmpty)
            {
                return(this);
            }

            mapping.IsGeneralizeMapping = false;
            var newRules = mapRules(mapping);

            return(new Interpretation(newRules));
        }
コード例 #16
0
        internal IEnumerable <FeatureEvidence> GetEvidences(string question)
        {
            var result         = new List <FeatureEvidence>();
            var parsedQuestion = UtteranceParser.Parse(question);

            foreach (var featureCover in FeatureCover.GetFeatureCovers(parsedQuestion, Graph))
            {
                if (_featureEvidences.ContainsKey(featureCover.FeatureKey))
                {
                    result.Add(_featureEvidences[featureCover.FeatureKey]);
                }
            }

            return(result);
        }
コード例 #17
0
        private FeatureBase getBestFeature(RulePart rulePart, FeatureCover cover)
        {
            FeatureInstance bestFeatureInstance = null;
            var             bestPMI             = double.NegativeInfinity;

            foreach (var featureInstance in cover.FeatureInstances)
            {
                //select best feature according to pointwise mutual information
                var featurePMI = getPointwiseMutualInformation(rulePart, featureInstance.Feature);
                if (featurePMI > bestPMI)
                {
                    bestPMI             = featurePMI;
                    bestFeatureInstance = featureInstance;
                }
            }

            return(bestFeatureInstance.Feature);
        }
コード例 #18
0
        private Ranked <ContextRuleMapping> getRankedMapping(FeatureCover cover, Ranked <Interpretation> rankedInterpretation)
        {
            var ruleDecomposition = createInterpretationDecompositions(rankedInterpretation.Value);
            //assign rule parts to features
            var assignments = new Dictionary <RulePart, FeatureBase>();

            foreach (var rulePart in ruleDecomposition.Parts)
            {
                var bestFeature = getBestFeature(rulePart, cover);
                assignments[rulePart] = bestFeature;
            }

            var rankedMapping = createRankedContextRuleMapping(assignments);

            //rerank with interpretation ranking
            var rerankedMapping = new Ranked <ContextRuleMapping>(rankedMapping.Value, rankedMapping.Rank * rankedInterpretation.Rank);

            return(rerankedMapping);
        }
コード例 #19
0
        private void reportToCompatibleGenerators(FeatureCover reportingCover, Interpretation generalInterpretation)
        {
            if (_completedGeneralInterpretations.Contains(generalInterpretation))
            {
                //we are searchnig only for new general interpretations
                return;
            }

            //remember that we have proceed this interpretation so we don't need to repeatedly increment it
            _completedGeneralInterpretations.Add(generalInterpretation);

            //feature key has always been present in the index
            var generatorCovers = _keyToGeneratorCovers[reportingCover.FeatureKey];

            var compatibleCovers = new List <FeatureCover>();

            foreach (var generatorCover in generatorCovers)
            {
                var generator = generatorCover.Item1;
                var cover     = generatorCover.Item2;

                var instantiatedInterpretation = generalInterpretation.InstantiateBy(cover, Graph);
                if (isCompatibleInterpretation(instantiatedInterpretation, generator))
                {
                    compatibleCovers.Add(cover);
                    var ruledInterpretation = new RuledInterpretation(generalInterpretation, reportingCover.FeatureKey);
                    _mapping.ReportInterpretation(ruledInterpretation);
                }
            }

            if (compatibleCovers.Count > 1)
            {
                Console.WriteLine(generalInterpretation);
                Console.WriteLine("\tCOMPATIBLE WITH:");
                foreach (var cover in compatibleCovers)
                {
                    var origin = cover.FeatureInstances.First().Origin;
                    Console.WriteLine("\t\t" + origin);
                }

                Console.WriteLine();
            }
        }
コード例 #20
0
        protected override bool adviceAnswer(string question, bool isBasedOnContext, NodeReference correctAnswerNode, IEnumerable <NodeReference> context)
        {
            var parsedQuestion = UtteranceParser.Parse(question);
            var covers         = FeatureCover.GetFeatureCovers(parsedQuestion, Graph);

            //setup interpretation generator
            var factory = new InterpretationsFactory(parsedQuestion, isBasedOnContext, correctAnswerNode);

            var contextPool = new ContextPool(Graph);

            contextPool.Insert(context.ToArray());

            var generator = new InterpretationGenerator(covers, factory, contextPool, this);

            //!!!!!!!!!!!!!!TODOOOOOOOOOOOOO!!!!!!!!!!!!!!
            //We have to check compatibility of the generator with all known interpretations
            _interpretationGenerators.Add(generator);

            //register covers according to its feature keys
            foreach (var cover in generator.Covers)
            {
                List <Tuple <InterpretationGenerator, FeatureCover> > generators;
                if (!_keyToGeneratorCovers.TryGetValue(cover.FeatureKey, out generators))
                {
                    _keyToGeneratorCovers[cover.FeatureKey] = generators = new List <Tuple <InterpretationGenerator, FeatureCover> >();
                }

                generators.Add(Tuple.Create(generator, cover));
            }

            //TODO decide whether it would be benefitial to report that
            //the advice is taken into account, however we don't believe it much.

            //TODO initialize with first interpretation?
            return(true);
        }
コード例 #21
0
 internal QuestionEvidence(FeatureCover questionCover)
 {
     Cover    = questionCover;
     Question = questionCover.OriginalUtterance;
 }
コード例 #22
0
        private void register(FeatureCover cover, NodeReference answer)
        {
            var evidence = getEvidence(cover);

            evidence.Advice(cover, answer);
        }
コード例 #23
0
 internal FeatureEvidence(FeatureCover cover, ComposedGraph graph)
 {
     Graph                = graph;
     FeatureKey           = cover.FeatureKey;
     _generalFeatureNodes = cover.GetGeneralNodes(graph).ToArray();
 }
コード例 #24
0
        private IEnumerable <Interpretation> getGeneralInterpretations(Interpretation interpretation, FeatureCover reportingCover)
        {
            var generalInterpretation = interpretation.GeneralizeBy(reportingCover, Graph);

            if (generalInterpretation != null)
            {
                //report interpretation as is
                yield return(generalInterpretation);

                yield break;
            }

            foreach (var extendedInterpretation in interpretation.ExtendBy(reportingCover.GetInstanceNodes(Graph), Graph))
            {
                var generalExtendedInterpretation = extendedInterpretation.GeneralizeBy(reportingCover, Graph);

                if (generalExtendedInterpretation == null)
                {
                    throw new NotSupportedException("generalization should be successful");
                }
                yield return(generalExtendedInterpretation);
            }
        }