Exemple #1
0
        public static OutputRecord.OutputRecordData AddTopEntityMetadata(WebPage[] entities)
        {
            List <OutputRecord.OutputRecordData.BingCustomSearchEntities> my = new List <OutputRecord.OutputRecordData.BingCustomSearchEntities>();

            OutputRecord.OutputRecordData test = new OutputRecord.OutputRecordData();

            if (entities != null)
            {
                foreach (WebPage entity in entities)
                {
                    OutputRecord.OutputRecordData.BingCustomSearchEntities ex = new OutputRecord.OutputRecordData.BingCustomSearchEntities();

                    ex.snippet    = entity.snippet;
                    ex.displayUrl = entity.displayUrl;
                    ex.name       = entity.name;

                    my.Add(ex);


                    //my.DataEntities.Add(ex);
                    // return rootObject;
                }
            }
            test.DataEntities = my;
            return(test);
        }
Exemple #2
0
        /// <summary>
        /// Use Azure Maps to find location of an address
        /// </summary>
        /// <param name="address">The address to search for.</param>
        /// <returns>Asynchronous task that returns objects identified in the image. </returns>
        async static Task <OutputRecord.OutputRecordData> GetPosition(InputRecord.InputRecordData inputRecord)
        {
            var result = new OutputRecord.OutputRecordData();

            var uri = azureMapstUri + "?api-version=1.0&query=" + inputRecord.Address;

            try
            {
                using (var client = new HttpClient())
                    using (var request = new HttpRequestMessage())
                    {
                        request.Method     = HttpMethod.Get;
                        request.RequestUri = new Uri(uri);
                        request.Headers.Add("X-ms-client-id", azureMapsKey);

                        var response = await client.SendAsync(request);

                        var responseBody = await response.Content.ReadAsStringAsync();

                        var data = JsonConvert.DeserializeObject <OutputRecord.OutputRecordData>(responseBody);

                        result = data;
                    }
            }
            catch
            {
                result = new OutputRecord.OutputRecordData();
            }

            return(result);
        }
Exemple #3
0
        /// <summary>
        /// Replace this method with a method that enriches or transforms the data in
        /// a meaningful manner
        /// </summary>
        /// <param name="inputField">Replace this with any fields you need to process.</param>
        /// <returns>Feel free to change the return type to meet your needs. </returns>
        private static async Task <OutputRecord.OutputRecordData> DoWork(InputRecord.InputRecordData inputRecord)
        {
            var outputRecord = new OutputRecord.OutputRecordData();

            // Parse MyInputField anc check if it contains anything in Dictionary
            // If no don't add to MyOutputField
            // separator could be space or , or ;
            //right now, facet is not available so look in the dictionary key for 'Organizations'

            string facetInput          = "Organizations";
            string filterTermsForFacet = FacetFilterLookup[facetInput];

            //string[] inputwords = inputRecord.MyInputField.Split(delimiterChars);
            string[]      filterTerms          = filterTermsForFacet.Split(",");
            StringBuilder sb                   = new StringBuilder();
            string        cleansedMyInputField = inputRecord.MyInputField;

            foreach (var filter in filterTerms)
            {
                // Remove all filter in inputRecord.MyInputField and then assign to outputRecord.MyOutputField
                if (cleansedMyInputField.Contains(filter))
                {
                    cleansedMyInputField = cleansedMyInputField.Replace(filter, "");
                }
            }

            outputRecord.MyOutputField = cleansedMyInputField;

            return(outputRecord);
        }
        private async static Task <OutputRecord.OutputRecordData> GetEntityMetadata(string title)
        {
            var uri    = springerapiendpoint + "?q=title:\"" + title + "\"&api_key=" + apikey;
            var result = new OutputRecord.OutputRecordData();

            using (var client = new HttpClient())
                using (var request = new HttpRequestMessage {
                    Method = HttpMethod.Get,
                    RequestUri = new Uri(uri)
                })
                {
                    HttpResponseMessage response = await client.SendAsync(request);

                    string responseBody = await response?.Content?.ReadAsStringAsync();

                    JObject        springerresults = JObject.Parse(responseBody);
                    IList <JToken> parsedresults   = springerresults["records"].Children().ToList();

                    foreach (JToken t in parsedresults)
                    {
                        result.DOI             = t.Value <string>("doi");
                        result.PublicationDate = t.Value <string>("publicationDate");
                        result.PublicationName = t.Value <string>("publicationName");
                        result.Publisher       = t.Value <string>("publisher");
                    }
                }

            return(result);
        }
