Пример #1
0
        public static string ClassifyPlainText(string text)
        {
            var document = new Document
            {
                Type    = Document.Types.Type.PlainText,
                Content = text
            };
            string error;
            object cargo;

            try
            {
                ClassifyTextResponse response = LanguageServiceClient.Create().ClassifyText(document);
                cargo = response.Categories;
                error = null;
            }
            catch (Exception e)
            {
                error = e.Message;
                cargo = string.Empty;
            }
            return(JsonConvert.SerializeObject(new JsonResponse()
            {
                Error = error,
                Cargo = cargo
            }));
        }
        public async stt::Task ClassifyTextAsync()
        {
            moq::Mock <LanguageService.LanguageServiceClient> mockGrpcClient = new moq::Mock <LanguageService.LanguageServiceClient>(moq::MockBehavior.Strict);
            ClassifyTextRequest request = new ClassifyTextRequest
            {
                Document = new Document(),
            };
            ClassifyTextResponse expectedResponse = new ClassifyTextResponse
            {
                Categories =
                {
                    new ClassificationCategory(),
                },
            };

            mockGrpcClient.Setup(x => x.ClassifyTextAsync(request, moq::It.IsAny <grpccore::CallOptions>())).Returns(new grpccore::AsyncUnaryCall <ClassifyTextResponse>(stt::Task.FromResult(expectedResponse), null, null, null, null));
            LanguageServiceClient client = new LanguageServiceClientImpl(mockGrpcClient.Object, null);
            ClassifyTextResponse  responseCallSettings = await client.ClassifyTextAsync(request.Document, gaxgrpc::CallSettings.FromCancellationToken(st::CancellationToken.None));

            xunit::Assert.Same(expectedResponse, responseCallSettings);
            ClassifyTextResponse responseCancellationToken = await client.ClassifyTextAsync(request.Document, st::CancellationToken.None);

            xunit::Assert.Same(expectedResponse, responseCancellationToken);
            mockGrpcClient.VerifyAll();
        }
        public void ClassifyText_ValidQueryWithDocumentURL()
        {
            Document document = new Document(DocumentType.PLAIN_TEXT, googleCloudUri: VALID_DOC_URL);

            Task <Tuple <ClassifyTextResponse, ResponseStatus> > response = nlIntelligence.ClassifyText(document);

            response.Wait();

            Assert.IsNotNull(response.Result.Item1);
            Assert.AreEqual(response.Result.Item2, NaturalLanguageStatus.OK);

            ClassifyTextResponse textResponse = response.Result.Item1;

            Assert.IsNotNull(textResponse.Categories);
            Assert.GreaterOrEqual(textResponse.Categories.Count, 1);

            foreach (ClassificationCategory classification in textResponse.Categories)
            {
                Assert.IsNotNull(classification.Name);
                Assert.IsNotEmpty(classification.Name);

                Assert.GreaterOrEqual(classification.Confidence, 0);
                Assert.LessOrEqual(classification.Confidence, 1);
            }
        }
 /// <summary>Snippet for ClassifyText</summary>
 public void ClassifyText()
 {
     // Snippet: ClassifyText(Document, CallSettings)
     // Create client
     LanguageServiceClient languageServiceClient = LanguageServiceClient.Create();
     // Initialize request argument(s)
     Document document = new Document();
     // Make the request
     ClassifyTextResponse response = languageServiceClient.ClassifyText(document);
     // End snippet
 }
        /// <summary>Snippet for ClassifyTextAsync</summary>
        public async Task ClassifyTextAsync()
        {
            // Snippet: ClassifyTextAsync(Document, CallSettings)
            // Additional: ClassifyTextAsync(Document, CancellationToken)
            // Create client
            LanguageServiceClient languageServiceClient = await LanguageServiceClient.CreateAsync();

            // Initialize request argument(s)
            Document document = new Document();
            // Make the request
            ClassifyTextResponse response = await languageServiceClient.ClassifyTextAsync(document);

            // End snippet
        }
 /// <summary>Snippet for ClassifyText</summary>
 public void ClassifyText_RequestObject()
 {
     // Snippet: ClassifyText(ClassifyTextRequest,CallSettings)
     // Create client
     LanguageServiceClient languageServiceClient = LanguageServiceClient.Create();
     // Initialize request argument(s)
     ClassifyTextRequest request = new ClassifyTextRequest
     {
         Document = new Document(),
     };
     // Make the request
     ClassifyTextResponse response = languageServiceClient.ClassifyText(request);
     // End snippet
 }
 // [START language_classify_gcs_core]
 /// <summary>
 /// Classify text in GCS
 /// </summary>
 public static void SampleClassifyText(string gcsUrs)
 {
     LanguageServiceClient languageServiceClient = LanguageServiceClient.Create();
     // string gcsUrs = "gs://cloud-samples-data/language/classify-entertainment.txt"
     ClassifyTextRequest request = new ClassifyTextRequest
     {
         Document = new Document
         {
             Type          = Document.Types.Type.PlainText,
             GcsContentUri = "gs://cloud-samples-data/language/classify-entertainment.txt",
         },
     };
     ClassifyTextResponse response = languageServiceClient.ClassifyText(request);
     // FIXME: inspect the results
 }
Пример #8
0
 // [START language_classify_text_core]
 /// <summary>
 /// Classify text
 /// </summary>
 public static void SampleClassifyText(string textContent)
 {
     LanguageServiceClient languageServiceClient = LanguageServiceClient.Create();
     // string textContent = "This is about film and movies and television and acting and movie theatres and theatre and drama and entertainment and the arts."
     ClassifyTextRequest request = new ClassifyTextRequest
     {
         Document = new Document
         {
             Type    = Document.Types.Type.PlainText,
             Content = "This is about film and movies and television and acting and movie theatres and theatre and drama and entertainment and the arts.",
         },
     };
     ClassifyTextResponse response = languageServiceClient.ClassifyText(request);
     // FIXME: inspect the results
 }
        /// <summary>Snippet for ClassifyTextAsync</summary>
        public async Task ClassifyTextAsync_RequestObject()
        {
            // Snippet: ClassifyTextAsync(ClassifyTextRequest,CallSettings)
            // Create client
            LanguageServiceClient languageServiceClient = await LanguageServiceClient.CreateAsync();

            // Initialize request argument(s)
            ClassifyTextRequest request = new ClassifyTextRequest
            {
                Document = new Document(),
            };
            // Make the request
            ClassifyTextResponse response = await languageServiceClient.ClassifyTextAsync(request);

            // End snippet
        }
        public async Task ClassifyTextAsync2()
        {
            Mock <LanguageService.LanguageServiceClient> mockGrpcClient = new Mock <LanguageService.LanguageServiceClient>(MockBehavior.Strict);
            ClassifyTextRequest request = new ClassifyTextRequest
            {
                Document = new Document(),
            };
            ClassifyTextResponse expectedResponse = new ClassifyTextResponse();

            mockGrpcClient.Setup(x => x.ClassifyTextAsync(request, It.IsAny <CallOptions>()))
            .Returns(new Grpc.Core.AsyncUnaryCall <ClassifyTextResponse>(Task.FromResult(expectedResponse), null, null, null, null));
            LanguageServiceClient client   = new LanguageServiceClientImpl(mockGrpcClient.Object, null);
            ClassifyTextResponse  response = await client.ClassifyTextAsync(request);

            Assert.Same(expectedResponse, response);
            mockGrpcClient.VerifyAll();
        }
        public void ClassifyText2()
        {
            Mock <LanguageService.LanguageServiceClient> mockGrpcClient = new Mock <LanguageService.LanguageServiceClient>(MockBehavior.Strict);
            ClassifyTextRequest request = new ClassifyTextRequest
            {
                Document = new Document(),
            };
            ClassifyTextResponse expectedResponse = new ClassifyTextResponse();

            mockGrpcClient.Setup(x => x.ClassifyText(request, It.IsAny <CallOptions>()))
            .Returns(expectedResponse);
            LanguageServiceClient client   = new LanguageServiceClientImpl(mockGrpcClient.Object, null);
            ClassifyTextResponse  response = client.ClassifyText(request);

            Assert.Same(expectedResponse, response);
            mockGrpcClient.VerifyAll();
        }
        public void ClassifyText()
        {
            moq::Mock <LanguageService.LanguageServiceClient> mockGrpcClient = new moq::Mock <LanguageService.LanguageServiceClient>(moq::MockBehavior.Strict);
            ClassifyTextRequest request = new ClassifyTextRequest
            {
                Document = new Document(),
            };
            ClassifyTextResponse expectedResponse = new ClassifyTextResponse
            {
                Categories =
                {
                    new ClassificationCategory(),
                },
            };

            mockGrpcClient.Setup(x => x.ClassifyText(request, moq::It.IsAny <grpccore::CallOptions>())).Returns(expectedResponse);
            LanguageServiceClient client   = new LanguageServiceClientImpl(mockGrpcClient.Object, null);
            ClassifyTextResponse  response = client.ClassifyText(request.Document);

            xunit::Assert.Same(expectedResponse, response);
            mockGrpcClient.VerifyAll();
        }
