示例#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();
        }
示例#2
0
        private static void TestAmbiguityGrammar()
        {
            var voc = Vocabulary.GetVocabularyFromFile(@"Vocabulary.json");
            var grammar = new Grammar(voc);
            Program.AddTypesToGrammar(grammar);


            grammar.AddRule(new Rule(0, "START", new[] {"VP"}, 0, 0));
            grammar.AddRule(new Rule(1, "VP", new[] {"V1", "NP"}, 0, 1));
            grammar.AddRule(new Rule(1, "VP", new[] {"VP", "NP"}, 0, 0));
            grammar.AddRule(new Rule(0, "NP", new[] {"NP", "NP"}, 0, 0));


            var parser = new Parser(grammar, true);
            var n = parser.ParseSentence("kissed John David");
            n.Print();
        }
示例#3
0
        private Tuple<Node, int>[] ParseAllSentences(Grammar currentHypothesis)
        {
            var allParses = new Tuple<Node, int>[sentencesWithCounts.Count];

            try
            {
                Parallel.ForEach(sentencesWithCounts, (sentenceItem, state, i) =>
                {
                    var parser = new Parser(currentHypothesis);
                    var n = parser.ParseSentence(sentenceItem.Key);
                    allParses[i] = Tuple.Create(n, sentenceItem.Value);
                });
                return allParses;
            }
            catch (Exception)
            {
                return null; //parsing failed.
            }
        }
示例#4
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);
        }
示例#5
0
        private static void TestMovement2()
        {
            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[] {"VP", "VP"}, 1, 1));
            grammar.AddRule(new Rule(1, "VP", new[] {"VP", "PP"}, 0, 1));
            grammar.AddRule(new Rule(1, "VP", new[] {"NP", "V1"}, 1, 0));
            grammar.AddRule(new Rule(1, "PP", new[] {"V2", "P"}, 1, 0));
            grammar.AddRule(new Rule(1, "NP", new[] {"D", "N"}, 1, 0));


            var parser = new Parser(grammar, true);
            var n = parser.ParseSentence("the man arrived to Mary"); //supposed to fail in parsing!!
            n.Print();
        }