Exemplo n.º 1
0
        public static ProgramNode Learn(Grammar grammar, Spec spec,
                                        Feature <double> scorer, DomainLearningLogic witnessFunctions)
        {
            var engine = new SynthesisEngine(grammar, new SynthesisEngine.Config
            {
                Strategies = new ISynthesisStrategy[]
                {
                    new EnumerativeSynthesis(),
                    new DeductiveSynthesis(witnessFunctions)
                },
                UseThreads  = false,
                LogListener = new LogListener(),
            });
            ProgramSet consistentPrograms = engine.LearnGrammar(spec);

            engine.Configuration.LogListener.SaveLogToXML("learning.log.xml");

            //foreach (ProgramNode p in consistentPrograms.RealizedPrograms) {
            //    Console.WriteLine(p);
            //}

            ProgramNode bestProgram = consistentPrograms.TopK(scorer).FirstOrDefault();

            if (bestProgram == null)
            {
                WriteColored(ConsoleColor.Red, "No program :(");
                return(null);
            }
            var score = bestProgram.GetFeatureValue(scorer);

            WriteColored(ConsoleColor.Cyan, $"[score = {score:F3}] {bestProgram}");
            return(bestProgram);
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Learns all region programs that satisfy the examples (advanced feature).
        ///     Demonstrates access to the entire program set.
        /// </summary>
        private static void LearnAllRegionPrograms()
        {
            var          session = new RegionSession();
            StringRegion input   = RegionSession.CreateStringRegion("Carrie Dodson 100");

            session.AddConstraints(new RegionExample(input, input.Slice(14, 17))); // "Carrie Dodson 100" => "Dodson"

            ProgramSet allPrograms = session.LearnAll().ProgramSet;
            IEnumerable <ProgramNode> topKPrograms = allPrograms.TopK(RegionLearner.Instance.ScoreFeature, 3);

            var i = 0;

            StringRegion[] otherInputs =
            {
                input, RegionSession.CreateStringRegion("Leonard Robledo NA"),
                RegionSession.CreateStringRegion("Margaret Cook 320")
            };
            foreach (ProgramNode programNode in topKPrograms)
            {
                Console.WriteLine("Program {0}:", ++i);
                var program = new RegionProgram(programNode, ReferenceKind.Parent);
                foreach (StringRegion str in otherInputs)
                {
                    StringRegion r = program.Run(str);
                    Console.WriteLine(r == null ? "null" : r.Value);
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        ///     Learns all region programs that satisfy the examples (advanced feature).
        ///     Demonstrates access to the entire program set.
        /// </summary>
        private static void LearnAllRegionPrograms()
        {
            var input = StringRegion.Create("Carrie Dodson 100");

            var positiveExamples = new[] {
                new ExtractionExample <StringRegion>(input, input.Slice(14, 17)) // "Carrie Dodson 100" => "Dodson"
            };
            var negativeExamples = Enumerable.Empty <ExtractionExample <StringRegion> >();

            ProgramSet allPrograms = Learner.Instance.LearnAllRegion(positiveExamples, negativeExamples);
            IEnumerable <ProgramNode> topKPrograms = allPrograms.TopK("Score", 3); // "Score" is the ranking feature

            var i = 0;

            StringRegion[] otherInputs = { input, StringRegion.Create("Leonard Robledo NA"), StringRegion.Create("Margaret Cook 320") };
            foreach (var prog in topKPrograms)
            {
                Console.WriteLine("Program {0}:", ++i);
                foreach (var str in otherInputs)
                {
                    State  inputState = State.Create(Language.Grammar.InputSymbol, str); // Create Microsoft.ProgramSynthesis input state
                    object r          = prog.Invoke(inputState);                         // Invoke Microsoft.ProgramSynthesis program node on the input state
                    Console.WriteLine(r != null ? (r as StringRegion).Value : "null");
                }
            }
        }
Exemplo n.º 4
0
        public static ProgramNode Learn(Grammar grammar, Spec spec)
        {
            var engine = new SynthesisEngine(grammar, new SynthesisEngine.Config
            {
                UseThreads  = false,
                LogListener = new LogListener(),
            });
            ProgramSet consistentPrograms = engine.LearnGrammar(spec);

            engine.Configuration.LogListener.SaveLogToXML("learning.log.xml");

            //foreach (ProgramNode p in consistentPrograms.RealizedPrograms) {
            //    Console.WriteLine(p);
            //}

            ProgramNode bestProgram = consistentPrograms.TopK("Score").FirstOrDefault();

            if (bestProgram == null)
            {
                WriteColored(ConsoleColor.Red, "No program :(");
                return(null);
            }
            var score = bestProgram["Score"];

            WriteColored(ConsoleColor.Cyan, $"[score = {score:F3}] {bestProgram}");
            return(bestProgram);
        }
Exemplo n.º 5
0
        public ProgramNode[] LearnSQLTopK(DataTable inputTable, DataTable outputTable, int k)
        {
            var        spec = SpecFromStateOutput(inputTable, outputTable);
            ProgramSet consistentPrograms = LearnProgramSet(spec, new Semantics.WitnessFunctions(Grammar));
            int        nProgs             = consistentPrograms.AllElements.ToArray().Length;

            k = (nProgs < k) ? nProgs : k;
            return(consistentPrograms.TopK(QueryRanker, k).ToArray());
        }
Exemplo n.º 6
0
        private ProgramNode[] LearnAll(Spec spec, Feature <double> scorer, DomainLearningLogic witnessFunctions)
        {
            //See if there is a ranking function.
            ProgramSet consistentPrograms = LearnProgramSet(spec, witnessFunctions);

            if (scorer != null)
            {
                //If there is a ranking function then find the best program.
                ProgramNode bestProgram = consistentPrograms.TopK(scorer).FirstOrDefault();
                if (bestProgram == null)
                {
                    Utils.Utils.WriteColored(ConsoleColor.Red, "No program :(");
                    return(null);
                }
                var score = bestProgram.GetFeatureValue(scorer);
                Utils.Utils.WriteColored(ConsoleColor.Cyan, $"[score = {score:F3}] {bestProgram}");
                return(new ProgramNode[] { bestProgram });
            }
            return(consistentPrograms.AllElements.ToArray());
        }
Exemplo n.º 7
0
 public static IEnumerable <ProgramNode> RankPrograms(ProgramSet programs, int topK, IFeature feature)
 {
     return(programs.TopK(feature, topK));
 }