예제 #1
0
        /// <summary>
        ///     Learns a programs for formatting a name but serializes and deserializes it before running it.
        ///     Demonstrates serializing a FlashFillProgram to a string.
        /// </summary>
        private static void SerializeProgram()
        {
            IEnumerable <FlashFillExample> examples = new[]
            {
                new FlashFillExample("Kettil Hansson", "Hansson, K.")
            };
            FlashFillProgram topRankedProgram = FlashFillProgram.Learn(examples);

            if (topRankedProgram == null)
            {
                Console.Error.WriteLine("Error: failed to learn format name program.");
            }
            else
            {
                // FlashFillPrograms can be serialized using .ToString().
                string serializedProgram = topRankedProgram.ToString();
                // Serialized programs can be loaded in another program using the FlashFill API using .Load():
                var parsedProgram = FlashFillProgram.Load(serializedProgram);
                foreach (var name in new[] { "Etelka Bala", "Myron Lampros" })
                {
                    string formatted = parsedProgram.Run(name);
                    Console.WriteLine("\"{0}\" => \"{1}\"", name, formatted);
                }
            }
        }
예제 #2
0
        /// <summary>
        ///     Learns a program to convert dates from "DD/MM/YYYY" to "YYYY-MM-DD".
        ///     Demonstrates providing examples using <see cref="string" /> instead of <see cref="FlashFillExample" />
        ///     and providing additional inputs.
        /// </summary>
        private static void LearnNormalizeDate()
        {
            // Can give FlashFillProgram's .Learn() function an IDictionary<string, string>
            //  instead of an IEnumerable of FlashFillExample.
            IDictionary <string, string> examples = new Dictionary <string, string>
            {
                { "02/04/1953", "1953-04-02" }
            };
            // Inputs for which the corresponding output is not known. May be used for improving ranking.
            // Given as strings instead of FlashFillInputs when the examples are given as Tuple<string, string>.
            IEnumerable <string> additionalInputs = new[]
            {
                "04/02/1962",
                "27/08/1998"
            };
            FlashFillProgram topRankedProgram = FlashFillProgram.Learn(examples, additionalInputs);

            if (topRankedProgram == null)
            {
                Console.Error.WriteLine("Error: failed to learn normalize date program.");
            }
            else
            {
                foreach (var date in new[] { "12/02/1972", "31/01/1983" })
                {
                    string normalized = topRankedProgram.Run(date);
                    Console.WriteLine("\"{0}\" => \"{1}\"", date, normalized);
                }
            }
        }
예제 #3
0
        /// <summary>
        ///     Learn to take two strings of a first name and last name and combine them into "Last, First" format.
        ///     Demonstrates inputs with multiple strings (columns) and also providing inputs without a known output.
        /// </summary>
        private static void LearnMergeNames()
        {
            // Inputs may be made up of multiple strings. If so, all inputs must contain the same number of strings.
            IEnumerable <FlashFillExample> examples = new[]
            {
                new FlashFillExample(new FlashFillInput("Kettil", "Hansson"), "Hansson, Kettil")
            };

            // Inputs for which the corresponding output is not known. May be used for improving ranking.
            FlashFillInput[] additionalInputs =
            {
                new FlashFillInput("Greta", "Hermansson")
            };
            FlashFillProgram topRankedProgram = FlashFillProgram.Learn(examples, additionalInputs);

            if (topRankedProgram == null)
            {
                Console.Error.WriteLine("Error: failed to learn merge names program.");
            }
            else
            {
                var testInputs = new[] { new FlashFillInput("Etelka", "Bala"), new FlashFillInput("Myron", "Lampros") };
                foreach (var name in testInputs)
                {
                    string merged = topRankedProgram.Run(name);
                    Console.WriteLine("{0} => \"{1}\"", name, merged);
                }
                // Instead of a FlashFillInput, .Run() can also take the inputs as an IEnumerable<string>
                //  or as a params string[]:
                Console.WriteLine("\"Nelly\", \"Akesson\" => \"{0}\"",
                                  topRankedProgram.Run(new List <string> {
                    "Nelly", "Akesson"
                }));
                Console.WriteLine("\"Nelly\", \"Akesson\" => \"{0}\"",
                                  topRankedProgram.Run("Nelly", "Akesson"));
            }
        }
예제 #4
0
        /// <summary>
        ///     Learn to normalize phone numbers in a few input formats to the same output format.
        ///     Demonstrates giving FlashFill multiple examples.
        /// </summary>
        private static void LearnNormalizePhoneNumber()
        {
            // Some programs may require multiple examples.
            // More examples ensures the proper program is learned and may speed up learning.
            IEnumerable <FlashFillExample> examples = new[]
            {
                new FlashFillExample("425-829-5512", "425-829-5512"),
                new FlashFillExample("(425) 829 5512", "425-829-5512")
            };
            FlashFillProgram topRankedProgram = FlashFillProgram.Learn(examples);

            if (topRankedProgram == null)
            {
                Console.Error.WriteLine("Error: failed to learn normalize phone number program.");
            }
            else
            {
                foreach (var phoneNumber in new[] { "425 233 1234", "(425) 777 3333" })
                {
                    string normalized = topRankedProgram.Run(phoneNumber);
                    Console.WriteLine("\"{0}\" => \"{1}\"", phoneNumber, normalized);
                }
            }
        }
예제 #5
0
        /// <summary>
        ///     Learn to reformat a name written "First Last" as "Last, F." where 'F' is the first initial.
        ///     Demonstrates basic usage of FlashFill API.
        /// </summary>
        private static void LearnFormatName()
        {
            // Examples are given as a FlashFillExample object which takes an input and output.
            IEnumerable <FlashFillExample> examples = new[]
            {
                new FlashFillExample("Kettil Hansson", "Hansson, K.")
            };
            // Given just the examples, the best program is returned
            FlashFillProgram topRankedProgram = FlashFillProgram.Learn(examples);

            if (topRankedProgram == null)
            {
                Console.Error.WriteLine("Error: failed to learn format name program.");
            }
            else
            {
                // Run the program on some new inputs.
                foreach (var name in new[] { "Etelka Bala", "Myron Lampros" })
                {
                    string formatted = topRankedProgram.Run(name);
                    Console.WriteLine("\"{0}\" => \"{1}\"", name, formatted);
                }
            }
        }