Ejemplo n.º 1
0
        public static List <Solution> DoIncremental(List <Literal> obs, Knowledgebase kb, int maxdepth, int n, int w, int b, bool sk)
        {
            var iteration    = 1; // count for skolem constants
            var window_start = 0;
            var window_end   = Math.Min(w * iteration, obs.Count);
            var previous     = new List <Solution>();

            // first, interpret the first window as normal
            var res = Etcetera.NBest(obs.GetRange(window_start, window_end - window_start), kb, maxdepth, b, false);
            var pre = "$" + iteration.ToString() + ":";

            foreach (Solution s in res)
            {
                previous.Add(skolemize_with_prefix(s, pre));
            }
            // then iterate through remaining windows
            while (window_end < obs.Count) // some remain
            {
                iteration   += 1;          //advance
                window_start = window_end;
                window_end   = Math.Min(w * iteration, obs.Count);
                res          = ContextualEtcAbduction(obs.GetRange(0, window_end), obs.GetRange(window_start, window_end - window_start), kb, previous, maxdepth, b, iteration);
                previous     = res;
            }
            if (previous.Count > n)                          // too many results
            {
                previous.RemoveRange(n, previous.Count - n); // truncate excess
            }
            return(previous);
        }
Ejemplo n.º 2
0
 public static List<Solution> DoAbduction(List<Literal> obs, Knowledgebase kb, int maxdepth, bool sk)
 {
     var list_of_lists = new List<List<Solution>>();
     foreach (Literal c in obs)
     {
         var remaining = new List<Literal>() { c };
         list_of_lists.Add(AndOrLeaflists(remaining, kb, maxdepth, new List<Literal>(), new List<Literal>()));
     }
     var combiner = new Combiner(list_of_lists);
     var res = new List<Solution>();
     foreach (Solution s in combiner)
     {
         foreach (Solution c in Crunch(s))
         {
             if (sk) 
             {
                 res.Add(skolemize(c));
             }
             else 
             {
                 res.Add(c);
             }
         }
     }
     return res.Distinct().ToList();
 }
Ejemplo n.º 3
0
        public static List <Solution> DoEtcAbduction(List <Literal> obs, Knowledgebase kb, int maxdepth, bool sk)
        {
            var solutions = Abduction.DoAbduction(obs, kb, maxdepth, sk);

            solutions.Sort((x, y) => JointLnProbability(y).CompareTo(JointLnProbability(x))); // reverse
            return(solutions);
        }
Ejemplo n.º 4
0
        // Pronoun classes define which pronouns an entity should use
        // static HashSet<string> PronounClasses = new HashSet<string>{
        //     "Masculine", // he, him, his, his, himself
        //     "Feminine", // she, her, her, hers, herself
        //     "Neuter", // it, it, its, its, itself
        //     "Plural" // they, them, their, theirs, themselves
        //     };

        // Text predicates are used in Text Knowledge Bases to direct text generation
        // static HashSet<string> TextPredicates = new HashSet<string>{
        //     "proper_noun", // e.g. (proper_noun PERSON1 "Samantha")
        //     "pronouns", // e.g. (pronouns PERSON1 Feminine)
        //     "common_noun", // e.g. (common_noun PERSON1 "accountant")
        //     "text", // e.g. (text Subject ?x "paid the fare to" Object ?y)
        //     "texta", // e.g. (texta Subject ?x "asked" Object ?y "to pay the fare")
        //     "textc", // e.g. (textc Subject ?x "paid the fare")
        // };

        // Base class constructor is tied to specific knowledge base and temlate axioms
        public TextGenerator(Knowledgebase kb, Knowledgebase tkb, List <Literal> tobs)
        {
            this.Kb          = kb;
            this.Tkb         = tkb;
            this.ProperNouns = new Dictionary <String, List <String> >();
            this.Pronouns    = new Dictionary <String, String>();
            this.CommonNouns = new Dictionary <String, List <String> >();

            foreach (Literal tob in tobs)
            {
                if (tob.Predicate == "proper_noun")
                {
                    String        key   = tob.Terms[0].Text;
                    List <String> value = tob.Terms.Skip(1).Select(t => t.Text).ToList(); // terms, not strings.
                    this.ProperNouns.Add(key, value);
                }
                if (tob.Predicate == "pronouns")
                {
                    String key   = tob.Terms[0].Text;
                    String value = tob.Terms[1].Text;
                    this.Pronouns.Add(key, value);
                }
                if (tob.Predicate == "common_noun")
                {
                    String        key   = tob.Terms[0].Text;
                    List <String> value = tob.Terms.Skip(1).Select(t => t.Text).ToList(); // terms, not strings.
                    this.CommonNouns.Add(key, value);
                }
            }
        }
