示例#1
0
        // Apply the text knowlegebase to the selected content, using only the first matching rule
        List <Literal> TextLiterals(List <Literal> content)
        {
            var result = new List <Literal>();

            foreach (Literal contentLiteral in content)
            {
                var textEntailments = Forward.Entailments(Tkb, new List <Literal>()
                {
                    contentLiteral
                });
                foreach (Entailment textEntailment in textEntailments)
                {
                    if (textEntailment.Entailed.Predicate == "text") // what about textc?
                    {
                        result.Add(textEntailment.Entailed);
                        break; // found one, so break out of the inner foreach loop
                    }
                }
            }
            return(result);
        }
示例#2
0
 // Get the structure of the given solution
 List <Entailment> Entailments(List <Literal> solution)
 {
     return(Forward.Entailments(Kb, solution));
 }
示例#3
0
        public static void Main(string[] cli_args)
        {
            var args = new Cli(cli_args);

            if (args.Help_flag)
            {
                Console.WriteLine(HELP_MESSAGE);
                System.Environment.Exit(1);
            }


            // read buffers
            var buffer_i = "";
            var buffer_k = "";

            if (args.Input_path != null)
            {
                buffer_i = File.ReadAllText(args.Input_path);
            }
            else
            {
                if (Console.IsInputRedirected)
                {
                    using (StreamReader reader = new StreamReader(Console.OpenStandardInput(), Console.InputEncoding))
                    {
                        buffer_i = reader.ReadToEnd();
                    }
                }
            }

            if (args.Knowledge_path != null)
            {
                buffer_k = File.ReadAllText(args.Knowledge_path);
            }

            // create knowledgebase
            var kb  = new Knowledgebase();
            var obs = kb.Add(buffer_i);

            obs.AddRange(kb.Add(buffer_k));

            // compose result
            var result = "";

            if (args.Parse_flag == true) // parse only
            {
                result = String.Join("\n", kb.Axioms.Select(dc => dc.Repr()));
            }

            else if (args.Forward_flag == true) // Forward chain
            {
                var entailments = Forward.Entailments(kb, obs);
                if (args.Graph_flag == true) // dot format graph
                {
                    result = Forward.Graph(obs, entailments, new List <Literal>());
                }
                else
                {
                    result = String.Join("\n", entailments.Select(e => e.Repr()));
                }
            }

            else // Abduction
            {
                var all_solutions = new List <List <Literal> >(); // void

                if (args.All_flag) // All flag
                {
                    all_solutions = Etcetera.DoEtcAbduction(obs, kb, args.Depth, true);
                }

                // else if incremental here

                else if (args.Incremental_flag)
                {
                    all_solutions = Incremental.DoIncremental(obs, kb, args.Depth, args.Nbest, args.Window, args.Beam, true);
                    //all_solutions = Realtime_Sidecar.incremental_alternative(obs, kb, args.Depth, args.Nbest, args.Window, args.Beam, true);
                }

                // else n-best
                else
                {
                    all_solutions = Etcetera.NBest(obs, kb, args.Depth, args.Nbest, true);
                }

                // Then decide what to output
                if (args.Graph_flag)
                {
                    var entailments = Forward.Entailments(kb, all_solutions[args.Solution - 1]);
                    result = Forward.Graph(all_solutions[args.Solution - 1], entailments, obs);
                }
                else if (all_solutions.Count == 0)
                {
                    result = "0 solutions.";
                }
                else
                {
                    var reslist = new List <String>();
                    if (args.EntailmentsFlag)
                    {
                        foreach (List <Literal> solution in all_solutions)
                        {
                            var entailed = Incremental.GetContext(solution, obs, kb);
                            if (entailed.Count == 0)
                            {
                                reslist.Add("none.");
                            }
                            else
                            {
                                reslist.Add($"({String.Join(" ",entailed.Select(e => e.Repr()))})");
                            }
                        }
                    }
                    else
                    {
                        foreach (List <Literal> solution in all_solutions)
                        {
                            var reprs = new List <String>();
                            foreach (Literal l in solution)
                            {
                                reprs.Add(l.Repr());
                            }
                            var reprstr = $"({String.Join(" ", reprs)})";
                            reslist.Add(reprstr);
                        }
                    }
                    result = $"{String.Join("\n",reslist)}\n{reslist.Count} solutions.";
                }
            }

            // output
            if (args.Output_path != null)
            {
                using (StreamWriter file = new StreamWriter(args.Output_path))
                {
                    file.WriteLine(result);
                }
            }
            else
            {
                Console.WriteLine(result);
            }
        }