コード例 #1
0
        private bool updateOldActions(string question, bool isBasedOnContext, NodeReference correctAnswerNode)
        {
            var parsedQuestion = UtteranceParser.Parse(question);
            var bestHypothesis = GetBestHypothesis(parsedQuestion);

            if (bestHypothesis == null)
            {
                return(false);
            }

            if (bestHypothesis.Control.Score < 0.9)
            {
                //this is different hypothesis
                return(false);
            }

            var questionEntry = GetQuestionEntry(parsedQuestion);

            //update push part of rule
            if (!updatePushPart(questionEntry, bestHypothesis, Pool))
            {
                return(false);
            }

            //context filter is updated with push part
            return(true);
        }
コード例 #2
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));
        }
コード例 #3
0
        private string tagWithContext(string edgeHint, string context, string tag, bool canResolveCoreference)
        {
            //first try the exact match
            if (replaceFirstOccurrence(ref edgeHint, context, tag))
            {
                return(edgeHint);
            }

            //next try word groups matching
            var contextWords = getWords(context);

            for (var n = contextWords.Length - 1; n > 0; --n)
            {
                foreach (var ngram in getNgrams(context, n))
                {
                    if (!UtteranceParser.IsInformativeWord(ngram))
                    {
                        continue;
                    }

                    var edgeHintCopy = edgeHint;
                    if (replaceFirstOccurrence(ref edgeHintCopy, ngram, tag) &&
                        !edgeHintCopy.Contains(ngram)
                        )
                    {
                        return(edgeHintCopy);
                    }
                }
            }

            return(edgeHint);
        }
コード例 #4
0
        internal void AdviceAnswer(string question, NodeReference answer)
        {
            var parsedQuestion = UtteranceParser.Parse(question);

            foreach (var featureCover in FeatureCover.GetFeatureCovers(parsedQuestion, Graph))
            {
                register(featureCover, answer);
            }
        }
コード例 #5
0
        private bool isInform(AnnotatedQuestionActionEntry action, SLUFactory sluFactory)
        {
            var slu = sluFactory.GetBestDialogAct(UtteranceParser.Parse(action.Text));

            if (slu is DontKnowAct || slu is NegateAct || slu is ChitChatAct || slu is AffirmAct)
            {
                return(false);
            }

            return(true);
        }
コード例 #6
0
        static void MainParsing(string[] args)
        {
            var utterance       = "yes, Obama is president of which state?";
            var parsedUtterance = UtteranceParser.Parse(utterance);

            var factory = new SLUFactory();
            var bestAct = factory.GetBestDialogAct(parsedUtterance);
            var acts    = factory.GetDialogActs(parsedUtterance);

            foreach (var act in acts)
            {
                Console.WriteLine(act);
            }
        }
コード例 #7
0
        internal void AddQuestion(string question)
        {
            lock (_L_global)
            {
                if (_questionIndex.ContainsKey(question))
                {
                    return;
                }

                _questionIndex[question] = new QuestionInfo(UtteranceParser.Parse(question));

                commitChanges();
            }
        }
コード例 #8
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);
        }
コード例 #9
0
        private void disableEquivalence(string utterance1, string utterance2)
        {
            var signature1 = getSignature(UtteranceParser.Parse(utterance1));
            var signature2 = getSignature(UtteranceParser.Parse(utterance2));

            HashSet <string> disabled;

            if (!_disabledEquivalencies.TryGetValue(signature1, out disabled))
            {
                _disabledEquivalencies[signature1] = disabled = new HashSet <string>();
            }

            disabled.Add(signature2);
        }
コード例 #10
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);
        }
