| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Text.RegularExpressions; |
| | 5 | |
|
| | 6 | | namespace Microsoft.Azure.CognitiveServices.FormRecognizer.Models |
| | 7 | | { |
| | 8 | | using Newtonsoft.Json; |
| | 9 | | using System.Collections; |
| | 10 | | using System.Collections.Generic; |
| | 11 | | using System.Linq; |
| | 12 | |
|
| | 13 | | public partial class ElementReference { |
| | 14 | | private string _resolvedRefProperty = null; |
| | 15 | | private int _pageIndex; |
| | 16 | | private int _lineIndex; |
| | 17 | | private int _wordIndex; |
| | 18 | |
|
| | 19 | | /// <summary> |
| | 20 | | /// Number of the page containing the element. |
| | 21 | | /// </summary> |
| 0 | 22 | | public int Page { get { Resolve(); return _pageIndex + 1; } } |
| | 23 | |
|
| 0 | 24 | | /// <summary> |
| | 25 | | /// Index of the page containing the element (0-indexed). |
| | 26 | | /// </summary> |
| 0 | 27 | | public int PageIndex { get { Resolve(); return _pageIndex; } } |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// Index of the line within the page containing the element (0-indexed). |
| | 31 | | /// </summary> |
| 0 | 32 | | public int LineIndex { get { Resolve(); return _lineIndex; } } |
| | 33 | |
|
| 0 | 34 | | /// <summary> |
| | 35 | | /// Index of the word within the line containing the element (0-indexed). |
| 0 | 36 | | /// </summary> |
| 0 | 37 | | public int WordIndex { get { Resolve(); return _wordIndex; } } |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// Returns the word referenced by the JSON pointer element reference. |
| | 41 | | /// </summary> |
| | 42 | | public Word ResolveWord(ReadReceiptResult result) |
| | 43 | | { |
| | 44 | | try |
| | 45 | | { |
| 0 | 46 | | return result.RecognitionResults[PageIndex].Lines[LineIndex].Words[WordIndex]; |
| | 47 | | } |
| 0 | 48 | | catch (Exception e) |
| | 49 | | { |
| | 50 | | //throw new ArgumentException("Invalid element reference."); |
| 0 | 51 | | throw new ArgumentException("Invalid element reference, {0}", e); |
| | 52 | | } |
| 0 | 53 | | } |
| | 54 | |
|
| | 55 | | private void Resolve() |
| | 56 | | { |
| 0 | 57 | | if (_resolvedRefProperty != RefProperty) |
| | 58 | | { |
| 0 | 59 | | var match = Regex.Match(RefProperty, @"^#/recognitionResults/(\d+)/lines/(\d+)/words/(\d+)$"); |
| 0 | 60 | | if (!match.Success) |
| | 61 | | { |
| 0 | 62 | | throw new ArgumentException("Invalid element reference."); |
| | 63 | | } |
| 0 | 64 | | _pageIndex = int.Parse(match.Groups[1].Value); |
| 0 | 65 | | _lineIndex = int.Parse(match.Groups[2].Value); |
| 0 | 66 | | _wordIndex = int.Parse(match.Groups[3].Value); |
| 0 | 67 | | _resolvedRefProperty = RefProperty; |
| | 68 | | } |
| 0 | 69 | | } |
| | 70 | | } |
| | 71 | | } |