Exemple #5
0
        /// <summary>
        /// Gets metadata for a particular entity based on its name using Bing Entity Search
        /// </summary>
        /// <param name="nameName">The image to extract objects for.</param>
        /// <returns>Asynchronous task that returns objects identified in the image. </returns>
        async static Task <OutputRecord.OutputRecordData> GetEntityMetadata(string nameName)
        {
            var uri    = bingApiEndpoint + "?q=" + nameName + "&mkt=en-us&count=10&offset=0&safesearch=Moderate";
            var result = new OutputRecord.OutputRecordData();

            using (var client = new HttpClient(new RetryHandler(new HttpClientHandler())))
                using (var request = new HttpRequestMessage())
                {
                    request.Method     = HttpMethod.Get;
                    request.RequestUri = new Uri(uri);
                    request.Headers.Add("Ocp-Apim-Subscription-Key", key);

                    var response = await client.SendAsync(request);

                    var responseBody = await response.Content.ReadAsStringAsync();

                    result = JsonConvert.DeserializeObject <OutputRecord.OutputRecordData>(responseBody);

                    // In addition to the list of entities that could match the name, for simplicity let's return information
                    // for the top match as additional metadata at the root object.
                    result = AddTopEntityMetadata(result);

                    // Do some cleanup on the returned result.
                    result.ImageUrl    = EmptyOrValue(result.ImageUrl);
                    result.Description = EmptyOrValue(result.Description);
                    if (result.Name == null)
                    {
                        result.Name = EmptyOrValue(nameName);
                    }
                    result.Url = EmptyOrValue(result.Url);
                    result.LicenseAttribution = EmptyOrValue(result.LicenseAttribution);
                }

            return(result);
        }
        /// <summary>
        /// Use Cognitive Service to translate text from one language to another.
        /// </summary>
        /// <param name="inputRecord">The input record that contains the original text to translate.</param>
        /// <param name="toLanguage">The language you want to translate to.</param>
        /// <returns>Asynchronous task that returns the translated text. </returns>
        async static Task <OutputRecord.OutputRecordData> TranslateText(InputRecord.InputRecordData inputRecord, string toLanguage)
        {
            string originalText = inputRecord.Text;

            var outputRecord = new OutputRecord.OutputRecordData();

            object[] body        = new object[] { new { Text = originalText } };
            var      requestBody = JsonConvert.SerializeObject(body);

            var uri = $"{path}&to={toLanguage}";

            using (var client = new HttpClient())
                using (var request = new HttpRequestMessage())
                {
                    request.Method     = HttpMethod.Post;
                    request.RequestUri = new Uri(uri);
                    request.Content    = new StringContent(requestBody, Encoding.UTF8, "application/json");
                    request.Headers.Add("Ocp-Apim-Subscription-Key", translatorApiKey);
                    request.Headers.Add("Ocp-Apim-Subscription-Region", translatorRegion);

                    var response = await client.SendAsync(request);

                    var responseBody = await response.Content.ReadAsStringAsync();

                    dynamic data = JsonConvert.DeserializeObject(responseBody);

                    outputRecord.TranslatedText = data?.First?.translations?.First?.text?.Value as string;
                    return(outputRecord);
                }
        }
