Пример #1
0
        /*
         * Method: ClassifyText
         *
         * Description: This method can be used to classify the document into categories. Each category is
         *   identified by a name and associated with a confidence number indicating how confident the API is
         *   about the classification.
         *
         * Parameters:
         *  - document (Document): The document/text on which you want Natural Language API to perform analysis.
         *
         *  - APIKey (String): Implicity required paramter which should be set through the constructor when
         *      creating an object of this class. For more details about the Google API Key please see:
         *      https://developers.google.com/places/web-service/get-api-key
         *
         * Return: The method returns a tuple of two items. If the query is successful, then the first item will
         *   be an object of type ClassifyTextResponse. If the query is unsuccessful and an error is returned,
         *   then the method returns null. The second element is a ResponseStatus object indicating the status of
         *   the query along with the appropiate HTTP code. Since the HTTP query is performed asynchronously, the
         *   return object is wrapped in Task<>.
         */
        public async Task <Tuple <ClassifyTextResponse, ResponseStatus> > ClassifyText(Document document)
        {
            if (BasicFunctions.isEmpty(APIKey))
            {
                return(new Tuple <ClassifyTextResponse, ResponseStatus>(null, NaturalLanguageStatus.MISSING_API_KEY));
            }
            if (document == null)
            {
                return(new Tuple <ClassifyTextResponse, ResponseStatus>(null, NaturalLanguageStatus.MISSING_DOCUMENT));
            }

            ClassifyTextRequest classifyTextRequest = new ClassifyTextRequest(document);

            if (classifyTextRequest == null)
            {
                return(new Tuple <ClassifyTextResponse, ResponseStatus>(null, NaturalLanguageStatus.INTERNAL_SERVER_ERROR));
            }

            // Preparing the header to send a JSON request body
            httpClient.DefaultRequestHeaders.Accept.Clear();
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            // API address to which we make the HTTP POST query
            String request_query         = "v1/documents:classifyText?" + $"key={APIKey}";
            HttpResponseMessage response = await httpClient.PostAsJsonAsync(request_query, classifyTextRequest);

            Stream stream = await response.Content.ReadAsStreamAsync();

            StreamReader streamReader = new StreamReader(stream);
            String       response_str = streamReader.ReadToEnd();

            // Similar two-step hop as we have seen in prior methods
            if (response.IsSuccessStatusCode)
            {
                ClassifyTextResponse classifyTextResponse;

                try {
                    classifyTextResponse = JsonConvert.DeserializeObject <ClassifyTextResponse>(response_str);

                    if (classifyTextResponse == null || classifyTextResponse.Categories.Count == 0)
                    {
                        return(new Tuple <ClassifyTextResponse, ResponseStatus>(null, NaturalLanguageStatus.ZERO_RESULTS));
                    }
                } catch (JsonSerializationException e) {
                    Debug.WriteLine(e.StackTrace);
                    return(new Tuple <ClassifyTextResponse, ResponseStatus>(null, NaturalLanguageStatus.DESERIALIZATION_ERROR));
                }

                return(new Tuple <ClassifyTextResponse, ResponseStatus>(classifyTextResponse, NaturalLanguageStatus.OK));
            }
            else
            {
                // If the query is not successful, then we try to extract details about the error from the response
                ClassifyTextResponse classifyTextResponse;

                try {
                    classifyTextResponse = JsonConvert.DeserializeObject <ClassifyTextResponse>(response_str);
                } catch (JsonSerializationException e) {
                    Debug.WriteLine(e.StackTrace);
                    return(new Tuple <ClassifyTextResponse, ResponseStatus>(null, NaturalLanguageStatus.DESERIALIZATION_ERROR));
                }

                /*
                 * If no error details are available, then we use the information from the response to determine
                 * the appropriate error code.
                 * If we do have an Error object, then we use it to identify the appropriate error code and message
                 */
                if (classifyTextResponse.Error == null)
                {
                    return(new Tuple <ClassifyTextResponse, ResponseStatus>(null,
                                                                            NaturalLanguageStatus.processErrorMessage(response.StatusCode.ToString(), response.ReasonPhrase)));
                }
                else
                {
                    return(new Tuple <ClassifyTextResponse, ResponseStatus>(null,
                                                                            NaturalLanguageStatus.processErrorMessage(classifyTextResponse.Error.Code.ToString(), classifyTextResponse.Error.Message)));
                }
            }
        }
