Esempio n. 1
0
        public async Task <IActionResult> GetDocumentStatus(string mortgageApplicationId, string documentId, CancellationToken cancellationToken)
        {
            var blob = await StorageHelper.GetBlob(_storageAccountName, "rawdocuments", $"{mortgageApplicationId.ToLower()}/{documentId}");

            if (!(await blob.ExistsAsync()))
            {
                return(NotFound());
            }

            MortgageApplicationDocument doc = new MortgageApplicationDocument();

            doc.PopuplateFromBlobProperties(blob);

            var finalBlob = await StorageHelper.GetBlob(_storageAccountName, "parseddocuments", $"final/{mortgageApplicationId.ToLower()}/{documentId}");

            if (await finalBlob.ExistsAsync())
            {
                doc.Status           = MortgageApplicationStatus.Processed;
                doc.HasParsedResults = true;
            }

            var markedupBlob = await StorageHelper.GetBlob(_storageAccountName, "parseddocuments", $"markedup/{mortgageApplicationId.ToLower()}/{documentId}");

            if (await markedupBlob.ExistsAsync())
            {
                doc.HasMarkedupResults = true;
            }

            return(Ok(doc));
        }
Esempio n. 2
0
        public static async Task Run([BlobTrigger("rawdocuments/{name}", Connection = "Documents")] CloudBlockBlob fileBlob,
                                     [Blob("parseddocuments", Connection = "Documents")] CloudBlobContainer parsedDirectory,
                                     ILogger log, Microsoft.Azure.WebJobs.ExecutionContext context,
                                     CancellationToken cancellationToken)
        {
            try
            {
                if (fileBlob.Metadata.ContainsKey("DocumentType"))
                {
                    //skip already processed documents
                    return;
                }

                var config = new ConfigurationBuilder()
                             .SetBasePath(context.FunctionAppDirectory)
                             .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                             .AddEnvironmentVariables()
                             .Build();

                string predictionKey                 = config["PredictionKey"];
                string predictionProjectId           = config["PredictionProjectId"];
                string cognativeKey                  = config["CognativeKey"];
                double minimumPrecision              = double.Parse(config["MinimalPrecision"]);
                string computerVisionServiceHostName = config["ComputerVisionServiceHostName"];
                string customVisionServiceHostName   = config["CustomVisionServiceHostName"];

                string documentURLWithSAS = GetBlobSasUri(fileBlob);
                string tagName            = await AnalyseImageAsync(documentURLWithSAS, predictionKey, predictionProjectId, minimumPrecision, customVisionServiceHostName, cancellationToken);

                if (!tagName.IsEmpty())
                {
                    MortgageApplicationDocument mortgageApplicationDocument = new MortgageApplicationDocument();
                    mortgageApplicationDocument.PopuplateFromBlobProperties(fileBlob);
                    mortgageApplicationDocument.DateParsed = DateTimeOffset.UtcNow;

                    var parsedResults = await ParseDocumentTextAsync(documentURLWithSAS, cognativeKey, computerVisionServiceHostName, mortgageApplicationDocument.FileName, cancellationToken);

                    string parsedFileName = $"{tagName}/{fileBlob.Name.Trim('/')}";

                    CloudBlockBlob blob = parsedDirectory.GetBlockBlobReference(parsedFileName);
                    mortgageApplicationDocument.SetBlobProperties(blob);
                    blob.Properties.ContentType = "application/json";

                    await blob.UploadTextAsync(JsonConvert.SerializeObject(parsedResults));
                }
                else
                {
                    tagName = "N/A";
                }

                fileBlob.Metadata.Add("DocumentType", tagName);
                await fileBlob.SetMetadataAsync();
            }
            catch (Exception ex)
            {
                log.LogError($"Identity Document failed: {ex.ToString()}");
            }
        }
