private static void runActions(ContextPool pool, ActionBlock actionBlock, NodesSubstitution substitutions = null, bool useFiltering = true) { if (substitutions != null) { pool.SetSubstitutions(substitutions); } var sortedActions = actionBlock.Actions.OrderByDescending((a) => a.Priority).ToArray(); var hasPushAction = sortedActions.Any(action => action is PushAction); if (hasPushAction) { //start new topic - but only once! (multiple pushes can appear) pool.ClearAccumulator(); } foreach (var action in sortedActions) { action.Run(pool); } if (useFiltering && pool.ActiveCount > 1) { pool.Filter(actionBlock.OutputFilter); } }
internal QuestionAnsweringModuleBase(ComposedGraph graph, CallStorage storage) { Storage = storage; Pool = new ContextPool(graph); _adviceAnswer = storage.RegisterCall("AdviceAnswer", c => { _AdviceAnswer(c.String("question"), c.Bool("isBasedOnContext"), c.Node("correctAnswerNode", Graph), c.Nodes("context", Graph)); }); _repairAnswer = storage.RegisterCall("RepairAnswer", c => { _RepairAnswer(c.String("question"), c.Node("suggestedAnswer", Graph), c.Nodes("context", Graph)); }); _setEquivalencies = storage.RegisterCall("SetEquivalence", c => { SetEquivalence(c.String("patternQuestion"), c.String("queriedQuestion"), c.Bool("isEquivalent")); }); _negate = storage.RegisterCall("Negate", c => { Negate(c.String("question")); }); }
internal ContextPool Clone() { var clone = new ContextPool(Graph); clone._accumulator.UnionWith(_accumulator); clone._substitutions = new Dictionary <NodeReference, NodeReference>(_substitutions); return(clone); }
private void setFilter(NodeReference correctAnswerNode, ActionBlock actionBlock, ContextPool pool) { foreach (var node in pool.ActiveNodes) { var isInOutput = node.Equals(correctAnswerNode); actionBlock.OutputFilter.Advice(node, isInOutput, false); } actionBlock.OutputFilter.Retrain(); ConsoleServices.Print(actionBlock.OutputFilter.Root); }
private bool updatePushPart(QuestionEntry questionEntry, PoolHypothesis hypothesis, ContextPool pool) { pool = pool.Clone(); if (!questionEntry.IsContextFree) { //TODO: we are now not able to learn this return(false); } pool.ClearAccumulator(); //we don't need substitute anything - we ran rules with original nodes only pool.SetSubstitutions(null); var pushActions = new List <PushAction>(); var insertActions = new List <InsertAction>(); //traverse all entries and collect all update/insert rules, that will //cover every correct answer var equivalentEntries = getPatternEquivalentEntries(questionEntry); var correctAnswers = new HashSet <NodeReference>(); foreach (var entry in equivalentEntries) { if (!entry.HasAnswer) { continue; } if (!pool.ContainsInAccumulator(entry.CorrectAnswer)) { var action = createPushAction(entry.Question, entry.CorrectAnswer); if (action == null) { //we cannot derive push rule insertActions.Add(new InsertAction(entry.CorrectAnswer)); pool.Insert(entry.CorrectAnswer); } else { //we have got push rule - we will apply it without constraining and filtering action.Run(pool); if (entry.QuestionNodes.Count != hypothesis.Substitutions.OriginalNodes.Count) { //TODO we are not able to update this for now return(false); } action = action.Resubstitution(entry.QuestionNodes, hypothesis.Substitutions.OriginalNodes); pushActions.Add(action); } correctAnswers.Add(entry.CorrectAnswer); } } hypothesis.ActionBlock.UpdatePush(pushActions); hypothesis.ActionBlock.UpdateInsert(insertActions); foreach (var node in pool.ActiveNodes) { //TODO detection of inclusion collision in filter var isCorrect = correctAnswers.Contains(node); hypothesis.ActionBlock.OutputFilter.Advice(node, isCorrect); } return(true); }