Beispiel #1
0
        private static void SimpleMovementTest()
        {
            var voc = Vocabulary.GetVocabularyFromFile(@"Vocabulary.json");
            var grammar = new Grammar(voc);
            Program.AddTypesToGrammar(grammar);

            grammar.AddProjectionTypeToTypeDictionary("CP", "V"); // no overt POS here, head of IP is V.
            grammar.AddProjectionTypeToTypeDictionary("IP", "V"); // no overt POS here, head of IP is V.
            grammar.AddRule(new Rule(1, "START", new[] {"CP"}, 0, 0));
            grammar.AddRule(new Rule(1, "CP", new[] {"IP"}, 0, 0));
            grammar.AddRule(new Rule(1, "IP", new[] {"NP", "VP"}, 1, 1));
            grammar.AddRule(new Rule(1, "VP", new[] {"V1", "NP"}, 0, 1));
            grammar.AddRule(new Rule(1, "VP", new[] {"V2", "PP"}, 0, 1));
            grammar.AddRule(new Rule(1, "PP", new[] {"P", "NP"}, 0, 1));
            grammar.AddRule(new Rule(1, "NP", new[] {"D", "N"}, 1, 0));
            grammar.AddNonTerminalToLandingSites("CP");
            grammar.AddMoveable("NP");
            grammar.AddMoveable("PP");
            grammar.GenerateDerivedRulesFromSchema();

            var parser = new Parser(grammar);
            var n = parser.ParseSentence("Who the bells toll for");
            n.Print();
            n = parser.ParseSentence("Who a man arrived from");
            n.Print();
            n = parser.ParseSentence("from Who a man arrived");
            n.Print();
            n = parser.ParseSentence("John David kissed");
            n.Print();
        }
Beispiel #2
0
        private static void Main(string[] args)
        {
            var voc = Vocabulary.GetVocabularyFromFile(@"Vocabulary.json");
            var grammar = new Grammar(voc);

            ProgramParams programParams;
            using (var file = File.OpenText(@"ProgramParameters.json"))
            {
                var serializer = new JsonSerializer();
                programParams = (ProgramParams) serializer.Deserialize(file, typeof(ProgramParams));
            }


            if (programParams.DataWithMovement)
                CreateMovementGrammar(grammar);
            else
                CreateSimpleGrammar(grammar);
            grammar.GenerateDerivedRulesFromSchema();

            var p = new Parser(grammar);
            var data = p.GenerateSentences(programParams.NumberOfDataSentences);

            using (var sw = File.AppendText("SessionReport.txt"))
            {
                sw.WriteLine("-------------------");
                sw.WriteLine("Session {0} ", DateTime.Now.ToString("MM/dd/yyyy h:mm tt"));
                sw.WriteLine("sentences: {0}, runs: {1}, movement: {2}", programParams.NumberOfDataSentences,
                    programParams.NumberOfRuns, programParams.DataWithMovement);
            }

            var stopWatch = StartWatch();

            var learner = new Learner(voc, grammar.NonTerminalsTypeDictionary, grammar.POSTypes, data, grammar);

            learner.originalGrammar.GenerateDerivedRulesFromSchema();
            var targetGrammarEnergy = learner.Energy(learner.originalGrammar);
            learner.originalGrammar.GenerateInitialRulesFromDerivedRules();
            var s = string.Format("Target Hypothesis:\n{0} with energy: {1}\n", learner.originalGrammar,
                targetGrammarEnergy);

            Console.WriteLine(s);
            using (var sw = File.AppendText("SessionReport.txt"))
            {
                sw.WriteLine(s);
            }

            for (var i = 0; i < programParams.NumberOfRuns; i++)
            {
                var sa = new SimulatedAnnealing(learner);
                sa.Run();
            }
            StopWatch(stopWatch);
        }