コード例 #11
0
        public Loader(string path)
        {
            _reader   = new StreamReader(path, Encoding.UTF8);
            DataLayer = new ExplicitLayer();

            Console.WriteLine("Counting lines: " + path);
            var lineCount   = 0;
            var edges       = new HashSet <string>();
            var myInterning = new Dictionary <string, string>();

            while (!_reader.EndOfStream)
            {
                var line = _reader.ReadLine();
                ++lineCount;
            }

            _reader.BaseStream.Seek(0, SeekOrigin.Begin);

            Console.WriteLine("Loading graph(" + lineCount + "): " + path);
            var lastPercent = 0;
            var lineIndex   = 0;

            while (!_reader.EndOfStream)
            {
                var line = _reader.ReadLine();

                var parts  = split(line);
                var source = intern(parts[0]);
                var edge   = intern(parts[1]);
                var target = intern(parts[2]);

                var sourceNode = GraphLayerBase.CreateReference(source);
                var targetNode = GraphLayerBase.CreateReference(target);

                _nodes.Add(sourceNode);
                _nodes.Add(targetNode);
                DataLayer.AddEdge(sourceNode, edge, targetNode);
                UtteranceParser.RegisterEntity(target);

                ++lineIndex;
                var currentPercent = 100 * lineIndex / lineCount;
                if (currentPercent != lastPercent)
                {
                    GC.Collect();
                    Console.WriteLine(currentPercent);
                    lastPercent = currentPercent;
                }
            }
        }
コード例 #12
0
        private Dictionary <string, object> getUserSlu(AnnotatedQuestionActionEntry userAction, SLUFactory sluFactory)
        {
            var slu = sluFactory.GetBestDialogAct(UtteranceParser.Parse(userAction.Text));

            if (slu is DontKnowAct || slu is NegateAct || slu is ChitChatAct || slu is AffirmAct)
            {
                return(slu.GetDialogAct().ToDictionary());
            }

            return(new Dictionary <string, object>()
            {
                { "act", "Inform" },
                { "text", userAction.Text }
            });
        }
コード例 #13
0
        /// <inheritdoc/>
        public QuestionAnswerReceiveResult ReceiveAnswerPart(IEnumerable <TurnLog> answerTurns)
        {
            var lastTurn        = answerTurns.Last();
            var parsedUtterance = UtteranceParser.Parse(lastTurn.Text);

            foreach (var word in parsedUtterance.Words)
            {
                if (_pool.Graph.HasEvidence(word))
                {
                    var answer = new[] { _pool.Graph.GetNode(word) };
                    return(QuestionAnswerReceiveResult.From(new Ranked <IEnumerable <NodeReference> >(answer, 1.0)));
                }
            }
            throw new NotImplementedException("Parse answer");
        }
コード例 #14
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);
        }
コード例 #15
0
        protected override bool adviceAnswer(string question, bool isBasedOnContext, NodeReference correctAnswerNode, IEnumerable <NodeReference> context)
        {
            if (question == null || question.Trim() == "")
            {
                return(false);
            }

            fillPool(context);
            var questionEntry = GetQuestionEntry(UtteranceParser.Parse(question));

            questionEntry.RegisterAnswer(isBasedOnContext, correctAnswerNode);

            return
                (updateOldActions(question, isBasedOnContext, correctAnswerNode) ||
                 createNewActions(question, isBasedOnContext, correctAnswerNode));
        }
コード例 #16
0
        internal override void Register(string utterance, ResponseBase response)
        {
            var parsedUtterance = UtteranceParser.Parse(utterance);

            foreach (var word in parsedUtterance.Words)
            {
                if (_requiredEntityAliases.Contains(word))
                {
                    _containsEntity = true;
                }
            }

            if (response is ByeAct)
            {
                _isDialogEnded = true;
            }
        }
コード例 #17
0
        static void Main(string[] args)
        {
            System.Console.SetBufferSize(240, 10000); // make sure buffer is bigger than window
            System.Console.SetWindowSize(240, 54);    //set window size to almost full screen

            Process p = Process.GetCurrentProcess();

            ShowWindow(p.MainWindowHandle, 3); //SW_MAXIMIZE = 3


            var parse = UtteranceParser.Parse("name of wife of Barack Obama president is Michelle Obama");

            //MultipleAdvice(args[0]);
            //ExplicitStateDialog(args[0]);
            //ProbabilisticMappingQATest(args[0]);
            RuleQuestionTest(args[0]);
        }