Esempio n. 3
0
        public static async Task Run([BlobTrigger("parseddocuments/w2/{name}", Connection = "Documents")] CloudBlockBlob parsedBlob,
                                     [Blob("parseddocuments", Connection = "Documents")] CloudBlobContainer blobDirectory,
                                     [Blob("rawdocuments/{name}", FileAccess.Read)] Stream originalImage,
                                     [Blob("parseddocuments/markedup/{name}", FileAccess.ReadWrite)] CloudBlockBlob markedUpImage,
                                     ILogger log, Microsoft.Azure.WebJobs.ExecutionContext context, CancellationToken cancellationToken)
        {
            try
            {
                MortgageApplicationDocument mortgageApplicationDocument = new MortgageApplicationDocument();
                mortgageApplicationDocument.PopuplateFromBlobProperties(parsedBlob);
                mortgageApplicationDocument.HasParsedResults = true;
                mortgageApplicationDocument.DocumentType     = "w2";

                string jsonContents = await parsedBlob.DownloadTextAsync();

                ProcessedDocument document = JsonConvert.DeserializeObject <ProcessedDocument>(jsonContents);

                CloudBlockBlob finalBlob = blobDirectory.GetBlockBlobReference(parsedBlob.Name.Replace("w2/", "final/"));
                mortgageApplicationDocument.Status        = MortgageApplicationStatus.Processed;
                mortgageApplicationDocument.DateProcessed = DateTimeOffset.UtcNow;
                mortgageApplicationDocument.SetBlobProperties(finalBlob);

                FindDataLine(document, "SSN", "Social Security Number", mortgageApplicationDocument, "a. Employee's social security number", "social security number", "Employee's soc. sec. number", "Employee's social security number");
                FindMoneyLine(document, "Wages", "Total Wages", mortgageApplicationDocument, "Wages");
                FindMoneyLine(document, "FedTax", "Federal Tax Witholding", mortgageApplicationDocument, "2. Federal income tax", "Federal income tax withheld");
                FindDataLine(document, "CopyType", "Copy Type", mortgageApplicationDocument, "Copy");
                FindMoneyLine(document, "SocialSecurityWage", "Social Security Wage", mortgageApplicationDocument, "Social security wages");
                FindMoneyLine(document, "SocialSecurityTax", "Social Security Tax", mortgageApplicationDocument, "Social security tax withheld");
                FindMoneyLine(document, "MedicareWage", "Medicare Wage", mortgageApplicationDocument, "Medicare wages and tips");
                FindMoneyLine(document, "MedicareTax", "Medicare Tax", mortgageApplicationDocument, "Medicare tax withheld");
                FindMoneyLine(document, "SocialSecurityTips", "Social Security Tips", mortgageApplicationDocument, "Social security tips");
                FindDataLine(document, "EmployerDetails", "Employer Details", mortgageApplicationDocument, "Employer's name, address, and ZIP code");
                FindDataLine(document, "EmployeeDetails", "Employee Details", mortgageApplicationDocument, "Employee's first name and initial", "Last name", "Employee's name, address, and ZIP code", "Employee's name (first, middle indial, last)");

                await finalBlob.UploadTextAsync(JsonConvert.SerializeObject(mortgageApplicationDocument));

                await MarkupService.HighlightHits(document, originalImage, markedUpImage);
            }
            catch (Exception ex)
            {
                log.LogError(ex, "W2 Processor Failed");
            }
        }