Ejemplo n.º 5
0
        public static List <Solution> incremental_alternative(List <Literal> obs, Knowledgebase kb, int maxdepth, int n, int w, int b, bool sk)
        {
            var rt = new Realtime(kb, maxdepth, w, b);

            foreach (Literal ob in obs)
            {
                rt.Observe(ob);
            }
            return(rt.FinalSolutions());
        }
Ejemplo n.º 6
0
        public static List <Entailment> Entailments(Knowledgebase kb, List <Literal> assumptions)
        {
            var stack       = new Stack <Literal>(assumptions);
            var productions = Production.FromKb(kb);
            var entailed    = new List <Entailment>();

            while (stack.Count > 0)
            {
                var current         = stack.Pop();
                var new_productions = new List <Production>();
                foreach (Production prod in productions)
                {
                    for (int i = 0; i < prod.Antecedents.Count; i++)
                    {
                        var ant   = prod.Antecedents[i];
                        var theta = Unify.unify(current, ant);
                        if (theta != null)
                        {
                            var new_consequent = prod.Consequent.Subst(theta);
                            var new_triggers   = new List <Literal>(prod.Triggers); // deep!
                            new_triggers.Add(current);
                            if (prod.Antecedents.Count == 1)                        // last one
                            {
                                var new_entailment = new Entailment();
                                new_entailment.Entailed = new_consequent;
                                new_entailment.Triggers = new_triggers;
                                entailed.Add(new_entailment);
                                stack.Push(new_consequent);
                            }
                            else
                            {
                                var new_antecedents = new List <Literal>();
                                for (int j = 0; j < prod.Antecedents.Count; j++)
                                {
                                    if (i != j) // skip matching antecedent.
                                    {
                                        new_antecedents.Add(prod.Antecedents[j].Subst(theta));
                                    }
                                }
                                var new_production = new Production();
                                new_production.Antecedents = new_antecedents;
                                new_production.Consequent  = new_consequent;
                                new_production.Triggers    = new_triggers;
                                new_productions.Add(new_production);
                            }
                        }
                    }
                }
                foreach (Production p in new_productions)
                {
                    productions.Add(p);
                }
            }
            return(entailed);
        }
Ejemplo n.º 7
0
 public Realtime(Knowledgebase kb, int depth, int window, int beam)
 {
     this.knowledgebase = kb;
     this.observables   = new List <Literal>();
     this.contexts      = new List <Solution>();
     this.current       = 0;
     this.cached        = 0;
     this.solutions     = new List <Solution>();
     this.depth         = depth;
     this.window        = window;
     this.beam          = beam;
 }
Ejemplo n.º 8
0
        public static List <Production> FromKb(Knowledgebase kb)
        {
            var res = new List <Production>();

            foreach (DefiniteClause c in kb.Axioms)
            {
                var one = new Production();
                one.Antecedents = c.Antecedents;
                one.Consequent  = c.Consequent;
                one.Triggers    = new List <Literal>();
                res.Add(one);
            }
            return(res);
        }