コード例 #18
0
        private string repairSpelling(string utterance)
        {
            var sentence = UtteranceParser.Parse(utterance);
            var builder  = new StringBuilder();

            foreach (var word in sentence.Words)
            {
                if (builder.Length > 0)
                {
                    builder.Append(' ');
                }

                builder.Append(word);
            }

            return(builder.ToString());
        }
コード例 #19
0
        internal IEnumerable <MappingControl <Trigger> > UtteranceEdge(string utterance)
        {
            var parsedUtterance = UtteranceParser.Parse(utterance);

            foreach (var triggerPair in _externalTriggers)
            {
                var bestHypothesis = triggerPair.Key.BestMap(parsedUtterance);
                if (bestHypothesis != null && bestHypothesis.Score > 0.5)
                {
                    return(new[] {
                        bestHypothesis.ChangeValue(triggerPair.Value)
                    });
                }
            }

            return(Triggers.FindMapping(parsedUtterance));
        }
コード例 #20
0
        protected override void repairAnswer(string question, NodeReference suggestedAnswer, IEnumerable <NodeReference> context)
        {
            fillPool(context);
            var hypotheses = GetSortedHypotheses(UtteranceParser.Parse(question)).ToArray();

            foreach (var hypothesis in hypotheses)
            {
                var actualAnswer = getActualAnswer(hypothesis);
                var isCorrect    = actualAnswer.Contains(suggestedAnswer);

                hypothesis.Control.Suggest(isCorrect);
            }

            if (suggestedAnswer != null)
            {
                var isBasedOnContext = hypotheses.Length > 0 && hypotheses[0].ActionBlock.Actions.Any(a => a is ExtendAction);
                AdviceAnswer(question, isBasedOnContext, suggestedAnswer);
            }
        }
コード例 #21
0
        protected override void setEquivalence(string patternQuestion, string queriedQuestion, bool isEquivalent)
        {
            var parsedQuestion = UtteranceParser.Parse(patternQuestion);

            if (isEquivalent)
            {
                var bestMap = Triggers.BestMap(parsedQuestion);
                if (bestMap == null)
                {
                    return;
                }

                Triggers.SetMapping(queriedQuestion, bestMap.Value);
            }
            else
            {
                Triggers.DisableEquivalence(patternQuestion, queriedQuestion);
            }
        }
コード例 #22
0
        protected override void negate(string question)
        {
            var parsedSentence = UtteranceParser.Parse(question);
            var bestHypothesis = GetBestHypothesis(parsedSentence);

            if (bestHypothesis == null)
            {
                //we cannot learn anything
                return;
            }

            var currentAnswer = getActualAnswer(bestHypothesis);

            foreach (var answer in currentAnswer)
            {
                bestHypothesis.ActionBlock.OutputFilter.Advice(answer, false, false);
            }

            bestHypothesis.ActionBlock.OutputFilter.Retrain();
        }
コード例 #23
0
        private IEnumerable <SemanticPart> lastRelevantUtterances(string question, NodeReference answer)
        {
            var result = new List <SemanticPart>();

            foreach (var word in UtteranceParser.Parse(question).Words)
            {
                var fromNode = Graph.GetNode(word);
                var paths    = Graph.GetPaths(fromNode, answer, MaximumGraphDepth, MaximumGraphWidth).Take(1).ToArray();
                if (paths.Length == 0)
                {
                    //there is no evidence
                    continue;
                }

                var part = new SemanticPart(question, paths);
                result.Add(part);
            }

            return(result);
        }