Пример #13
0
        public async static Task <Dictionary <string, object> > GoogleNaturalLanguageUnderstanding(AppSettings appSettings, string text)
        {
            string methodName = "GoogleNaturalLanguageUnderstanding";
            Dictionary <string, object> result = new Dictionary <string, object>();

            try
            {
                CommonService.SetGoogleCredentials(@"googleAI-Harima.json");
                var client = LanguageServiceClient.Create();
                AnalyzeSentimentResponse analyzeSentimentResponse = new AnalyzeSentimentResponse();
                try
                {
                    analyzeSentimentResponse = await client.AnalyzeSentimentAsync(new Document()
                    {
                        Content = text,
                        Type    = Document.Types.Type.PlainText
                    });
                }
                catch (Exception e)
                {
                    Log.Write(appSettings, LogEnum.ERROR.ToString(), label, className, methodName, $"ERROR: {text}");
                    Log.Write(appSettings, LogEnum.ERROR.ToString(), $"02{Assembly.GetExecutingAssembly().GetName().Name}", MethodBase.GetCurrentMethod().ReflectedType.Name, methodName, $"Watson error: {e.Source + Environment.NewLine + e.Message}");
                }

                AnalyzeEntitiesResponse analyzeEntitiesResponse = new AnalyzeEntitiesResponse();
                try
                {
                    analyzeEntitiesResponse = await client.AnalyzeEntitiesAsync(new Document()
                    {
                        Content = text,
                        Type    = Document.Types.Type.PlainText
                    });
                }
                catch (Exception e)
                {
                    Log.Write(appSettings, LogEnum.ERROR.ToString(), label, className, methodName, $"ERROR: {text}");
                    Log.Write(appSettings, LogEnum.ERROR.ToString(), $"02{Assembly.GetExecutingAssembly().GetName().Name}", MethodBase.GetCurrentMethod().ReflectedType.Name, methodName, $"Watson error: {e.Source + Environment.NewLine + e.Message}");
                }

                ClassifyTextResponse classifyTextResponse = new ClassifyTextResponse();
                try
                {
                    LanguageTranslatorRequest languageTranslatorRequest = new LanguageTranslatorRequest();
                    languageTranslatorRequest.Text   = text;
                    languageTranslatorRequest.Source = "es";
                    languageTranslatorRequest.Target = "en";
                    string translation = await LanguageTranslatorService.GoogleLanguageTranslator(appSettings, languageTranslatorRequest);

                    classifyTextResponse = await client.ClassifyTextAsync(new Document()
                    {
                        Content = translation,
                        Type    = Document.Types.Type.PlainText
                    });
                }
                catch (Exception e)
                {
                    Log.Write(appSettings, LogEnum.ERROR.ToString(), label, className, methodName, $"ERROR: {text}");
                    Log.Write(appSettings, LogEnum.ERROR.ToString(), $"02{Assembly.GetExecutingAssembly().GetName().Name}", MethodBase.GetCurrentMethod().ReflectedType.Name, methodName, $"Watson error: {e.Source + Environment.NewLine + e.Message}");
                }
                result["sentiment"] = analyzeSentimentResponse;
                result["entities"]  = analyzeEntitiesResponse;
                result["clasifiy"]  = classifyTextResponse;
                return(result);
            }
            catch (Exception e)
            {
                Log.Write(appSettings, LogEnum.ERROR.ToString(), label, className, methodName, $"ERROR: {text}");
                Log.Write(appSettings, LogEnum.ERROR.ToString(), label, className, methodName, $"ERROR: {e.Source + Environment.NewLine + e.Message + Environment.NewLine + e.StackTrace}");
                throw e;
            }
        }