Ejemplo n.º 9
0
        private static List <List <Literal> > ContextualAndOrLeaflists(List <Literal> remaining, Knowledgebase kb, int depth, List <Literal> context, List <Literal> antecedents, List <Literal> assumptions)
        {
            if (depth == 0 && antecedents.Count > 0) // fail with empty list (!)
            {
                return(new List <Solution>());
            }
            else if (remaining.Count == 0)  // done with this level
            {
                if (antecedents.Count == 0) // found one
                {
                    return(new List <Solution>()
                    {
                        assumptions
                    });                                          // list of lists
                }
                else
                {
                    return(ContextualAndOrLeaflists(antecedents, kb, depth - 1, context, new List <Literal>(), assumptions));
                }
            }

            else // more to go on this level
            {
                var literal = remaining[0]; // pop pt 1
                remaining.RemoveAt(0); // pop pt 2
                var predicate = literal.Predicate;
                if (!kb.IndexByConsequent.ContainsKey(predicate))
                {
                    assumptions.Add(literal); // shift literal to assumptions
                    return(ContextualAndOrLeaflists(remaining, kb, depth, context, antecedents, assumptions));
                }

                else
                {
                    var res   = new List <Solution>();
                    var rules = kb.IndexByConsequent[predicate];
                    foreach (DefiniteClause rule in rules)
                    {
                        var consequent = rule.Consequent;
                        var theta      = unify(literal, consequent);
                        if (theta != null)                     // unifies
                        {
                            if (depth == 0)                    // no depth for revision
                            {
                                return(new List <Solution>()); // (empty)
                            }
                            else
                            {
                                var new_remaining = new List <Literal>();
                                foreach (Literal c in remaining)
                                {
                                    new_remaining.Add(c.Subst(theta));
                                }

                                var new_antecedents = new List <Literal>();
                                foreach (Literal c in rule.Antecedents)
                                {
                                    new_antecedents.Add(c.Subst(theta));
                                }
                                new_antecedents = standardize(new_antecedents);
                                foreach (Literal c in antecedents)
                                {
                                    new_antecedents.Add(c.Subst(theta));
                                }

                                var new_assumptions = new List <Literal>();
                                foreach (Literal c in assumptions)
                                {
                                    new_assumptions.Add(c.Subst(theta)); // !!!! BUG
                                }

                                res.AddRange(ContextualAndOrLeaflists(new_remaining, kb, depth, context, new_antecedents, new_assumptions));
                            }
                        }
                    }
                    foreach (Literal context_literal in context)
                    {
                        var theta = unify(literal, context_literal);
                        if (theta != null) // unifies
                        {
                            var new_remaining = new List <Literal>();
                            foreach (Literal c in remaining)
                            {
                                new_remaining.Add(c.Subst(theta));
                            }

                            var new_antecedents = new List <Literal>();
                            foreach (Literal c in antecedents)
                            {
                                new_antecedents.Add(c.Subst(theta));
                            }

                            var new_assumptions = new List <Literal>();
                            foreach (Literal c in assumptions)
                            {
                                new_assumptions.Add(c.Subst(theta));
                            }

                            res.AddRange(ContextualAndOrLeaflists(new_remaining, kb, depth, context, new_antecedents, new_assumptions));
                        }
                    }
                    return(res);
                }
            }
        }
Ejemplo n.º 10
0
        public static List <Literal> GetContext(Solution solution, List <Literal> obs, Knowledgebase kb)
        {
            var res = new List <Literal>();

            foreach (Entailment entailment in Entailments(kb, solution))
            {
                if (!obs.Contains(entailment.Entailed) && !res.Contains(entailment.Entailed))
                {
                    res.Add(entailment.Entailed);
                }
            }
            return(res);
        }
