|
| 1 | +// coding: utf - 8 |
| 2 | +// -------------------------------------------------------------------------- |
| 3 | +// Copyright(c) Microsoft Corporation. All rights reserved. |
| 4 | +// Licensed under the MIT License. See License.txt in the project root for |
| 5 | +// license information. |
| 6 | +// -------------------------------------------------------------------------- |
| 7 | + |
| 8 | +using ConsoleTables; |
| 9 | +using System.Configuration; |
| 10 | +using System.Diagnostics; |
| 11 | + |
| 12 | +namespace Quickstarts |
| 13 | +{ |
| 14 | + internal class Program |
| 15 | + { |
| 16 | + const string GNU_PARAM_SYMBOL = " --"; |
| 17 | + static readonly ConsoleTable helpTable = new(["FunctionName", "Description"]); |
| 18 | + static readonly List<(string, string)> sampleFunctionList = [ |
| 19 | + ("ExtractLayoutAsync", "Sample - Extract the layout of a document"), |
| 20 | + ("AnalyzeWithPrebuiltModel", "Sample - Analyze a document with a prebuilt model"), |
| 21 | + ("ExtractHighResolution", "Sample - Add-on : High resolution extraction"), |
| 22 | + ("ExtractFormula", "Sample - Add-on : Formula extraction"), |
| 23 | + ("ExtractFontProperty", "Sample - Add-on : Font property extraction"), |
| 24 | + ("ExtractBarcodeProperty", "Sample - Add-on : Barcode property extraction"), |
| 25 | + ("ExtractKeyValuePairs", "Sample - Add-on : Key-value pairs extraction"), |
| 26 | + ("DetectLanguage", "Sample - Add-on : Language detection")]; |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Here is the entrance for the Azure Document Intelligence client SDK Samples. |
| 30 | + /// </summary> |
| 31 | + /// <returns></returns> |
| 32 | + static async Task Main() |
| 33 | + { |
| 34 | + Console.WriteLine("Hello, welcome to the Azure Document Intelligence client SDK Sample Codes for .Net!"); |
| 35 | + |
| 36 | + // Read the Azure Document Intelligence endpoint and apikey from app.config. |
| 37 | + // For how to obtain the endpoint and apikey, please see Prerequisites in README.md . |
| 38 | + var docIntelligenceEndPoint = ConfigurationManager.AppSettings["AZURE_DOCUMENT_INTELLIGENCE_ENDPOINT"]; |
| 39 | + var docIntelligenceApiKey = ConfigurationManager.AppSettings["AZURE_DOCUMENT_INTELLIGENCE_KEY"]; |
| 40 | + if (string.IsNullOrWhiteSpace(docIntelligenceEndPoint) || string.IsNullOrWhiteSpace(docIntelligenceApiKey)) |
| 41 | + { |
| 42 | + var exceptionStr = "Missing the Azure Document Intelligence EndPoint and ApiKey."; |
| 43 | + Console.WriteLine(exceptionStr); |
| 44 | + Console.WriteLine("Press the 'Enter' for getting help."); |
| 45 | + Console.ReadLine(); |
| 46 | + Process.Start(new ProcessStartInfo("https://learn.microsoft.com/en-us/azure/ai-services/document-intelligence/quickstarts/get-started-sdks-rest-api?view=doc-intel-4.0.0&pivots=programming-language-csharp#prerequisites") { UseShellExecute = true }); |
| 47 | + throw new ArgumentException(exceptionStr); |
| 48 | + } |
| 49 | + |
| 50 | + Type sampleType = typeof(Samples); |
| 51 | + var sampleInstance = new Samples(docIntelligenceEndPoint!, docIntelligenceApiKey!); |
| 52 | + |
| 53 | + sampleFunctionList.ForEach(f => |
| 54 | + { |
| 55 | + (string funcName, string description) = f; |
| 56 | + helpTable.AddRow(funcName, description); |
| 57 | + }); |
| 58 | + PrintHelpInfo(); |
| 59 | + |
| 60 | + bool running = true; |
| 61 | + while (running) |
| 62 | + { |
| 63 | + Console.WriteLine("Please input the command which you want to execute:"); |
| 64 | + string? commandStr = Console.ReadLine(); |
| 65 | + if (!string.IsNullOrWhiteSpace(commandStr)) |
| 66 | + { |
| 67 | + switch (commandStr.ToLower()) |
| 68 | + { |
| 69 | + case "help": PrintHelpInfo(); break; |
| 70 | + case "cls": Console.Clear(); break; |
| 71 | + case "quit": |
| 72 | + case "exit": running = false; break; |
| 73 | + default: |
| 74 | + var commandArr = commandStr.Split(GNU_PARAM_SYMBOL); |
| 75 | + if (commandArr.Length >= 1) |
| 76 | + { |
| 77 | + var funcName = commandArr[0]; |
| 78 | + var methodInfo = sampleType.GetMethod(funcName); |
| 79 | + if (methodInfo != null) |
| 80 | + { |
| 81 | + Console.WriteLine(); |
| 82 | + Utils.ConsoleImportantWriteLine($"Running function: {commandStr}"); |
| 83 | + |
| 84 | + object? taskResult = null; |
| 85 | + if (commandArr.Length > 1) |
| 86 | + { |
| 87 | + var paramKVP = commandArr[1].Split(" "); |
| 88 | + var paramName = paramKVP[0].ToLower(); |
| 89 | + var paramValue = paramKVP[1].Trim('"').Trim('\''); |
| 90 | + if (paramName == "path") |
| 91 | + { |
| 92 | + taskResult = methodInfo.Invoke(sampleInstance, [paramValue, ""]); |
| 93 | + } |
| 94 | + else if (paramName == "url") |
| 95 | + { |
| 96 | + taskResult = methodInfo.Invoke(sampleInstance, ["", paramValue]); |
| 97 | + } |
| 98 | + else |
| 99 | + { |
| 100 | + Console.WriteLine("No corresponding parameter, please check it!"); |
| 101 | + PrintHelpInfo(); |
| 102 | + } |
| 103 | + } |
| 104 | + else |
| 105 | + { |
| 106 | + taskResult = methodInfo.Invoke(sampleInstance, ["", ""]); |
| 107 | + } |
| 108 | + |
| 109 | + if (taskResult != null) |
| 110 | + { |
| 111 | + await (Task)taskResult; |
| 112 | + } |
| 113 | + } |
| 114 | + else |
| 115 | + { |
| 116 | + Console.WriteLine("The syntax format is incorrect, please check it!"); |
| 117 | + PrintHelpInfo(); |
| 118 | + } |
| 119 | + } |
| 120 | + else |
| 121 | + { |
| 122 | + Console.WriteLine("No such function"); |
| 123 | + PrintHelpInfo(); |
| 124 | + } |
| 125 | + break; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + Console.WriteLine("Program Exited."); |
| 131 | + } |
| 132 | + |
| 133 | + /// <summary> |
| 134 | + /// Print the sample command list and help information. |
| 135 | + /// </summary> |
| 136 | + static void PrintHelpInfo() |
| 137 | + { |
| 138 | + Console.WriteLine("Here're the demo functions:"); |
| 139 | + Console.ForegroundColor = ConsoleColor.Green; |
| 140 | + Console.WriteLine("--------------------------------------------------------------------------------"); |
| 141 | + helpTable.Write(Format.Minimal); |
| 142 | + Console.WriteLine("--------------------------------------------------------------------------------"); |
| 143 | + Console.ResetColor(); |
| 144 | + Console.WriteLine("If you want to run the demo function with the sample test data, just input the function name directly."); |
| 145 | + Console.WriteLine("e.g."); |
| 146 | + Utils.ConsoleImportantWriteLine("ExtractLayoutAsync"); |
| 147 | + Console.WriteLine(); |
| 148 | + Console.WriteLine("If you want to execute the function with your customized data, input the function name with the parameter by GNU style, as below:"); |
| 149 | + Utils.ConsoleImportantWriteLine("--path the file path to analyze in you local file system."); |
| 150 | + Utils.ConsoleImportantWriteLine("--url the file url which you expect to analyze online."); |
| 151 | + Console.WriteLine("e.g."); |
| 152 | + Utils.ConsoleImportantWriteLine("ExtractLayoutAsync --path <The file path to analyze in you local file system>"); |
| 153 | + Console.WriteLine("or"); |
| 154 | + Utils.ConsoleImportantWriteLine("ExtractLayoutAsync --url <The file url which you expect to analyze online>"); |
| 155 | + Console.WriteLine("--------------------------------------------------------------------------------"); |
| 156 | + Console.WriteLine(); |
| 157 | + Console.WriteLine("Here're the console commands:"); |
| 158 | + Console.WriteLine("================================================================================"); |
| 159 | + Utils.ConsoleImportantWriteLine("help ------------ Print the command list"); |
| 160 | + Utils.ConsoleImportantWriteLine("cls ------------ Clear the console output"); |
| 161 | + Utils.ConsoleImportantWriteLine("exit | quit ------------ Exit program"); |
| 162 | + Console.WriteLine("================================================================================"); |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments