Beispiel #1
0
 public static IEnumerable <char> GetEnumerable(this ICharacterSource source)
 {
     for (long i = 0; i < source.Length; i++)
     {
         yield return(source[i]);
     }
 }
Beispiel #2
0
        public static string Build(this ICharacterSource source)
        {
            var result = new char[source.Length];

            for (long i = 0; i < source.Length; i++)
            {
                result[i] = source[i];
            }

            return(new string(result));
        }
        public NeuralOCRModel(ICharacterSource characterGenerator, List <int> hiddenLayerSizes)
        {
            ImageWidth              = characterGenerator.ImageWidth;
            ImageHeight             = characterGenerator.ImageHeight;
            this.characterGenerator = characterGenerator;
            var layerSizes = new List <int>();

            layerSizes.Add(ImageWidth * ImageHeight); // input layer
            layerSizes.AddRange(hiddenLayerSizes);
            layerSizes.Add(10);                       // output layer
            NeuralNetwork = new NeuralNetwork(layerSizes);
            NeuralNetwork.InitializeWeights(0.2);
            NeuralNetwork.InitializeBiases(0.2);
        }
Beispiel #4
0
        public LiteralExpression(ICharacterSource value)
        {
            var str = value.Build();

            if (str.Length > 2 && str.ToLower().StartsWith("0x") && long.TryParse(str.Substring(2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out long hexValue))
            {
                Value = hexValue;
            }
            else if (long.TryParse(str, out long decimalValue))
            {
                Value = decimalValue;
            }
            else
            {
                throw new SourceError(value, "Invalid literal.");
            }
        }
Beispiel #5
0
        public CharacterRegion(ICharacterSource source, long index, long length)
        {
            Source = source;

            if (index >= Source.Length || index < 0)
            {
                throw new IndexOutOfRangeException();
            }

            Index = index;

            if (index + length > Source.Length)
            {
                throw new IndexOutOfRangeException();
            }

            Length = length;
        }
Beispiel #6
0
        /// <summary>
        /// Visualizes a few examples and their classification outcome to get an overall impression of the network's accuracy.
        /// </summary>
        /// <param name="network">The neural network to test.</param>
        /// <param name="characterSource">The character source to generate samples from.</param>
        /// <param name="rows">Amount of examples to show vertically.</param>
        /// <param name="columns">Amount of examples to show horizontally.</param>
        /// <returns>An object that can be assigned directly to an ImageControl's Source property.</returns>
        public static ImageSource DrawTestGallery(IOCRModel ocrModel, ICharacterSource characterSource, int rows, int columns)
        {
            const int inputScaleFact = 2;
            const int labelHeight    = 16;
            int       width          = (characterSource.ImageWidth * inputScaleFact + sectionPadding) * columns;
            int       height         = (characterSource.ImageHeight * inputScaleFact + labelHeight + sectionPadding) * rows;
            var       output         = new WriteableBitmap(width, height, 96.0, 96.0, PixelFormats.Rgb24, null);

            byte[] bytes   = new byte[width * height * 3];
            var    samples = characterSource.GenerateMulti(rows * columns);
            int    baseY   = 0;

            for (int i = 0; i < rows; i++)
            {
                int baseX = 0;
                for (int j = 0; j < columns; j++)
                {
                    var sample = samples[i * columns + j];
                    drawInput(sample.Image, bytes, inputScaleFact, baseX, baseY, width);
                    var  result  = ocrModel.ExecuteSingle(sample.Image);
                    bool correct = (result.MostConfident == sample.Character);
                    for (int y = baseY + characterSource.ImageHeight * inputScaleFact + labelHeight / 4;
                         y < baseY + characterSource.ImageHeight * inputScaleFact + labelHeight / 2; y++)
                    {
                        for (int x = baseX; x < baseX + characterSource.ImageWidth * inputScaleFact; x++)
                        {
                            bytes[(x + y * width) * 3]     = (byte)(correct ? 0 : 255);
                            bytes[(x + y * width) * 3 + 1] = (byte)(correct ? 255 : 0);
                            bytes[(x + y * width) * 3 + 2] = (byte)0;
                        }
                    }
                    baseX += characterSource.ImageWidth * inputScaleFact + sectionPadding;
                }
                baseY += characterSource.ImageHeight * inputScaleFact + labelHeight + sectionPadding;
            }

            output.WritePixels(new Int32Rect(0, 0, width, height), bytes, width * 3, 0);
            return(output);
        }
Beispiel #7
0
 public static ICharacterSource Concat(this ICharacterSource first, ICharacterSource second)
 {
     return(new CombinedSources(first, second));
 }
Beispiel #8
0
 public static Document Parse(ICharacterSource source)
 {
     return(BuildDocument(ParseTokens(GetTokens(source))));
 }
Beispiel #9
0
 public SourceError(ICharacterSource source, long index, long length, string message) : base(message)
 {
     ErrorSource = new CharacterRegion(source, index, length);
 }
Beispiel #10
0
 private static ICharacterSource ResolveSource(Resolver resolver, string parent, ICharacterSource source)
 {
     try
     {
         return(new StringSource("&<resolved>", resolver(parent, source.Build())));
     }
     catch (KeyNotFoundException)
     {
         throw new SourceError(source, "Name could not be resolved.");
     }
 }
 public NeuralOCRModel(ICharacterSource characterGenerator)
     : this(characterGenerator, new List <int>() { 32, 16 })
 {
 }
Beispiel #12
0
 public CallExpression(ICharacterSource call)
 {
     Call = call;
 }
Beispiel #13
0
 public Statement(ICharacterSource name, Expression expression) : base(name)
 {
     Expression = expression;
 }
 public ReferenceExpression(ICharacterSource reference)
 {
     Reference = reference;
 }
Beispiel #15
0
        private static Token[] GetTokens(ICharacterSource source)
        {
            var result = new List <Token>();

            for (long i = 0; i < source.Length; i++)
            {
                if (IsNameChar(source[i], true))
                {
                    var length = 1L;

                    for (long j = i + 1; j < source.Length && IsNameChar(source[j], false); j++, length++)
                    {
                    }

                    result.Add(new Token
                    {
                        Type  = TokenType.Name,
                        Value = source.Substring(i, length)
                    });

                    i += length - 1;
                }
                else if (IsValueChar(source[i], true))
                {
                    var length = 1L;

                    for (long j = i + 1; j < source.Length && IsValueChar(source[j], false); j++, length++)
                    {
                    }

                    if (length == 1 && source[i] == '-')
                    {
                        result.Add(new Token
                        {
                            Type  = TokenType.Symbol,
                            Value = source.Substring(i, length)
                        });
                    }
                    else
                    {
                        result.Add(new Token
                        {
                            Type  = TokenType.Value,
                            Value = source.Substring(i, length)
                        });
                    }

                    i += length - 1;
                }
                else if (IsSymbolChar(source[i], true))
                {
                    var length = 1L;

                    for (long j = i + 1; j < source.Length && IsSymbolChar(source[j], false); j++, length++)
                    {
                    }

                    result.Add(new Token
                    {
                        Type  = TokenType.Symbol,
                        Value = source.Substring(i, length)
                    });

                    i += length - 1;
                }
            }

            return(result.ToArray());
        }
Beispiel #16
0
 public Structure(ICharacterSource name) : base(name)
 {
 }
Beispiel #17
0
 public Field(ICharacterSource type, ICharacterSource name, Expression initialValue = null) : base(name)
 {
     Type         = type;
     InitialValue = initialValue;
 }
Beispiel #18
0
 public Member(ICharacterSource name)
 {
     Name = name;
 }
Beispiel #19
0
 public SourceError(ICharacterSource source, string message) : base(message)
 {
     ErrorSource = source;
 }
Beispiel #20
0
 public static ICharacterSource Substring(this ICharacterSource source, long index, long length)
 {
     return(new CharacterRegion(source, index, length));
 }
Beispiel #21
0
 public Method(ICharacterSource returnType, ICharacterSource name) : base(name)
 {
     ReturnType = returnType;
 }
Beispiel #22
0
 public Token(TokenType type, ICharacterSource value)
 {
     Type  = type;
     Value = value;
 }