Beispiel #1
0
        private static void Main(string[] args)
        {
            var jsonText = @"{""data"": [
                                {""person"": {
                                        ""name"": {
                                                ""first"": ""Carrie"", 
                                                ""last"": ""Dodson""
                                        }, 
                                        ""address"": ""1 Microsoft Way"", 
                                        ""phone number"": []
                                        }
                                }, 
                                {""person"": {
                                        ""name"": {
                                                ""first"": ""Leonard"", 
                                                ""last"": ""Robledo""
                                        }, 
                                        ""phone number"": [
                                                ""123-4567-890"", 
                                                ""456-7890-123"", 
                                                ""789-0123-456""
                                        ]}
                                } 
                             ]}";

            // Learn a program with the constraint to flatten the entire document.
            var learner = new Learner();
            CompoundExtractionProgram <JsonRegion> program = learner.Learn(new[] { new FlattenDocument(jsonText) });

            if (program == null)
            {
                Console.WriteLine("Fail to learn a program!");
                return;
            }

            // Serialize and deserialize the program
            var programText = program.Serialize();

            program = Loader.Instance.Load(programText);

            if (program == null)
            {
                Console.WriteLine("Fail to load deserialized program!");
                return;
            }

            // Run the program and obtain the tree output
            ITreeOutput <JsonRegion> tree = program.Run(jsonText);

            // Run the program and obtain the table output using outer join semantics
            IEnumerable <TableRow <JsonRegion> > table = program.RunTable(jsonText, TreeToTableSemantics.OuterJoin);

            Console.WriteLine("OuterJoin Semantic Table!");
            PrintTable(table);

            // Run the program and obtain the table output using inner join semantics
            table = program.RunTable(jsonText, TreeToTableSemantics.InnerJoin);
            Console.WriteLine("InnerJoin Semantic Table!");
            PrintTable(table);

            Console.ReadKey();
        }
Beispiel #2
0
        private static void Main(string[] args)
        {
            var jsonText = @"[
                                {""person"": {
                                        ""name"": {
                                                ""first"": ""Carrie"", 
                                                ""last"": ""Dodson""
                                        }, 
                                        ""address"": ""1 Microsoft Way"", 
                                        ""phone number"": []
                                        }
                                }, 
                                {""person"": {
                                        ""name"": {
                                                ""first"": ""Leonard"", 
                                                ""last"": ""Robledo""
                                        }, 
                                        ""phone number"": [
                                                ""123-4567-890"", 
                                                ""456-7890-123"", 
                                                ""789-0123-456""
                                        ]}
                                } 
                             ]";

            // Option 1: Joining Inner Arrays
            // Learn a program with the constraint to flatten the entire document.
            var session = new Session();

            session.AddConstraints(new FlattenDocument(jsonText));
            Program program = session.Learn();

            if (program == null)
            {
                Console.WriteLine("Fail to learn a program!");
                return;
            }

            // Serialize and deserialize the program
            var programText = program.Serialize();

            program = Loader.Instance.Load(programText);

            if (program == null)
            {
                Console.WriteLine("Fail to load deserialized program!");
                return;
            }

            // Run the program and obtain the tree output
            ITreeOutput <JsonRegion> tree = program.Run(jsonText);

            // Run the program and obtain the table output using outer join semantics
            IEnumerable <TableRow <JsonRegion> > outerJoinTable = program.RunTable(jsonText, TreeToTableSemantics.OuterJoin);

            Console.WriteLine("OuterJoin Semantic Table!");
            PrintTable(outerJoinTable);

            // Run the program and obtain the table output using inner join semantics
            IEnumerable <TableRow <JsonRegion> > innerJoinTable = program.RunTable(jsonText, TreeToTableSemantics.InnerJoin);

            Console.WriteLine("InnerJoin Semantic Table!");
            PrintTable(innerJoinTable);

            // Option 2: No Joining Inner Arrays
            var noJoinSession = new Session();

            noJoinSession.AddConstraints(new FlattenDocument(jsonText), new NoJoinInnerArrays());
            Program noJoinProgram = noJoinSession.Learn();

            if (noJoinProgram == null)
            {
                Console.WriteLine("Fail to learn a program!");
                return;
            }

            // Run the program and obtain the table output
            IEnumerable <TableRow <JsonRegion> > table = noJoinProgram.RunTable(jsonText);

            Console.WriteLine("No Joining Inner Array Table!");
            PrintTable(table);

            Console.WriteLine("Done.");
        }