Exemple #7
0
        private async static Task <OutputRecord.OutputRecordData> AddEnrichedMetadata(AyalonResponse ayalonResponse, string text)
        {
            HashSet <string> entityTypes = new HashSet <string>();
            HashSet <string> concepts    = new HashSet <string>();
            HashSet <string> relations   = new HashSet <string>();
            HashSet <string> gender      = new HashSet <string>();

            int age = 0;

            if (ayalonResponse.Entities != null)
            {
                foreach (AyalonEntity entity in ayalonResponse.Entities)
                {
                    entityTypes.Add(entity.EntityType);
                    if (entity.EntityType == "AGE")
                    {
                        string ageText = text.Substring(entity.StartPosition, entity.EndPosition - entity.StartPosition);
                        age = (await ResolveAgeEntity(ageText)).Age;
                    }
                    if (entity.EntityType == "GENDER")
                    {
                        string genderText = text.Substring(entity.StartPosition, entity.EndPosition - entity.StartPosition);
                        if (genderText.Length > 0)
                        {
                            gender.Add((await ResolveGenderEntity(genderText)).Gender);
                        }
                    }
                    if (entity.Linking != null)
                    {
                        var umlsConcept = entity.Linking.FirstOrDefault(e => { return(e.Source == "UMLS" && e.Concept_Id != null); });
                        if (umlsConcept != null)
                        {
                            concepts.Add("UMLS " + umlsConcept.Concept_Id + " (" + text.Substring(entity.StartPosition, entity.EndPosition - entity.StartPosition) + ")");
                        }
                    }
                }
            }
            if (ayalonResponse.Relations != null)
            {
                foreach (Relation relation in ayalonResponse.Relations)
                {
                    if (relation.RelationType != null)
                    {
                        relations.Add(relation.RelationType);
                    }
                }
            }

            var rootObject = new OutputRecord.OutputRecordData
            {
                entityTypes = entityTypes.ToArray(),
                concepts    = concepts.ToArray(),
                relations   = relations.ToArray(),
                age         = age,
                gender      = gender.ToArray()
            };

            return(rootObject);
        }
        /// <summary>
        /// Replace this method with a method that enriches or transforms the data in
        /// a meaningful manner
        /// </summary>
        /// <param name="inputField">Replace this with any fields you need to process.</param>
        /// <returns>Feel free to change the return type to meet your needs. </returns>
        private static async Task <OutputRecord.OutputRecordData> DoWork(InputRecord.InputRecordData inputRecord)
        {
            var outputRecord = new OutputRecord.OutputRecordData();

            outputRecord.MyOutputField = "Hello " + inputRecord.MyInputField;

            return(outputRecord);
        }
Exemple #9
0
        static OutputRecord.OutputRecordData AddTopEntityMetadata(OutputRecord.OutputRecordData rootObject)
        {
            CoreData coreData = new CoreData();

            if (rootObject.Entities != null)
            {
                foreach (BingEntity entity in rootObject.Entities.value)
                {
                    if (entity.EntityPresentationInfo != null)
                    {
                        if (entity.EntityPresentationInfo.EntityTypeHints != null)
                        {
                            if (entity.EntityPresentationInfo.EntityTypeHints[0] != "Person" &&
                                entity.EntityPresentationInfo.EntityTypeHints[0] != "Organization" &&
                                entity.EntityPresentationInfo.EntityTypeHints[0] != "Location"
                                )
                            {
                                continue;
                            }
                        }
                    }

                    if (entity.Description != null && entity.Description != "")
                    {
                        rootObject.Description = entity.Description;
                        rootObject.Name        = entity.Name;
                        if (entity.Image != null)
                        {
                            rootObject.ImageUrl = entity.Image.ThumbnailUrl;
                        }

                        if (entity.contractualRules != null)
                        {
                            foreach (var rule in entity.contractualRules)
                            {
                                if (rule.TargetPropertyName == "description")
                                {
                                    rootObject.Url = rule.Url;
                                }

                                if (rule._type == "ContractualRules/LicenseAttribution")
                                {
                                    rootObject.LicenseAttribution = rule.LicenseNotice;
                                }
                            }
                        }

                        return(rootObject);
                    }
                }
            }

            return(rootObject);
        }
