/// <summary> /// Learns a program to extract a sequence of regions from a file. /// </summary> private static void LearnSequenceReferencingSibling() { var input = SequenceLearner.CreateStringRegion( "United States\nCarrie Dodson 100\nLeonard Robledo 75\nMargaret Cook 320\n" + "Canada\nConcetta Beck 350\nNicholas Sayers 90\nFrancis Terrill 2430\n" + "Great Britain\nNettie Pope 50\nMack Beeson 1070"); StringRegion[] areas = { input.Slice(0, 13), input.Slice(69, 75), input.Slice(134, 147) }; // Suppose we want to extract all last names from the input string. var examples = new[] { new MemberPrefix <StringRegion, StringRegion>(input, new[] { input.Slice(14, 20), // input => "Carrie" input.Slice(32, 39), // input => "Leonard" }) }; SequenceProgram topRankedProg = SequenceLearner.Instance.Learn(examples); if (topRankedProg == null) { Console.Error.WriteLine("Error: Learning fails!"); return; } foreach (var a in areas .SelectMany(area => topRankedProg.Run(area) .Select(output => new { Input = area, Output = output }))) { var output = a.Output != null ? a.Output.Value : "null"; Console.WriteLine("\"{0}\" => \"{1}\"", a.Input, output); } }
/// <summary> /// Learns a program to extract a sequence of regions using its preceding sibling as reference. /// </summary> private static void LearnSequence() { // It is advised to learn a sequence with at least 2 examples because generalizing a sequence from a single element is hard. // Also, we need to give positive examples continuously (i.e., we cannot skip any example). var input = SequenceLearner.CreateStringRegion( "United States\nCarrie Dodson 100\nLeonard Robledo 75\nMargaret Cook 320\n" + "Canada\nConcetta Beck 350\nNicholas Sayers 90\nFrancis Terrill 2430\n" + "Great Britain\nNettie Pope 50\nMack Beeson 1070"); // Suppose we want to extract all last names from the input string. var examples = new[] { new MemberPrefix <StringRegion, StringRegion>(input, new[] { input.Slice(14, 20), // input => "Carrie" input.Slice(32, 39), // input => "Leonard" }) }; SequenceProgram topRankedProg = SequenceLearner.Instance.Learn(examples); if (topRankedProg == null) { Console.Error.WriteLine("Error: Learning fails!"); return; } foreach (var r in topRankedProg.Run(input)) { var output = r != null ? r.Value : "null"; Console.WriteLine(output); } }