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); }
private static void LearnFromNewExample() { Console.Out.Write("Provide a new input-output example (e.g., \"(Sumit Gulwani)\",\"Gulwani\"): "); try { string input = Console.ReadLine(); if (input != null) { var startFirstExample = input.IndexOf("\"", StringComparison.Ordinal) + 1; var endFirstExample = input.IndexOf("\"", startFirstExample + 1, StringComparison.Ordinal) + 1; var startSecondExample = input.IndexOf("\"", endFirstExample + 1, StringComparison.Ordinal) + 1; var endSecondExample = input.IndexOf("\"", startSecondExample + 1, StringComparison.Ordinal) + 1; if ((startFirstExample >= endFirstExample) || (startSecondExample >= endSecondExample)) { throw new Exception("Invalid example format. Please try again. input and out should be between quotes"); } var inputExample = input.Substring(startFirstExample, endFirstExample - startFirstExample - 1); var outputExample = input.Substring(startSecondExample, endSecondExample - startSecondExample - 1); var inputState = State.CreateForExecution(Grammar.InputSymbol, inputExample); Examples.Add(inputState, outputExample); } } catch (Exception) { throw new Exception("Invalid example format. Please try again. input and out should be between quotes"); } var spec = new ExampleSpec(Examples); Console.Out.WriteLine("Learning a program for examples:"); foreach (var example in Examples) { Console.WriteLine("\"{0}\" -> \"{1}\"", example.Key.Bindings.First().Value, example.Value); } var scoreFeature = new RankingScore(Grammar); ProgramSet topPrograms = _prose.LearnGrammarTopK(spec, scoreFeature, 4, null); if (topPrograms.IsEmpty) { throw new Exception("No program was found for this specification."); } _topProgram = topPrograms.RealizedPrograms.First(); Console.Out.WriteLine("Top 4 learned programs:"); var counter = 1; foreach (var program in topPrograms.RealizedPrograms) { if (counter > 4) { break; } Console.Out.WriteLine("=========================="); Console.Out.WriteLine("Program {0}: ", counter); Console.Out.WriteLine(program.PrintAST(ASTSerializationFormat.HumanReadable)); counter++; } }
public IEnumerable <ProgramNode> GetTopKPrograms(Dictionary <string, string> examples, int k) { var prose = ConfigureSynthesis(grammar); var scoreFeature = new RankingScore(grammar); var programs = prose.LearnGrammarTopK(GetSpecification(examples), scoreFeature, k, null); return(programs); }
public void TestLearnSubstringNegativeAbsPosRanking() { Result <Grammar> grammar = CompileGrammar(); SynthesisEngine prose = ConfigureSynthesis(grammar.Value); State firstInput = State.CreateForExecution(grammar.Value.InputSymbol, "(Toby Miller)"); var examples = new Dictionary <State, object> { { firstInput, "Toby 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 output = topProgram.Invoke(firstInput) as string; Assert.AreEqual("Toby Miller", output); State secondInput = State.CreateForExecution(grammar.Value.InputSymbol, "(Courtney Lynch)"); output = topProgram.Invoke(secondInput) as string; Assert.AreEqual("Courtney Lynch", output); }
public void TestLearnSubstringNegativeAbsPosRanking() { var grammar = CompileGrammar(); var prose = ConfigureSynthesis(grammar.Value); var firstInput = State.CreateForExecution(grammar.Value.InputSymbol, "(Gustavo Soares)"); var examples = new Dictionary <State, object> { { firstInput, "Gustavo Soares" } }; var spec = new ExampleSpec(examples); var scoreFeature = new RankingScore(grammar.Value); var topPrograms = prose.LearnGrammarTopK(spec, scoreFeature, 1, null); var topProgram = topPrograms.RealizedPrograms.First(); var output = topProgram.Invoke(firstInput) as string; Assert.AreEqual("Gustavo Soares", output); var secondInput = State.CreateForExecution(grammar.Value.InputSymbol, "(Titus Barik)"); output = topProgram.Invoke(secondInput) as string; Assert.AreEqual("Titus Barik", output); }
public void TestLearnSubstringOneExample() { var grammar = CompileGrammar(); var prose = ConfigureSynthesis(grammar.Value); var input = State.CreateForExecution(grammar.Value.InputSymbol, "Gustavo Soares"); var examples = new Dictionary <State, object> { { input, "Soares" } }; var spec = new ExampleSpec(examples); var scoreFeature = new RankingScore(grammar.Value); var topPrograms = prose.LearnGrammarTopK(spec, scoreFeature, 1, null); var topProgram = topPrograms.RealizedPrograms.First(); var output = topProgram.Invoke(input) as string; Assert.AreEqual("Soares", output); var input2 = State.CreateForExecution(grammar.Value.InputSymbol, "Sumit Gulwani"); var output2 = topProgram.Invoke(input2) as string; Assert.AreEqual("Gulwani", output2); }
private static void LearnFromNewExample() { Console.Out.Write("Provide a new input-output example all integers only (e.g., {[1, 2, 3, 4], 7}:"); //\"(Sumit Gulwani)\",\"Gulwani\"): "); try { string input = Console.ReadLine(); if (input != null) { int startpos = input.IndexOf("["); int endpos = input.IndexOf("]"); int final_endpos = input.IndexOf("}"); int num_char_res = final_endpos - (endpos + 3); int des_result = int.Parse(input.Substring(endpos + 3, num_char_res).ToString()); int num_char = endpos - startpos; string substr = input.Substring(startpos + 1, num_char - 1); char[] separator = { ',' }; string[] substrlist = substr.Split(separator); var numbers = new List <int>(); foreach (string x in substrlist) { string temp = x.Trim(); numbers.Add(int.Parse(temp)); } if (input[0] != '{' || input[1] != '[') { throw new Exception( "Invalid example format. Please try again. Follow exact format, including presense/absence of whitespaces"); } int[] inputExample = numbers.ToArray(); int outputExample = des_result; State inputState = State.CreateForExecution(Grammar.InputSymbol, inputExample); Examples.Add(inputState, outputExample); } } catch (Exception) { throw new Exception("Invalid example format. Please try again. Follow exact format, including presense/absence of whitespaces"); } var spec = new ExampleSpec(Examples); Console.Out.WriteLine("Learning a program for examples:"); foreach (KeyValuePair <State, object> example in Examples) { int[] temp = (int[])example.Key.Bindings.First().Value; Console.WriteLine("\"{0}\" -> \"{1}\"", example.Key.Bindings.First().Value, example.Value); } var scoreFeature = new RankingScore(Grammar); ProgramSet topPrograms = _prose.LearnGrammarTopK(spec, scoreFeature, 50, null); if (topPrograms.IsEmpty) { throw new Exception("No program was found for this specification."); } _topProgram = topPrograms.RealizedPrograms.First(); Console.Out.WriteLine("Top 4 learned programs:"); var counter = 1; foreach (ProgramNode program in topPrograms.RealizedPrograms) { if (counter > 50) { break; } Console.Out.WriteLine("=========================="); Console.Out.WriteLine("Program {0}: ", counter); Console.Out.WriteLine(program.PrintAST(ASTSerializationFormat.HumanReadable)); counter++; } }
private static void LearnFromNewExample() { Console.Out.Write("Enter 4 elements of the input:\n"); try { Dictionary <uint?, uint?> d = new Dictionary <uint?, uint?>(); for (int t = 0; t < 4; t++) { //Console.WriteLine("Please enter integer" + (t+1) +": "); d.Add((uint)Convert.ToInt32(Console.ReadLine()), (uint)t); } Console.WriteLine("Please enter desired output: "); uint output = (uint)Convert.ToInt32(Console.ReadLine()); State inputState = State.CreateForExecution(Grammar.InputSymbol, d); Examples.Add(inputState, output); } catch (Exception) { throw new Exception("Invalid example"); } var spec = new ExampleSpec(Examples); Console.Out.WriteLine("Learning a program for examples:"); foreach (KeyValuePair <State, object> example in Examples) { Dictionary <uint?, uint?> temp = (Dictionary <uint?, uint?>)example.Key.Bindings.First().Value; Console.Write("["); foreach (uint?m in temp.Keys) { Console.Write(m + ","); } Console.Write("] "); Console.Write("-> \"{0}\"\n", example.Value); } var scoreFeature = new RankingScore(Grammar); ProgramSet topPrograms = _prose.LearnGrammarTopK(spec, scoreFeature, 4, null); if (topPrograms.IsEmpty) { throw new Exception("No program was found for this specification."); } _topProgram = topPrograms.RealizedPrograms.First(); Console.Out.WriteLine("Top 4 learned programs:"); var counter = 1; foreach (ProgramNode program in topPrograms.RealizedPrograms) { if (counter > 4) { break; } Console.Out.WriteLine("=========================="); Console.Out.WriteLine("Program {0}: ", counter); Console.Out.WriteLine(program.PrintAST(ASTSerializationFormat.HumanReadable)); counter++; } }
private static void LearnFromNewExample() { Console.Out.Write("Provide a new input-output example (e.g., \"10 True\",\"20 False\"): "); try { string line = Console.ReadLine(); if (line == null) { throw new Exception("No Input"); } string[] splittedLine = line.Split(' '); var inputExample = UInt32.Parse(splittedLine[0]); var outputExample = Boolean.Parse(splittedLine[1]); State inputState = State.CreateForExecution(Grammar.InputSymbol, inputExample); // TODO: Check example existence if (Examples.ContainsKey(inputState)) { if ((bool)Examples[inputState] != outputExample) { throw new Exception("Inconsistent Example"); } } else { Examples.Add(inputState, outputExample); } } catch (Exception e) { Console.WriteLine(e.ToString()); throw e; //throw new Exception("Invalid example format. Please try again. input and out should be between quotes"); } var spec = new ExampleSpec(Examples); Console.Out.WriteLine("Learning a program for examples:"); foreach (KeyValuePair <State, object> example in Examples) { Console.WriteLine("\"{0}\" -> \"{1}\"", example.Key.Bindings.First().Value, example.Value); } var scoreFeature = new RankingScore(Grammar); ProgramSet topPrograms = _prose.LearnGrammarTopK(spec, scoreFeature, 1, null); if (topPrograms.IsEmpty) { throw new Exception("No program was found for this specification."); } _topProgram = topPrograms.RealizedPrograms.First(); Console.Out.WriteLine("Top 4 learned programs:"); var counter = 1; foreach (ProgramNode program in topPrograms.RealizedPrograms) { if (counter > 4) { break; } Console.Out.WriteLine("=========================="); Console.Out.WriteLine("Program {0}: ", counter); Console.Out.WriteLine(program.PrintAST(ASTSerializationFormat.HumanReadable)); counter++; } }