Exemple #10
0
        /// <summary>
        /// Gets metadata for a particular entity based on its name using Bing Entity Search
        /// </summary>
        /// <param name="entityName">The name of the entity to extract data for.</param>
        /// <returns>Asynchronous task that returns entity data. </returns>
        public async static Task <OutputRecord.OutputRecordData> GetEntityMetadata(string entityName, ILogger log)
        {
            var uri    = bingApiEndpoint + "?q=" + entityName + " Fraud" + "&customconfig=" + customconfig + "&mkt=en-us&count=10&offset=0&safesearch=Moderate";
            var result = new OutputRecord.OutputRecordData();

            using (var client = new HttpClient())
                using (var request = new HttpRequestMessage
                {
                    Method = HttpMethod.Get,
                    RequestUri = new Uri(uri)
                })
                {
                    request.Headers.Add("Ocp-Apim-Subscription-Key", key);

                    HttpResponseMessage responseMsg = await client.SendAsync(request);

                    string responseBody = await responseMsg?.Content?.ReadAsStringAsync();

                    BingCustomSearchResponse bingResult = JsonConvert.DeserializeObject <BingCustomSearchResponse>(responseBody);
                    log.LogInformation("Custom Search function: Response result." + bingResult.ToString());
                    // BingResponse bingResult = JsonConvert.DeserializeObject<BingResponse>(responseBody);
                    if (bingResult != null)
                    {
                        // In addition to the list of entities that could match the name, for simplicity let's return information
                        // for the top match as additional metadata at the root object.
                        // return AddTopEntityMetadata(bingResult.Entities?.Value);

                        for (int i = 0; i < bingResult.webPages.value.Length; i++)

                        {
                            var webPage = bingResult.webPages.value[i];



                            Console.WriteLine("name: " + webPage.name);

                            Console.WriteLine("url: " + webPage.url);

                            Console.WriteLine("displayUrl: " + webPage.displayUrl);

                            Console.WriteLine("snippet: " + webPage.snippet);

                            Console.WriteLine("dateLastCrawled: " + webPage.dateLastCrawled);

                            Console.WriteLine();
                        }

                        return(AddTopEntityMetadata(bingResult.webPages?.value));
                    }
                }

            return(result);
        }
Exemple #11
0
        /// <summary>
        /// Use Cognitive Service to translate text from one language to another.
        /// </summary>
        /// <param name="inputRecord">The input record that contains the original text to translate.</param>
        /// <param name="toLanguage">The language you want to translate to.</param>
        /// <returns>Asynchronous task that returns the translated text. </returns>
        async static Task <OutputRecord.OutputRecordData> AnalyzeForm(InputRecord.InputRecordData inputRecord)
        {
            string base_url = FormsRecognizerEndpoint + @"/formrecognizer/v1.0-preview/custom";
            string fileUrl  = inputRecord.formUrl;
            string sasToken = inputRecord.formSasToken;

            var outputRecord = new OutputRecord.OutputRecordData();

            byte[] bytes = null;

            using (WebClient client = new WebClient())
            {
                // Read the form to analyze
                bytes = client.DownloadData(fileUrl + sasToken);
            }

            using (var client = new HttpClient())
                using (var request = new HttpRequestMessage())
                {
                    var url = base_url + "/models/" + ModelId + "/analyze";

                    request.Method     = HttpMethod.Post;
                    request.RequestUri = new Uri(url);
                    request.Content    = new ByteArrayContent(bytes);
                    request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
                    request.Headers.Add("Ocp-Apim-Subscription-Key", FormsRecognizerKey);

                    var response = await client.SendAsync(request);

                    var responseBody = await response.Content.ReadAsStringAsync();

                    if (response.IsSuccessStatusCode)
                    {
                        dynamic data = JsonConvert.DeserializeObject(responseBody);

                        var result = JsonConvert.DeserializeObject <FormsRecognizerResponse>(responseBody);

                        var addressValue = GetField(result, "Address", 0);
                        var invoiceFor   = GetField(result, "Invoice For", 0);

                        outputRecord.Address   = addressValue;
                        outputRecord.Recipient = invoiceFor;

                        return(outputRecord);
                    }
                    else
                    {
                        throw new SystemException(response.StatusCode.ToString() + ": " + response.ToString() + "\n " + responseBody);
                    }
                }
        }
Exemple #12
0
        private static async Task <Dictionary <string, string> > AnalyzeForm(string modelId,
                                                                             string endPoint, string subscriptionKey, Stream myBlob,
                                                                             Dictionary <string, string> formEntities, string entityTable,
                                                                             string storageConnectionString, string sourceFile, string page, string formType)
        {
            var outputRecord = new OutputRecord.OutputRecordData
            {
                FormEntities = new Dictionary <string, string>()
            };

            byte[] bytes = null;
            bytes = ReadFully(myBlob);

            using (var client = new HttpClient())
                using (var request = new HttpRequestMessage())
                {
                    endPoint           = endPoint.Replace("ModelId", modelId);
                    request.Method     = HttpMethod.Post;
                    request.RequestUri = new Uri(endPoint);
                    request.Content    = new ByteArrayContent(bytes);
                    request.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
                    request.Headers.Add("Ocp-Apim-Subscription-Key", subscriptionKey);

                    var response = await client.SendAsync(request);

                    var responseBody = await response.Content.ReadAsStringAsync();

                    if (response.IsSuccessStatusCode)
                    {
                        dynamic data = JsonConvert.DeserializeObject(responseBody);

                        var result = JsonConvert.DeserializeObject <FormsRecognizerResponse>(responseBody);

                        GetKeyValuePairs(result, formEntities, entityTable,
                                         storageConnectionString, sourceFile, page, formType);
                        formEntities.Add("IsProcessed", "false");
                        return(formEntities);
                    }
                    else
                    {
                        throw new SystemException(response.StatusCode.ToString() + ": " + response.ToString() + "\n " + responseBody);
                    }
                }
        }
