Ejemplo n.º 1
0
        public void TestLearnSubstringOneExample()
        {
            Result <Grammar> grammar = CompileGrammar();
            SynthesisEngine  prose   = ConfigureSynthesis(grammar.Value);

            State input    = State.CreateForExecution(grammar.Value.InputSymbol, "Toby Miller");
            var   examples = new Dictionary <State, object> {
                { input, "Miller" }
            };

            var spec = new ExampleSpec(examples);

            var         scoreFeature = new RankingScore(grammar.Value);
            ProgramSet  topPrograms  = prose.LearnGrammarTopK(spec, scoreFeature, 1, null);
            ProgramNode topProgram   = topPrograms.RealizedPrograms.First();

            var x = topProgram.PrintAST();
            var y = ProgramNode.Parse(x, grammar.Value); // var y is null. ==> SDK method

            var se = new ASTSerialization.Serialization(grammar.Value);
            var xe = se.PrintXML(topProgram);

            xe.Save(SavedProgramAST);
            topProgram = se.Parse(xe); // var topProgram is ok. ==> ASTSerialization.Serialization method

            var output = topProgram.Invoke(input) as string;

            Assert.AreEqual("Miller", output);

            State input2  = State.CreateForExecution(grammar.Value.InputSymbol, "Courtney Lynch");
            var   output2 = topProgram.Invoke(input2) as string;

            Assert.AreEqual("Lynch", output2);
        }
Ejemplo n.º 2
0
        // dump to JSON
        public JObject DumpJSON()
        {
            var obj           = new JObject();
            var serialization = new ASTSerialization.Serialization(TLearner._grammar);
            var serializedAST = serialization.PrintXML(_program);

            // File.WriteAllText("/Users/xrc/Repository/Prem/logs/ast.xml",x);
            // System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(ProgramNode));
            // Stream stream = new MemoryStream();
            // xs.Serialize(stream, _program);
            // var y = xs.Deserialize(stream);
            // var gm = _program.Grammar;
            // var y = ProgramNode.Parse(x, gm,ASTSerializationFormat.XML);
            // var z = System.Xml.Linq.XElement.Parse(x);
            // var y = ProgramNode.ParseXML(TLearner._grammar,z);
            obj.Add("program", serializedAST.ToString());
            obj.Add("score", score);
            return(obj);
        }