Пример #2
0
        /*
         * Method: AnalyzeEntities
         *
         * Description: This method can be used to find entities in a document or text. If you only wish to run
         *   sentiment analysis on each entity, simultaneously, then please use the AnalyzeEntitySentiment()
         *   method.
         *
         * Parameters:
         *  - document (Document): The document/text from which you want Natural Language API to identify and
         *      analyze entities.
         *  - encodingType (EncodingType): The encoding type to help the API determine offsets. Acceptable values
         *      are NONE, UTF8, UTF16, UTF32. If NONE is specified, then encoding-specific information is not set.
         *
         *  - APIKey (String): Implicity required paramter which should be set through the constructor when
         *      creating an object of this class. For more details about the Google API Key please see:
         *      https://developers.google.com/places/web-service/get-api-key
         *
         * Return: The method returns a tuple of two items. If the query is successful, then the first item will
         *   be an object of type AnalyzeEntitiesResponse, which conatins a list of all the entities identified and
         *   the language of the document. If the query is unsuccessful and an error is returned, then the method
         *   returns null. The second element is a ResponseStatus object indicating the status of the query along
         *   with the appropiate HTTP code. Since the HTTP query is performed asynchronously, the return object is
         *   wrapped in Task<>.
         */
        public async Task <Tuple <AnalyzeEntitiesResponse, ResponseStatus> > AnalyzeEntities(Document document, EncodingType encodingType)
        {
            if (BasicFunctions.isEmpty(APIKey))
            {
                return(new Tuple <AnalyzeEntitiesResponse, ResponseStatus>(null, NaturalLanguageStatus.MISSING_API_KEY));
            }
            if (document == null)
            {
                return(new Tuple <AnalyzeEntitiesResponse, ResponseStatus>(null, NaturalLanguageStatus.MISSING_DOCUMENT));
            }

            AnalyzeEntitiesRequest entitiesRequest = new AnalyzeEntitiesRequest(document, encodingType);

            if (entitiesRequest == null)
            {
                return(new Tuple <AnalyzeEntitiesResponse, ResponseStatus>(null, NaturalLanguageStatus.INTERNAL_SERVER_ERROR));
            }

            // Preparing the header to send a JSON request body
            httpClient.DefaultRequestHeaders.Accept.Clear();
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            // API address to which we make the HTTP POST query
            String request_query         = "v1/documents:analyzeEntities?" + $"key={APIKey}";
            HttpResponseMessage response = await httpClient.PostAsJsonAsync(request_query, entitiesRequest);

            Stream stream = await response.Content.ReadAsStreamAsync();

            StreamReader streamReader = new StreamReader(stream);
            String       response_str = streamReader.ReadToEnd();

            /*
             * Similar two-step hop as we have seen before. We try to deserialize the response string, expecting
             * an object of AnalyzeEntitiesResponse. If the response is not an AnalyzeEntitiesResponse object,
             * then we will encounter a JSONSerialization error and return null. If it is as we expect, then we
             * just return the AnalyzeEntitiesResponse object, so long as it is not empty or null.
             */
            if (response.IsSuccessStatusCode)
            {
                AnalyzeEntitiesResponse entitiesResponse;

                try {
                    entitiesResponse = JsonConvert.DeserializeObject <AnalyzeEntitiesResponse>(response_str);

                    if (entitiesResponse == null || entitiesResponse.Entities.Count == 0)
                    {
                        return(new Tuple <AnalyzeEntitiesResponse, ResponseStatus>(null, NaturalLanguageStatus.ZERO_RESULTS));
                    }
                } catch (JsonSerializationException e) {
                    Debug.WriteLine(e.StackTrace);
                    return(new Tuple <AnalyzeEntitiesResponse, ResponseStatus>(null, NaturalLanguageStatus.DESERIALIZATION_ERROR));
                }

                return(new Tuple <AnalyzeEntitiesResponse, ResponseStatus>(entitiesResponse, NaturalLanguageStatus.OK));
            }
            else
            {
                // If the query is not successful, then we try to extract details about the error from the response
                AnalyzeEntitiesResponse entitiesResponse;

                try {
                    entitiesResponse = JsonConvert.DeserializeObject <AnalyzeEntitiesResponse>(response_str);
                } catch (JsonSerializationException e) {
                    Debug.WriteLine(e.StackTrace);
                    return(new Tuple <AnalyzeEntitiesResponse, ResponseStatus>(null, NaturalLanguageStatus.DESERIALIZATION_ERROR));
                }

                // If no error details are available, then we use the information from the response to determine
                // the appropriate error code
                if (entitiesResponse.Error == null)
                {
                    return(new Tuple <AnalyzeEntitiesResponse, ResponseStatus>(null,
                                                                               NaturalLanguageStatus.processErrorMessage(response.StatusCode.ToString(), response.ReasonPhrase)));
                }
                else
                {
                    // If we do have an Error object, then we use it to identify the appropriate error code and message
                    return(new Tuple <AnalyzeEntitiesResponse, ResponseStatus>(null,
                                                                               NaturalLanguageStatus.processErrorMessage(entitiesResponse.Error.Code.ToString(), entitiesResponse.Error.Message)));
                }
            }
        }