public static List <AssetData> RetrieveAssetData(string blockId, List <Block> blocks) { //Create a new TableToolbox with the blocks returned by AWS Textract TableToolbox tableToolbox = new TableToolbox(blocks); //Filter the Toolbox to only remember the blocks that are descendants of the Table tableToolbox.FilterToChildren(blockId); //Build the lookup dictionary for the relevant blocks tableToolbox.BuildBlockLookup(); //Build an [x,y] 2-Dimensional array of the Table's Cells tableToolbox.ConstructTable(blockId); return(FindAssetsInTable(tableToolbox)); }
private static List <AssetData> FindAssetsInTable(TableToolbox tableToolbox) { //Navigate the cells of the table to find column indexes for the information that must be located by matching a header string int[] assetIndex = tableToolbox.FindIndex(new List <string>() { "asset", "holding", "assets", "holdings" }); int[] quantityIndex = tableToolbox.FindIndex(new List <string>() { "quantity", "amount", "#", "quan" }); //If there is not an index for assets (assetColumnIndex has -1 as x coordinate), return empty list; if (assetIndex[0] == -1) { return(new List <AssetData>()); } //Else, iterate through each row in the table and add assets to the list List <AssetData> assets = new List <AssetData>(); for (int row = 0; row < tableToolbox.Table.GetLength(1); row++) { //Evaluate if the row contains a valid asset try { //Get all text from the cell at index (x,y) in the assumed quantity column string quantityString = tableToolbox.GetAllTextFromCell(quantityIndex[0], row); //Strip quantityString's decimal place (Round down) quantityString = quantityString.Substring(0, quantityString.IndexOf('.')); //Attempt to convert the string to an int var quantity = int.Parse(quantityString); assets.Add(new AssetData( tableToolbox.GetAllTextFromCell(assetIndex[0], row), quantity)); } catch { // ignored exceptions, catch will assume failures to execute above code as indication of a non-valid asset } } return(assets); }