ふるすたっくえんじにあの日記

ASP .NET MVC (C#)、.NET Framework、iOS (Objective-c) アプリ、Androidアプリ (Java)、AWSあたり

【C#】【Google Cloud Vision API】拡張

何に使うのかはわからない。
とりあえずSelectManyの繰り返しがひどかったので用意してみた。
何に使うのかはわからない。
ぶっちゃけTextAnnotationの仕様がよくわかってない。。
TextAnnotation.Text.Split('\n')とBlocks.Textが一致してほしいんだけどなぁ。。



using Google.Cloud.Vision.V1;
using System.Linq;

public static class CloudVisionEx
{
    // TextAnnotation → Page → Block → Paragraph → Word → Symbol → Text

    public static Block[] Blocks(this TextAnnotation textAnnotation) => textAnnotation.Pages.SelectMany(x => x.Blocks).ToArray();

    public static Paragraph[] Paragraphs(this TextAnnotation textAnnotation) => textAnnotation.Pages.SelectMany(x => x.Paragraphs()).ToArray();

    public static Paragraph[] Paragraphs(this Page page) => page.Blocks.SelectMany(x => x.Paragraphs).ToArray();

    public static Word[] Words(this TextAnnotation textAnnotation) => textAnnotation.Pages.SelectMany(x => x.Words()).ToArray();

    public static Word[] Words(this Page page) => page.Blocks.SelectMany(x => x.Words()).ToArray();

    public static Word[] Words(this Block block) => block.Paragraphs.SelectMany(x => x.Words).ToArray();

    public static Symbol[] Symbols(this TextAnnotation textAnnotation) => textAnnotation.Pages.SelectMany(x => x.Symbols()).ToArray();

    public static Symbol[] Symbols(this Page page) => page.Blocks.SelectMany(x => x.Symbols()).ToArray();

    public static Symbol[] Symbols(this Block block) => block.Paragraphs.SelectMany(x => x.Symbols()).ToArray();

    public static Symbol[] Symbols(this Paragraph paragraph) => paragraph.Words.SelectMany(x => x.Symbols).ToArray();

    public static string[] Text(this TextAnnotation textAnnotation) => textAnnotation.Pages.SelectMany(x => x.Text()).ToArray();

    public static string[] Text(this Page page) => page.Blocks.SelectMany(x => x.Text()).ToArray();

    public static string[] Text(this Block block) => block.Paragraphs.Select(x => x.Text()).ToArray();

    public static string Text(this Paragraph paragraph) => string.Join(string.Empty, paragraph.Words.Select(x => x.Text()));

    public static string Text(this Word word) => string.Join(string.Empty, word.Symbols.SelectMany(x => x.Text));
}