Exemple #13
0
        /// <summary>
        /// Use Cognitive Services to find objects in an image
        /// </summary>
        /// <param name="imageUrl">The image to extract objects for.</param>
        /// <returns>Asynchronous task that returns objects identified in the image. </returns>
        async static Task <OutputRecord.OutputRecordData> GetObjects(string imageUrl)
        {
            var requestBody = "{ \"url\": \"" + imageUrl + "\" }";
            var uri         = $"{path}";
            var result      = new OutputRecord.OutputRecordData();

            using (var client = new HttpClient())
                using (var request = new HttpRequestMessage())
                {
                    request.Method     = HttpMethod.Post;
                    request.RequestUri = new Uri(uri);
                    request.Content    = new StringContent(requestBody, Encoding.UTF8, "application/json");
                    request.Headers.Add("Ocp-Apim-Subscription-Key", cognitiveServicesKey);

                    var response = await client.SendAsync(request);

                    var responseBody = await response.Content.ReadAsStringAsync();

                    result = JsonConvert.DeserializeObject <OutputRecord.OutputRecordData>(responseBody);
                }

            return(result);
        }
Exemple #14
0
        /// <summary>
        /// Gets metadata for a particular entity based on its name using Ayalon
        /// </summary>
        /// <param name="entityName">The name of the entity to extract data for.</param>
        /// <returns>Asynchronous task that returns entity data. </returns>
        private async static Task <OutputRecord.OutputRecordData> GetEntityMetadata(InputRecord inputRecord)
        {
            var uri = ayalonEndpoint + "/query";

            var result = new OutputRecord.OutputRecordData();

            using (var client = new HttpClient())
                using (var request = new HttpRequestMessage
                {
                    Method = HttpMethod.Post,
                    RequestUri = new Uri(uri)
                })
                {
                    var serializerSettings = new JsonSerializerSettings();
                    serializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
                    var data = new AyalonInputRecord {
                        Documents = new AyalonDocument[] { new AyalonDocument {
                                                               Id = inputRecord.RecordId, Text = inputRecord.Data.Document
                                                           } }
                    };
                    var json          = JsonConvert.SerializeObject(data, serializerSettings);
                    var stringContent = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
                    request.Content = stringContent;

                    HttpResponseMessage response = await client.SendAsync(request);

                    string responseBody = await response?.Content?.ReadAsStringAsync();

                    AyalonResponse[] ayalonResponses = JsonConvert.DeserializeObject <AyalonResponse[]>(responseBody);
                    if (ayalonResponses != null)
                    {
                        return(AddEnrichedMetadata(ayalonResponses[0], inputRecord.Data.Document).Result);
                    }
                }

            return(result);
        }
        private static async Task <OutputRecord.OutputRecordData> FetchEntityMetadata(string entityName)
        {
            var uriSearch = BingSearchApiEndpoint + "?q=" + entityName + "&mkt=en-us&count=1&answerCount=1&promote=images%2Cvideos";
            var result    = new OutputRecord.OutputRecordData();

            var client  = new HttpClient();
            var request = new HttpRequestMessage();

            request.Method = HttpMethod.Get;
            request.Headers.Add("Ocp-Apim-Subscription-Key", Key);

            request.RequestUri = new Uri(uriSearch);
            HttpResponseMessage responseSearch = await client.SendAsync(request);

            string responseSearchBody = await responseSearch?.Content?.ReadAsStringAsync();

            BingSearchEntity bingSearchEntity = JsonConvert.DeserializeObject <BingSearchEntity>(responseSearchBody);

            if (bingSearchEntity != null)
            {
                return(AddTopEntityData(entityName, bingSearchEntity));
            }
            return(result);
        }
