Document Intelligence | Contract Model | C# SDK

Avni Bhatt 120 Reputation points
2023-12-02T04:36:55.7233333+00:00

Hi,

I am trying to use this Pre-built Contract Model in C# SDK. It basically extracts:

  • Title - TEXT
  • ContractId - TEXT
  • Parties - LIST
  • ExecutionDate - TEXT
  • ExpirationDate - TEXT
  • RenewalDate - TEXT
  • Jurisdictions - TEXT

On the AI studio, it works fine, I can see the results in JSON too. When i try to embed the code in C# SDK, I get the results for all text type fields. However, For "Parties", its a list, so I am not able to find an easy way to find it.

I tried below code that works to get other fields:

foreach(AnalyzedDocument doc in result.Documents)

{

System.Collections.Generic.IReadOnlyDictionary<string, DocumentField> Fields = doc.Fields;

Fields ["EffectiveDate"].Content;

}

However, as the values corresponding to "Parties" is a List, its not available via Content directly.

Upon placing debugger, its found at below node which is very much nested.

How do I access it on code?

User's image

Azure AI Document Intelligence
Azure AI Document Intelligence
An Azure service that turns documents into usable data. Previously known as Azure Form Recognizer.
2,116 questions
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Joao Baptista 90 Reputation points
    2023-12-02T06:42:56.5466667+00:00

    Hello @Avni Bhatt

    It looks like you need to interact with this as an index page when the field is a collection

    you can follow this Example

    Example: the following should result in PageIndex = 3, LineIndex = 7, WordIndex = 12 "#/analyzeResult/readResults/3/lines/7/words/12" from DocumentResult "#/readResults/3/lines/7/words/12" from PageResult

    Maybe, you can try something like this:

    foreach (AnalyzedDocument doc in result.Documents)
    {
        IReadOnlyDictionary<string, DocumentField> fields = doc.Fields;
    
        string effectiveDate = fields["EffectiveDate"].Content;
    
        if (fields.TryGetValue("Parties", out DocumentField partiesField) && partiesField.ValueType == DocumentFieldType.List)
        {
            var partiesList = new List<string>();
            foreach (var party in partiesField.AsList())
            {
    
                if (party.ValueType == DocumentFieldType.String)
                {
                    partiesList.Add(party.AsString());
                }
            }
    
            // Now partiesList contains all the parties from the "Parties" field
            // You can process this...
    		// And I don't know if parties is a sting list, but I'm assuming it is 😊
    
    
        }
    }
    
    

    I hope this can help you 😊

    Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.

    2 people found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Avni Bhatt 120 Reputation points
    2023-12-03T13:32:48.7+00:00

    Hi João Ricardo Rodrigues De La Cruz Baptista ,

    Thanks for the insights. Parties actually is another list of key value pair but your answer helped in right direction. I just added this in the code that you helped with.

    Thanks again!

     if (Fields.TryGetValue("Parties", out DocumentField partiesField) && partiesField.FieldType == DocumentFieldType.List)
     {
         
         var partiesList = partiesField.Value.AsList();
         foreach (var parties in partiesList)
         {
             var myDictionary1 = parties.Value.AsDictionary();
             foreach (string str1 in myDictionary1.Keys)
             {
                 Azure.AI.FormRecognizer.DocumentAnalysis.DocumentField correspondingValue1 = myDictionary1[str1];
                 Console.WriteLine("Key: " + str1 + ", Value: " + correspondingValue1.Content);
             }
         }
    }
    
    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.