Esempio n. 4
0
        public async Task <ActionResult <UploadDocumentResult> > UploadDocument(IFormFile file, string mortgageApplicationId, CancellationToken cancellationToken)
        {
            UploadDocumentResult result = new UploadDocumentResult();

            if (mortgageApplicationId.IsEmpty())
            {
                result.Message = "Mortgage Application Id not specified";
                return(result);
            }

            if (file == null || file.FileName.IsEmpty())
            {
                result.Message = "No Files Uploaded";
                return(result);
            }

            MortgageApplicationDocument fileResult = new MortgageApplicationDocument();

            fileResult.FileName              = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
            fileResult.DocumentId            = Guid.NewGuid().ToString("n");
            fileResult.MortgageApplicationId = mortgageApplicationId;
            fileResult.DateUploaded          = DateTimeOffset.UtcNow;
            var blob = await StorageHelper.GetBlob(_storageAccountName, "rawdocuments", $"{mortgageApplicationId.ToLower()}/{fileResult.DocumentId}");

            blob.Metadata.Add("MortgageApplicationId", mortgageApplicationId);
            blob.Metadata.Add("FileName", fileResult.FileName);
            blob.Metadata.Add("CreateTime", fileResult.DateUploaded.ToString());
            blob.Metadata.Add("DocumentId", fileResult.DocumentId);
            blob.Properties.ContentType = file.ContentType;

            using (var fileStream = file.OpenReadStream())
            {
                await blob.UploadFromStreamAsync(fileStream, null, _retryPolicy, null, cancellationToken);

                result.FilesUploaded.Add(fileResult);
            }

            result.Succeeded = true;
            return(result);
        }
        public async Task <ActionResult <MortgageApplication> > GetMortgageApplication(string mortgageApplicationId, CancellationToken cancellationToken)
        {
            var blobs = await StorageHelper.GetBlobs(_storageAccountName, "rawdocuments", $"{mortgageApplicationId.ToLower()}/");

            MortgageApplication mortgageApplication = new MortgageApplication()
            {
                MortgageApplicationId = mortgageApplicationId, Documents = new List <MortgageApplicationDocument>()
            };

            if (blobs.SafeAny())
            {
                foreach (var blob in blobs)
                {
                    MortgageApplicationDocument doc = new MortgageApplicationDocument();
                    doc.PopuplateFromBlobProperties(blob);
                    string         documentId = doc.DocumentId;
                    CloudBlockBlob finalBlob  = await StorageHelper.GetBlob(_storageAccountName, "parseddocuments", $"final/{doc.MortgageApplicationId}/{documentId}");

                    MortgageApplicationDocument finalDoc = Newtonsoft.Json.JsonConvert.DeserializeObject <MortgageApplicationDocument>(await finalBlob.DownloadTextAsync());
                    mortgageApplication.Documents.Add(finalDoc);

                    var markedupBlob = await StorageHelper.GetBlob(_storageAccountName, "parseddocuments", $"markedup/{mortgageApplicationId.ToLower()}/{documentId}");

                    finalDoc.HasParsedResults = true;
                    if (await markedupBlob.ExistsAsync())
                    {
                        finalDoc.HasMarkedupResults = true;
                    }
                }
            }
            else
            {
                return(NotFound());
            }

            return(Ok(mortgageApplication));
        }
Esempio n. 6
0
        private static DataField FindDataLine(ProcessedDocument document, string name, string title, MortgageApplicationDocument mortgageApplication, params string[] titlesToFind)
        {
            ProcessedLine foundLine = null;

            foreach (var titleToFind in titlesToFind)
            {
                string cleanTitle = CLEANUP.Replace(titleToFind, "");
                foundLine = document.Lines.Where(l => CLEANUP.Replace(l.Text, "").IndexOf(cleanTitle, StringComparison.OrdinalIgnoreCase) > -1).FirstOrDefault();

                if (foundLine != null)
                {
                    break;
                }
            }

            var dataLine = foundLine?.BoundingBox.FindClosestsBelow(document.Lines) ?? null;

            if (dataLine == null)
            {
                return(null);
            }

            DataField dataField = new DataField()
            {
                FieldName  = name,
                FieldTitle = title,
                LabelBox   = foundLine.BoundingBox,
                ValueBox   = dataLine.BoundingBox,
                Value      = dataLine.Text
            };

            if (mortgageApplication.DataFields == null)
            {
                mortgageApplication.DataFields = new List <DataField>();
            }

            mortgageApplication.DataFields.Add(dataField);

            return(dataField);
        }
Esempio n. 7
0
        private static DataField FindMoneyLine(ProcessedDocument document, string name, string title, MortgageApplicationDocument mortgageApplication, params string[] titlesToFind)
        {
            DataField dataField = FindDataLine(document, name, title, mortgageApplication, titlesToFind);

            if (dataField != null)
            {
                double tmpVal;
                if (double.TryParse(CLEANUPMONEY.Replace(dataField.Value, ""), out tmpVal))
                {
                    dataField.Value = tmpVal.ToString("c");
                }
            }

            return(dataField);
        }