Exemple #16
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("Data Processor skill: C# HTTP trigger function processed a request.");

            // Read input, deserialize it and validate it.
            var data = GetStructuredInput(req.Body);

            if (data == null)
            {
                return(new BadRequestObjectResult("The request schema does not match expected schema."));
            }

            var storageConnectionString = Environment.GetEnvironmentVariable("StorageContainerString");
            var entityTable             = Environment.GetEnvironmentVariable("EntityTableName");
            var cosmosUri       = Environment.GetEnvironmentVariable("CosmosUri");
            var cosmosKey       = Environment.GetEnvironmentVariable("CosmosKey");
            var cosmosDbId      = Environment.GetEnvironmentVariable("CosmosDbId");
            var cosmosContainer = Environment.GetEnvironmentVariable("CosmosContainer");

            var response = new WebApiResponse();

            foreach (var record in data.Values)
            {
                if (record == null || record.RecordId == null)
                {
                    continue;
                }

                OutputRecord responseRecord = new OutputRecord
                {
                    RecordId = record.RecordId
                };

                var rootObject = new OutputRecord.OutputRecordData
                {
                    FormDoc = new List <FormDoc>()
                };

                try
                {
                    log.LogInformation("Data Processor skill: Process record : {0}", record.Data.Url);

                    var storageAccount  = CreateStorageAccountFromConnectionString(storageConnectionString);
                    var sourceBlob      = new CloudBlob(new Uri(record.Data.Url), storageAccount.Credentials);
                    var sourceContainer = sourceBlob.Container.Name;
                    var sourceFilePath  = sourceBlob.Name;
                    var sourceFileName  = sourceFilePath.Replace(sourceBlob.Parent.Prefix, "").Replace(".pdf", "");

                    var json    = string.Empty;
                    var formDoc = await ProcessData(storageConnectionString, entityTable,
                                                    sourceFileName, sourceContainer, log);

                    rootObject.FormDoc = formDoc;

                    json = JsonConvert.SerializeObject(formDoc, Formatting.Indented);
                    log.LogInformation("Json Value : " + json);

                    WriteToCosmos(formDoc, cosmosUri, cosmosDbId,
                                  cosmosContainer, cosmosKey);

                    rootObject.FormDocJson = json;
                    responseRecord.Data    = rootObject;
                }
                catch (Exception e)
                {
                    // Something bad happened, log the issue.
                    var error = new OutputRecord.OutputRecordMessage
                    {
                        Message = e.Message
                    };

                    log.LogInformation("Data Processor skill: C# Exception : {0}", e.Message);

                    responseRecord.Errors = new List <OutputRecord.OutputRecordMessage>
                    {
                        error
                    };
                }
                finally
                {
                    response.values.Add(responseRecord);
                }
            }

            log.LogInformation($"Completed");

            return(new OkObjectResult(response));
        }
