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 void FirstTest(List<string> sentence, TestsAvailable test)
        {
            RulesKeyValues = new Dictionary<List<p.TermsRight>, string>();
            Values = new List<string>();
            switch (test)
            {
                case TestsAvailable.AddAndMultiplyNumbersTest:
                    AddAndMultiplyNumberTest();
                    break;
                case TestsAvailable.JohnMaryTest:
                    JohnMaryTest();
                    break;
                case TestsAvailable.MaryRunsTest:
                    MaryRunsTest();
                    break;
                default:
                    break;
            }

            //Gramar initalizing with Rules created above
            p.Grammar grammar = new p.Grammar();
            grammar.InitTest(RulesKeyValues, Values);

            //Runing test for given grammar and sentence
            RunTest(grammar, sentence);
        }
        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;
        }