public void InitialTest()
        {
            RulesKeyValues = new Dictionary<List<p.TermsRight>, string>();
            Values = new List<string>();
            var sentence = new List<string> { "I", "feel", "it" };
            p.Grammar grammar = new p.Grammar();
            MaryRunsTest();
            grammar.InitTest(RulesKeyValues, Values);
            p.EarleyParser parser = new p.EarleyParser();
            parser.Parse(sentence, grammar);

            var x = new JavaScriptSerializer().Serialize(parser.Charts);
            this.Context.Response.Write(x);
        }
示例#2
0
        public static void RunTest(p.Grammar grammar, List<string> sentence)
        {
            //Printing out what the sentence is
            Console.WriteLine(p.GetStringFromList(sentence));

            //Initializing parser
            p.EarleyParser parser = new p.EarleyParser();

            //Printing information if parsing was successfull (sentence was parsed for given grammar or not)
            Console.WriteLine(parser.Parse(sentence, grammar) ? "Parsed successfully +" : "Parsing was unsuccessfull -");

            //Printing chart
            parser.PrintOutChart();
            EarleyParserLogic.ASTLogic.BuildAbstractSyntaxTree(parser);
            Program.WaitForPress();
        }
        public string ParseSentence(string sentenceToParse)
        {
            List<Response> response =
                (List<Response>)new JavaScriptSerializer().Deserialize(sentenceToParse, typeof(List<Response>));

            RulesKeyValues = new Dictionary<List<p.TermsRight>, string>();
            Values = new List<string>();

            var sentence = response[0]
                .sentence?.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries)?.ToList();

            if (sentence == null || !sentence.Any())
                CustomErrorParseSentence(out response, out sentence);
            p.Grammar grammar = new p.Grammar();

            Dictionary<string, List<string>> grammarHelper = new Dictionary<string, List<string>>();
            foreach (var rule in response[0].rules)
            {
                if (grammarHelper.Any(r => r.Key == rule.leftSide))
                    grammarHelper[rule.leftSide].Add(rule.rightSide);
                else grammarHelper.Add(rule.leftSide, new List<string>() { rule.rightSide });
            }

            foreach (var entity in grammarHelper)
            {
                List<p.TermsRight> terms = new List<p.TermsRight>();
                foreach (var termRight in entity.Value)
                {
                    List<string> termsRight = termRight.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries).ToList();
                    terms.Add(new p.TermsRight(termsRight));
                    foreach (var t in termsRight)
                        if (t.All(x => char.IsLower(x)) || t.All(x => char.IsNumber(x)))
                            Values.Add(entity.Key);
                }
                RulesKeyValues.Add(p.Grammar.ListOfListsBuilder(terms.ToArray()), entity.Key);
            }

            grammar.InitTest(RulesKeyValues, Values);
            p.EarleyParser parser = new p.EarleyParser();
            parser.Parse(sentence, grammar);
            var JSONresult = new JavaScriptSerializer().Serialize(parser.Charts);
            return JSONresult;
        }