Ejemplo n.º 11
0
        public static List <Solution> ContextualEtcAbduction(List <Literal> obs, List <Literal> window, Knowledgebase kb, List <Solution> previous, int maxdepth, int beam, int iteration)
        {
            var ln_pr_to_beat = double.NegativeInfinity;
            var n_best        = new List <Solution>();
            var n_best_ln_pr  = new List <Double>();

            foreach (Solution previous_solution in previous)
            {
                var previous_solution_jlpr = JointLnProbability(previous_solution);
                var context       = GetContext(previous_solution, obs, kb);
                var list_of_lists = new List <List <Solution> >();
                foreach (Literal c in window)
                {
                    var remaining = new List <Literal>()
                    {
                        c
                    };
                    list_of_lists.Add(ContextualAndOrLeaflists(remaining, kb, maxdepth, context, new List <Literal>(), new List <Literal>()));
                }
                var combiner = new Combiner(list_of_lists);
                foreach (Solution s in combiner)
                {
                    if (BestCaseLnProbability(s) > ln_pr_to_beat) // maybe!
                    {
                        foreach (Solution solution in Abduction.Crunch(s))
                        {
                            var jlpr = JointLnProbability(solution) + previous_solution_jlpr; //add ln
                            if (jlpr > ln_pr_to_beat)
                            {
                                solution.AddRange(previous_solution); // important!
                                var insert_at = n_best_ln_pr.Count;
                                for (int i = 0; i < n_best_ln_pr.Count; i++)
                                {
                                    if (n_best_ln_pr[i] > jlpr)
                                    {
                                        insert_at = i;
                                        break;
                                    }
                                }
                                n_best.Insert(insert_at, solution);
                                n_best_ln_pr.Insert(insert_at, jlpr);
                                if (n_best.Count > beam)
                                {
                                    n_best.RemoveAt(0);
                                    n_best_ln_pr.RemoveAt(0);
                                    ln_pr_to_beat = n_best_ln_pr[0]; // second worst [0] is now lowest
                                }
                            }
                        }
                    }
                }
            }

            n_best.Reverse(); // 0 is now highest;
            var pre = "$" + iteration.ToString() + ":";
            var res = new List <Solution>();

            foreach (Solution c in n_best)
            {
                res.Add(skolemize_with_prefix(c, pre));
            }
            return(res);
        }
Ejemplo n.º 12
0
        public static List <Solution> NBest(List <Literal> obs, Knowledgebase kb, int maxdepth, int n, bool sk)
        {
            var ln_pr_to_beat = double.NegativeInfinity;
            var n_best        = new List <Solution>();
            var n_best_ln_pr  = new List <Double>();
            var list_of_lists = new List <List <Solution> >();

            foreach (Literal c in obs)
            {
                var remaining = new List <Literal>()
                {
                    c
                };
                list_of_lists.Add(Abduction.AndOrLeaflists(remaining, kb, maxdepth, new List <Literal>(), new List <Literal>()));
            }
            var combiner = new Combiner(list_of_lists);

            foreach (Solution s in combiner)
            {
                if (BestCaseLnProbability(s) > ln_pr_to_beat)
                {
                    foreach (Solution solution in Abduction.Crunch(s))
                    {
                        var jpr = JointLnProbability(solution);
                        if (jpr > ln_pr_to_beat)
                        {
                            var insert_at = n_best_ln_pr.Count;
                            for (int i = 0; i < n_best_ln_pr.Count; i++)
                            {
                                if (n_best_ln_pr[i] > jpr)
                                {
                                    insert_at = i;
                                    break;
                                }
                            }
                            n_best.Insert(insert_at, solution);
                            n_best_ln_pr.Insert(insert_at, jpr);
                            if (n_best.Count > n)
                            {
                                n_best.RemoveAt(0);
                                n_best_ln_pr.RemoveAt(0);
                                //ln_pr_to_beat = jpr; // shouldn't this be the last item instead?
                                ln_pr_to_beat = n_best_ln_pr[0]; // second worst [0] is now lowest
                            }
                        }
                    }
                }
            }
            n_best.Reverse(); // 0 is now highest;
            if (sk)           // skolemize here
            {
                var res = new List <Solution>();
                foreach (Solution c in n_best)
                {
                    res.Add(skolemize(c));
                }
                return(res);
            }
            else
            {
                return(n_best);
            }
        }
Ejemplo n.º 13
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);
            }
        }
Ejemplo n.º 14
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);
            }
        }