public static IEnumerable <char> GetEnumerable(this ICharacterSource source) { for (long i = 0; i < source.Length; i++) { yield return(source[i]); } }
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); }
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."); } }
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; }
/// <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); }
public static ICharacterSource Concat(this ICharacterSource first, ICharacterSource second) { return(new CombinedSources(first, second)); }
public static Document Parse(ICharacterSource source) { return(BuildDocument(ParseTokens(GetTokens(source)))); }
public SourceError(ICharacterSource source, long index, long length, string message) : base(message) { ErrorSource = new CharacterRegion(source, index, length); }
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 }) { }
public CallExpression(ICharacterSource call) { Call = call; }
public Statement(ICharacterSource name, Expression expression) : base(name) { Expression = expression; }
public ReferenceExpression(ICharacterSource reference) { Reference = reference; }
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()); }
public Structure(ICharacterSource name) : base(name) { }
public Field(ICharacterSource type, ICharacterSource name, Expression initialValue = null) : base(name) { Type = type; InitialValue = initialValue; }
public Member(ICharacterSource name) { Name = name; }
public SourceError(ICharacterSource source, string message) : base(message) { ErrorSource = source; }
public static ICharacterSource Substring(this ICharacterSource source, long index, long length) { return(new CharacterRegion(source, index, length)); }
public Method(ICharacterSource returnType, ICharacterSource name) : base(name) { ReturnType = returnType; }
public Token(TokenType type, ICharacterSource value) { Type = type; Value = value; }