예제 #1
0
        public static IActionResult RunCryptonymLinkerForLists([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, TraceWriter log, ExecutionContext executionContext)
        {
            string skillName = executionContext.FunctionName;
            IEnumerable <WebApiRequestRecord> requestRecords = WebApiSkillHelpers.GetRequestRecords(req);

            if (requestRecords == null)
            {
                return(new BadRequestObjectResult($"{skillName} - Invalid request record array."));
            }

            CryptonymLinker     cryptonymLinker = new CryptonymLinker(executionContext.FunctionAppDirectory);
            WebApiSkillResponse response        = WebApiSkillHelpers.ProcessRequestRecords(skillName, requestRecords,
                                                                                           (inRecord, outRecord) => {
                var words   = JsonConvert.DeserializeObject <JArray>(JsonConvert.SerializeObject(inRecord.Data["words"]));
                var cryptos = words.Select(jword =>
                {
                    var word = jword.Value <string>();
                    if (word.All(Char.IsUpper) && cryptonymLinker.Cryptonyms.TryGetValue(word, out string description))
                    {
                        return(new { value = word, description });
                    }
                    return(null);
                });

                outRecord.Data["cryptonyms"] = cryptos.ToArray();
                return(outRecord);
            });

            return((ActionResult) new OkObjectResult(response));
        }
예제 #2
0
        public static async Task <IActionResult> RunImageStore([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, TraceWriter log, ExecutionContext executionContext)
        {
            string skillName = executionContext.FunctionName;
            IEnumerable <WebApiRequestRecord> requestRecords = WebApiSkillHelpers.GetRequestRecords(req);

            if (requestRecords == null || requestRecords.Count() != 1)
            {
                return(new BadRequestObjectResult($"{skillName} - Invalid request record array: Skill requires exactly 1 image per request."));
            }

            string blobStorageConnectionString = GetAppSetting("BlobStorageAccountConnectionString");
            string blobContainerName           = String.IsNullOrEmpty(req.Headers["BlobContainerName"]) ? Config.AZURE_STORAGE_CONTAINER_NAME : (string)req.Headers["BlobContainerName"];

            if (String.IsNullOrEmpty(blobStorageConnectionString) || String.IsNullOrEmpty(blobContainerName))
            {
                return(new BadRequestObjectResult($"{skillName} - Information for the blob storage account is missing"));
            }
            ImageStore imageStore = new ImageStore(blobStorageConnectionString, blobContainerName);

            WebApiSkillResponse response = await WebApiSkillHelpers.ProcessRequestRecordsAsync(skillName, requestRecords,
                                                                                               async (inRecord, outRecord) => {
                string imageData = inRecord.Data["imageData"] as string;
                string imageUri  = await imageStore.UploadToBlob(imageData, Guid.NewGuid().ToString());
                outRecord.Data["imageStoreUri"] = imageUri;
                return(outRecord);
            });

            return((ActionResult) new OkObjectResult(response));
        }
예제 #3
0
        public static IActionResult RunHocrGenerator([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, TraceWriter log, ExecutionContext executionContext)
        {
            string skillName = executionContext.FunctionName;
            IEnumerable <WebApiRequestRecord> requestRecords = WebApiSkillHelpers.GetRequestRecords(req);

            if (requestRecords == null || requestRecords.Count() != 1)
            {
                return(new BadRequestObjectResult($"{skillName} - Invalid request record array: Skill requires exactly 1 image per request."));
            }

            WebApiSkillResponse response = WebApiSkillHelpers.ProcessRequestRecords(skillName, requestRecords,
                                                                                    (inRecord, outRecord) => {
                List <OcrImageMetadata> imageMetadataList = JsonConvert.DeserializeObject <List <OcrImageMetadata> >(JsonConvert.SerializeObject(inRecord.Data["ocrImageMetadataList"]));
                Dictionary <string, string> annotations   = JsonConvert.DeserializeObject <JArray>(JsonConvert.SerializeObject(inRecord.Data["wordAnnotations"]))
                                                            .Where(o => o.Type != JTokenType.Null)
                                                            .GroupBy(o => o["value"].Value <string>())
                                                            .Select(g => g.First())
                                                            .ToDictionary(o => o["value"].Value <string>(), o => o["description"].Value <string>());

                List <HocrPage> pages = new List <HocrPage>();
                for (int i = 0; i < imageMetadataList.Count; i++)
                {
                    pages.Add(new HocrPage(imageMetadataList[i], i, annotations));
                }
                HocrDocument hocrDocument      = new HocrDocument(pages);
                outRecord.Data["hocrDocument"] = hocrDocument;
                return(outRecord);
            });

            return((ActionResult) new OkObjectResult(response));
        }
        public static IActionResult RunHocrGenerator([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, TraceWriter log, ExecutionContext executionContext)
        {
            string skillName = executionContext.FunctionName;
            IEnumerable <WebApiRequestRecord> requestRecords = WebApiSkillHelpers.GetRequestRecords(req);

            if (requestRecords == null || requestRecords.Count() != 1)
            {
                return(new BadRequestObjectResult($"{skillName} - Invalid request record array: Skill requires exactly 1 image per request."));
            }

            var blobStorageConnectionString = GetAppSetting("BlobStorageAccountConnectionString");
            var blobContainerName           = String.IsNullOrEmpty(req.Headers["BlobContainerName"]) ? Config.AZURE_STORAGE_CONTAINER_NAME : (string)req.Headers["BlobContainerName"];

            if (String.IsNullOrEmpty(blobStorageConnectionString) || String.IsNullOrEmpty(blobContainerName))
            {
                return(new BadRequestObjectResult($"{skillName} - Information for the blob storage account is missing"));
            }
            var imageStore   = new ImageStore(blobStorageConnectionString, blobContainerName);
            var expiry       = DateTimeOffset.UtcNow.Add(s_imageBlobSASDuration);
            var imageBlobSAS = imageStore.GetSharedAccessSignature(expiry);

            WebApiSkillResponse response = WebApiSkillHelpers.ProcessRequestRecords(skillName, requestRecords,
                                                                                    (inRecord, outRecord) => {
                List <OcrImageMetadata> imageMetadataList = JsonConvert.DeserializeObject <List <OcrImageMetadata> >(JsonConvert.SerializeObject(inRecord.Data["ocrImageMetadataList"]));
                Dictionary <string, string> annotations   = JsonConvert.DeserializeObject <JArray>(JsonConvert.SerializeObject(inRecord.Data["wordAnnotations"]))
                                                            .Where(o => o.Type != JTokenType.Null)
                                                            .GroupBy(o => o["value"].Value <string>())
                                                            .Select(g => g.First())
                                                            .ToDictionary(o => o["value"].Value <string>(), o => o["description"].Value <string>());

                List <HocrPage> pages = new List <HocrPage>();
                for (int i = 0; i < imageMetadataList.Count; i++)
                {
                    pages.Add(new HocrPage(imageMetadataList[i], i, imageBlobSAS, annotations));
                }
                HocrDocument hocrDocument      = new HocrDocument(pages);
                outRecord.Data["hocrDocument"] = hocrDocument;
                return(outRecord);
            });

            return((ActionResult) new OkObjectResult(response));
        }
예제 #5
0
        public static IActionResult RunCryptonymLinker([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, TraceWriter log, ExecutionContext executionContext)
        {
            string skillName = executionContext.FunctionName;
            IEnumerable <WebApiRequestRecord> requestRecords = WebApiSkillHelpers.GetRequestRecords(req);

            if (requestRecords == null)
            {
                return(new BadRequestObjectResult($"{skillName} - Invalid request record array."));
            }

            CryptonymLinker     cryptonymLinker = new CryptonymLinker(executionContext.FunctionAppDirectory);
            WebApiSkillResponse response        = WebApiSkillHelpers.ProcessRequestRecords(skillName, requestRecords,
                                                                                           (inRecord, outRecord) => {
                string word = inRecord.Data["word"] as string;
                if (word.All(Char.IsUpper) && cryptonymLinker.Cryptonyms.TryGetValue(word, out string description))
                {
                    outRecord.Data["cryptonym"] = new { value = word, description };
                }
                return(outRecord);
            });

            return((ActionResult) new OkObjectResult(response));
        }