コード例 #24
0
        protected override ModifiableResponse execute()
        {
            var question = Context.Get(RequestAnswer.QuestionProperty);

            if (question == null)
            {
                throw new NotSupportedException("Cannot provide answer without question");
            }

            Context.SetValue(LastQuestion, question);

            var parsedQuestion = UtteranceParser.Parse(question);
            var answer         = Context.QuestionAnsweringModule.GetAnswer(parsedQuestion).ToArray();

            ModifiableResponse response;

            if (answer.Length <= Context.MaximumUserReport)
            {
                Context.Pool.ClearAccumulator();
                Context.Pool.Insert(answer);
                if (Context.Pool.HasActive)
                {
                    response = Response("It is", Context.Pool.ActiveNodes);
                }
                else
                {
                    response = Response("I don't know.");
                }
            }
            else
            {
                //TODO find criterion
                response = Response("There is too much information for output. Please be more specific");
            }


            Context.Remove(RequestAnswer.QuestionProperty);

            EmitEdge(QuestionAnswered);
            return(response);
        }
コード例 #25
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);
        }
コード例 #26
0
 private NodeReference N(string name)
 {
     UtteranceParser.RegisterEntity(name);
     return(GraphLayerBase.CreateReference(name));
 }
コード例 #27
0
 internal QuestionEntry(string question, ComposedGraph graph)
 {
     Question       = question;
     ParsedQuestion = UtteranceParser.Parse(question);
     QuestionNodes  = findeQuestionNodes(graph);
 }
コード例 #28
0
        internal ActionEntry(int actionIndex, Dictionary <string, object> data)
        {
            ActionIndex = actionIndex;
            Data        = data;

            if (data.ContainsKey(CallStorage.TimeEntry))
            {
                Time = (DateTime)data[CallStorage.TimeEntry];
            }

            if (data.ContainsKey("user_id"))
            {
                UserId = data["user_id"] as string;
            }

            if (data.ContainsKey("task_id"))
            {
                TaskId = int.Parse(data["task_id"] as string);
            }

            Type = "T_" + resolveType(data);

            object textData;

            if (
                data.TryGetValue("message", out textData) ||
                data.TryGetValue("utterance", out textData) ||
                data.TryGetValue("response", out textData)
                )
            {
                Text = textData as string;
            }
            else
            {
                Text = "";
            }

            object actData;

            if (data.TryGetValue("response_act", out actData))
            {
                if (actData != null)
                {
                    Act = actData.ToString();
                }
            }

            if (data.TryGetValue("response_act_json", out actData))
            {
                if (actData != null)
                {
                    MachineActJson = actData.ToString();
                }
            }

            if (data.ContainsKey("task"))
            {
                var substitutions = data["substitutions"] as Newtonsoft.Json.Linq.JArray;
                Text = string.Format(data["format"].ToString(), substitutions.ToArray());

                if (data.ContainsKey("success_code"))
                {
                    Text = $"Success: {data["success_code"]}, " + Text;
                }
            }

            switch (Type)
            {
            case "T_advice":
                Text  = "<b>Question</b>:" + data["question"] + "<br>";
                Text += "<b>Context</b>:" + data["context"] + "<br>";
                Text += "<b>Answer</b>:" + data["correctAnswerNode"] + "<br><br>";
                break;

            case "T_equivalence":
                Text  = "<b>PatternQuestion</b>:" + data["patternQuestion"] + "<br>";
                Text += "<b>QueriedQuestion</b>:" + data["queriedQuestion"] + "<br>";
                Text += "<b>IsEquivalent</b>:" + data["isEquivalent"] + "<br><br>";
                break;

            case "T_utterance":
                var utterance = UtteranceParser.Parse(data["utterance"] as string);
                Act = _factory.GetBestDialogAct(utterance).ToString();
                break;
            }
        }
コード例 #29
0
        public void SetMapping(string utterance, T data)
        {
            var sentence = UtteranceParser.Parse(utterance);

            Mapping[sentence] = data;
        }
コード例 #30
0
ファイル: WordStats.cs プロジェクト: m9ra/KnowledgeDialog
        private string[] getUtteranceWords(string utterance)
        {
            var parsed = UtteranceParser.Parse(utterance);

            return(parsed.Words.Distinct().ToArray());
        }