Exemple #17
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("Custom skill: C# HTTP trigger function processed a request.");

            // Read input, deserialize it and validate it.
            var data = GetStructuredInput(req.Body);

            if (data == null)
            {
                return(new BadRequestObjectResult("The request schema does not match expected schema."));
            }

            var storageConnectionString = Environment.GetEnvironmentVariable("StorageContainerString");
            var modelTable  = Environment.GetEnvironmentVariable("ModelTableName");
            var entityTable = Environment.GetEnvironmentVariable("EntityTableName");

            // Calculate the response for each value.
            var response = new WebApiResponse();

            foreach (var record in data.Values)
            {
                if (record == null || record.RecordId == null)
                {
                    continue;
                }

                OutputRecord responseRecord = new OutputRecord
                {
                    RecordId = record.RecordId
                };

                var rootObject = new OutputRecord.OutputRecordData
                {
                    FormEntitiesv2 = new Dictionary <string, string>()
                };

                try
                {
                    // Read Azure Table and find all entries where mlmodel = "Form"
                    // Read information about the storage account and storage key from App Settings
                    var storageAccount  = CreateStorageAccountFromConnectionString(storageConnectionString);
                    var sourceBlob      = new CloudBlob(new Uri(record.Data.Url), storageAccount.Credentials);
                    var sourceContainer = sourceBlob.Container.Name;
                    // Since we are storing the file into format "container/formtype/attachmenttype/files"
                    var formType       = sourceBlob.Parent.Parent.Prefix.Replace("/", "");
                    var sourceFilePath = sourceBlob.Name;
                    var sourceFileName = sourceFilePath.Replace(sourceBlob.Parent.Prefix, "").Replace(".pdf", "");

                    log.LogInformation("Form Recognizer Skill function: Url : {0}", record.Data.Url);

                    var sortedModel = GetModelInfo(storageConnectionString, modelTable,
                                                   formType).Result;

                    // Loop through all the results once it's sorted by Page Number
                    foreach (var model in sortedModel)
                    {
                        var    folder = sourceBlob.Parent.Prefix;
                        var    file   = sourceFileName + "_" + model.Page.PadLeft(3, '0') + ".jpg";
                        var    blob   = GetBlobReference(folder + file, sourceContainer, storageConnectionString);
                        Stream myBlob = new MemoryStream();
                        blob.DownloadToStreamAsync(myBlob).Wait();
                        myBlob.Position = 0;

                        var entities = AnalyzeForm(model.ModelId, model.EndPoint,
                                                   model.SubscriptionKey, myBlob, rootObject.FormEntitiesv2, entityTable,
                                                   storageConnectionString, file, model.Page, formType, folder, sourceContainer).Result;
                        log.LogInformation("Form Recognizer Skill : C# HTTP output : {0}", responseRecord.Data);
                    }

                    responseRecord.Data = rootObject;
                }
                catch (Exception e)
                {
                    // Something bad happened, log the issue.
                    var error = new OutputRecord.OutputRecordMessage
                    {
                        Message = e.Message
                    };

                    log.LogInformation("Custom skill: C# Exception : {0}", e.Message);

                    responseRecord.Errors = new List <OutputRecord.OutputRecordMessage>
                    {
                        error
                    };
                }
                finally
                {
                    response.values.Add(responseRecord);
                }
            }

            return(new OkObjectResult(response));
        }
Exemple #18
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("LuisSkill function: C# HTTP trigger function processed a request.");

            var response = new WebApiResponse
            {
                Values = new List <OutputRecord>()
            };

            string requestBody = new StreamReader(req.Body).ReadToEnd();
            var    data        = JsonConvert.DeserializeObject <WebApiRequest>(requestBody);

            // Do some schema validation
            if (data == null)
            {
                return(new BadRequestObjectResult("The request schema does not match expected schema."));
            }
            if (data.Values == null)
            {
                return(new BadRequestObjectResult("The request schema does not match expected schema. Could not find values array."));
            }

            var storageConnectionString = Environment.GetEnvironmentVariable("StorageContainerString");
            var modelTable  = Environment.GetEnvironmentVariable("ModelTableName");
            var entityTable = Environment.GetEnvironmentVariable("EntityTableName");

            log.LogInformation("LuisSkill function: Model Name : {0}", modelTable);

            foreach (var record in data.Values)
            {
                OutputRecord responseRecord = new OutputRecord
                {
                    RecordId = record.RecordId
                };
                // Read Azure Table and find all entries where mlmodel = "luis"
                // Read information about the storage account and storage key from App Settings
                var storageAccount  = CreateStorageAccountFromConnectionString(storageConnectionString);
                var sourceBlob      = new CloudBlob(new Uri(record.Data.Url), storageAccount.Credentials);
                var sourceContainer = sourceBlob.Container.Name;
                var sourceFilePath  = sourceBlob.Name;
                // Since we are storing the file into format "container/formtype/attachmenttype/files"
                var formType       = sourceBlob.Parent.Parent.Prefix.Replace("/", "");
                var sourceFileName = sourceFilePath.Replace(sourceBlob.Parent.Prefix, "").Replace(".pdf", "");

                log.LogInformation("LuisSkill function: Url : {0}", record.Data.Url);

                log.LogInformation("LuisSkill function: Text  : {0}", record.Data.OcrText);

                var sortedModel = GetModelInfo(storageConnectionString, modelTable,
                                               formType, log).Result;

                // Construct object for results
                var rootObject = new OutputRecord.OutputRecordData
                {
                    LuisEntities = new Dictionary <string, string>()
                };

                var duplicatePair = new ListWithDuplicates();

                // Loop through all the results once it's sorted by Page Number
                foreach (var model in sortedModel)
                {
                    var folder = sourceBlob.Parent.Prefix;
                    var file   = sourceFileName + "_" + model.Page.PadLeft(3, '0') + ".jpg";
                    log.LogInformation("LuisSkill function: Model  : {0}, {1}", model.PartitionKey, model.Page);

                    var convertedText = record.Data.OcrText[Convert.ToInt32(model.Page) - 1];
                    if (model.StartIndex > 0 && convertedText.Substring(model.StartIndex).Length > 500)
                    {
                        convertedText = convertedText.Substring(model.StartIndex, 500);
                    }
                    else if (model.StartIndex > 0)
                    {
                        convertedText = convertedText.Substring(model.StartIndex);
                    }

                    if (model.StartIndex == 0 && convertedText.Length > 500)
                    {
                        convertedText = convertedText.Substring(model.StartIndex, 500);
                    }
                    var entities = GetEntities(convertedText, model.ModelId, model.EndPoint,
                                               model.SubscriptionKey, rootObject.LuisEntities, formType, entityTable,
                                               storageConnectionString, file, model.Page, log, duplicatePair).Result;
                }
                responseRecord.Data = rootObject;

                response.Values.Add(responseRecord);
            }

            return((ActionResult) new OkObjectResult(response));
        }
