예제 #1
0
        public T Parse(string input)
        {
            input = ParseProcessor.Preprocess(input, null);

            var diff = DMP.diff_main(Template, input, false);

            return(ParseValues(input, diff, TokenNames));
        }
예제 #2
0
        static void Main(string[] args)
        {
            UI            ui  = UI.getInstance();
            List <string> str = ui.getUserInput();

            // Parsing STRING -> FORMAT -> MODEL
            ParseProcessor  parseProcessor = ParseProcessor.getInstance();
            IExpressionType mathmodel      = parseProcessor.parseFromInputToMathModel(str);

            // CALCULATIONS
            Context         ct         = Context.getInstance();
            Computator      computator = new Computator();
            IExpressionType computed   = mathmodel.doOperation(computator);

            // PARSING MODEL -> FORMAT -> STRING

            /*XmlDocument res = parseProcessor.parseToInputFromMathModel(computed);
             * Console.WriteLine(res.InnerXml);*/
        }
예제 #3
0
        private T ParseValues(string input, List <Diff> diffs, List <string> tokenNames)
        {
            var output = OutputBuilder.InitializeOutput(diffs);

            int variableIndex = -1;

            int valueIndex  = -1;
            int valueLength = 0;

            int commonLength      = 0;
            int diffTemplate      = 0;
            int diffInput         = 0;
            int totalParsedLength = 0;

            for (int i = 0; i <= diffs.Count; i++)
            {
                bool tokenProcessed = valueIndex != -1;

                if (i < diffs.Count)
                {
                    var part = diffs[i];
                    switch (part.operation)
                    {
                    case Operation.DELETE:
                    {
                        int cnt = Utilities.SubstringCount(part.text, ParseProcessor.ProcessedToken);
                        variableIndex += cnt;
                        diffTemplate  += part.text.Length - ParseProcessor.ProcessedToken.Length * cnt;

                        if (part.text.EndsWith(ParseProcessor.ProcessedToken))
                        {
                            valueIndex  = commonLength + diffInput;
                            valueLength = 0;
                        }
                    }
                    break;

                    case Operation.INSERT:
                    {
                        if (tokenProcessed)
                        {
                            totalParsedLength += part.text.Length;
                            valueLength       += part.text.Length;

                            tokenProcessed = false;
                        }

                        diffInput += part.text.Length;
                    }
                    break;

                    case Operation.EQUAL:
                    {
                        commonLength += part.text.Length;
                    }
                    break;
                    }
                }

                if (tokenProcessed)
                {
                    string parsedValue = ParseProcessor.Postprocess(input.Substring(valueIndex, valueLength), null);
                    output = OutputBuilder.TokenParsed(output, tokenNames[variableIndex], parsedValue);

                    valueIndex = -1;
                }
            }

            double differenceNormalized = 1 - (double)commonLength / (commonLength + Math.Max(diffTemplate, diffInput - totalParsedLength));

            output = OutputBuilder.FinalizeOutput(output, differenceNormalized);

            return(output);
        }