Example #1
0
        public static void Main(string[] cli_args)
        {
            var args = new TextGeneratorCli(cli_args);

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


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

            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));

            // create templates knowledgebase
            buffer_t = File.ReadAllText(args.Templates_path);
            var           tkb  = new Knowledgebase();
            var           tobs = tkb.Add(buffer_t);
            TextGenerator tg   = new TextGenerator(kb, tkb, tobs);

            // read the solution
            buffer_s = File.ReadAllText(args.Solution_path);
            var skb      = new Knowledgebase();
            var solution = skb.Add(buffer_s); // solution literals only


            var result = "";

            result = $"{String.Join(" || ",tg.Generate(solution, obs, ranker: args.Ranker ))}";

            // output
            if (args.Output_path != null)
            {
                using (StreamWriter file = new StreamWriter(args.Output_path))
                {
                    file.WriteLine(result);
                }
            }
            else
            {
                Console.WriteLine(result);
            }
        }
Example #2
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);
            }
        }