Exemple #19
0
        private static async Task <Dictionary <string, string> > AnalyzeForm(string modelId,
                                                                             string endPoint, string subscriptionKey, Stream myBlob,
                                                                             Dictionary <string, string> formEntities, string entityTable,
                                                                             string storageConnectionString, string sourceFile, string page, string formType,
                                                                             string folder, string sourceContainer)
        {
            var outputRecord = new OutputRecord.OutputRecordData
            {
                FormEntitiesv2 = new Dictionary <string, string>()
            };

            byte[] bytes = null;
            bytes = ReadFully(myBlob);

            using (var client = new HttpClient())
                using (var request = new HttpRequestMessage())
                {
                    endPoint           = endPoint.Replace("ModelId", modelId);
                    request.Method     = HttpMethod.Post;
                    request.RequestUri = new Uri(endPoint);
                    request.Content    = new ByteArrayContent(bytes);
                    request.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
                    request.Headers.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
                    var frResult = new FormsRecognizerResponse()
                    {
                        status = "Running"
                    };

                    var response = await client.SendAsync(request);

                    var responseBody = await response.Content.ReadAsStringAsync();

                    var result = string.Empty;

                    if (response.StatusCode == System.Net.HttpStatusCode.Accepted)
                    {
                        var operationLocation = response.Headers.GetValues("Operation-Location").FirstOrDefault();
                        if (!string.IsNullOrEmpty(operationLocation))
                        {
                            var clientGet = new HttpClient();
                            clientGet.DefaultRequestHeaders.Add(
                                "Ocp-Apim-Subscription-Key", subscriptionKey);

                            while (frResult.status.Trim().ToLower() != "succeeded" &&
                                   frResult.status.Trim().ToLower() != "failed")
                            {
                                Thread.Sleep(1000);
                                var httpGetResult = clientGet.GetAsync(operationLocation).Result;
                                result   = httpGetResult.Content.ReadAsStringAsync().Result;
                                frResult = JsonConvert.DeserializeObject <FormsRecognizerResponse>(result);
                            }

                            if (frResult.status.Trim().ToLower() == "failed")
                            {
                                return(formEntities);
                            }
                        }

                        // Save the returned Json to Blob
                        var storageAccount = CreateStorageAccountFromConnectionString(storageConnectionString);
                        var jsonFile       = sourceFile.Replace(".jpg", ".json");
                        var blob           = GetBlobReference(folder + jsonFile, sourceContainer, storageConnectionString);
                        blob.UploadTextAsync(result).Wait();

                        GetKeyValuePairs(frResult, formEntities, entityTable,
                                         storageConnectionString, sourceFile, page, formType);
                        formEntities.Add("IsProcessed", "false");
                        return(formEntities);
                    }
                    else
                    {
                        throw new SystemException(response.StatusCode.ToString() + ": " + response.ToString() + "\n " + responseBody);
                    }
                }
        }