Exemplo n.º 1
0
        protected void Evaluate(int inputSetSize)
        {
            var inputRecords = GetRecords(inputSetSize);

            InputRecords = inputRecords.Count();

            var rawTokens = GetTokens(inputRecords);

            if (rawTokens == null || !rawTokens.Any())
            {
                throw new FormatikException("Could not find any similarities between the input document and the example");
            }

            this.Separators = GetOutputSeparators(rawTokens);

            Tokens = PurifyTokens(rawTokens, Separators);

            // construct output template
            SetTokenWrappers(Tokens, Separators);

            var firstToken = Tokens.First();
            var lastToken  = Tokens.Last();

            var headerSize = firstToken.Values
                             .Select(value => Example.IndexOf($"{firstToken.Prefix}{value.Value}{firstToken.Suffix}{(Separators.Count() > 1 ? Separators.Skip(1).First() : string.Empty)}"))
                             .Min(index => index);

            this.Header = headerSize > 0 ? Example.Substring(0, headerSize) : string.Empty;

            var footerStartAt = lastToken.Values
                                .Select(value =>
            {
                var lastTokenString = $"{(Separators.Count() > 1 ? Separators.Skip(1).First() : Separators.First())}{lastToken.Prefix}{value.Value}{lastToken.Suffix}";
                return(Example.LastIndexOf(lastTokenString) + lastTokenString.Length);
            })
                                .Max(index => index);

            this.Footer = footerStartAt > 0 ? Example.Substring(footerStartAt) : string.Empty;
        }
Exemplo n.º 2
0
        private int Process(IEnumerable <IInputRecord> records, Stream output, Encoding encoding)
        {
            var processed = 0;

            using (var writer = new StreamWriter(output, encoding, 8192, true))
            {
                if (!string.IsNullOrEmpty(Header))
                {
                    writer.Write(Header);
                }

                var isFirst            = true;
                var _1stOrderSeparator = Separators.First();
                var _2ndOrderSeparator = Separators.Skip(1).FirstOrDefault() ?? string.Empty;
                foreach (var record in records)
                {
                    if (isFirst)
                    {
                        isFirst = false;
                    }
                    else
                    {
                        writer.Write(_1stOrderSeparator);
                    }

                    writer.Write(String.Join(_2ndOrderSeparator,
                                             Tokens.Select(token => token.GetOutput(record))));

                    processed++;
                }

                if (!string.IsNullOrEmpty(Footer))
                {
                    writer.Write(Footer);
                }
            }

            return(processed);
        }