-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataConverter.cs
84 lines (67 loc) · 3.12 KB
/
DataConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;
using Azure.AI.FormRecognizer.DocumentAnalysis;
namespace OcrProject;
public class DataConverter
{
private static readonly string OutputPath = "/Users/toponaute/Documents/GitHub/OcrProject/outputs";
public static InvoiceFields Convert(AnalyzedDocument document)
{
var data = new InvoiceFields();
data.VendorName = SetValue(document.Fields, nameof(InvoiceFields.VendorName));
data.CustomerName = SetValue(document.Fields, nameof(InvoiceFields.CustomerName));
document.Fields.TryGetValue("Items", out var itemsField);
if (itemsField is { FieldType: DocumentFieldType.List })
foreach (var itemField in itemsField.Value.AsList())
{
if (itemField.FieldType != DocumentFieldType.Dictionary) continue;
var itemFields = itemField.Value.AsDictionary();
data.Items.Add(new Items
{
Description = SetValue(itemFields, nameof(Items.Description)),
Amount = SetCurrency(itemFields, nameof(Items.Amount)),
UnitPrice = SetCurrency(itemFields, nameof(Items.UnitPrice)),
TotalPrice = SetCurrency(itemFields, nameof(Items.TotalPrice))
});
}
data.InvoiceTotal = SetCurrency(document.Fields, nameof(InvoiceFields.InvoiceTotal));
data.Subtotal = SetCurrency(document.Fields, nameof(InvoiceFields.Subtotal));
data.TotalTax = SetCurrency(document.Fields, nameof(InvoiceFields.TotalTax));
data.InvoiceTotal = SetCurrency(document.Fields, nameof(InvoiceFields.InvoiceTotal));
return data;
}
private static string SetValue(IReadOnlyDictionary<string, DocumentField> fields, string key)
{
fields.TryGetValue(key, out var documentField);
return documentField is { FieldType : DocumentFieldType.String }
? documentField.Value.AsString()
: string.Empty;
}
private static Currency? SetCurrency(IReadOnlyDictionary<string, DocumentField> fields, string key)
{
fields.TryGetValue(key, out var documentField);
if (documentField is not { FieldType : DocumentFieldType.Currency }) return null;
var currency = documentField.Value.AsCurrency();
return new Currency
{
CurrencyValue = $"{currency.Symbol}{currency.Amount} {currency.Code}"
};
}
public static async Task WriteJsonFile(string fileName, IReadOnlyList<InvoiceFields> data)
{
var options = new JsonSerializerOptions
{
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Converters = { new CurrencyConverter() }
};
for(var i = 0; i < data.Count; i++)
{
var jsonString = JsonSerializer.Serialize(data, options);
var outputJsonPath = $"{OutputPath}/{fileName}_{i}.json";
await File.WriteAllTextAsync(outputJsonPath, jsonString);
Console.WriteLine(outputJsonPath);
}
}
}