Esempio n. 1
0
        public void AnalyzeEntities_ValidQueryWithDocumentURL()
        {
            Document document = new Document(DocumentType.PLAIN_TEXT, googleCloudUri: VALID_DOC_URL);

            Task <Tuple <AnalyzeEntitiesResponse, ResponseStatus> > response = nlIntelligence.AnalyzeEntities(document, EncodingType.UTF8);

            response.Wait();

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

            AnalyzeEntitiesResponse entitiesResponse = response.Result.Item1;

            Assert.IsNotNull(entitiesResponse.Entities);
            Assert.GreaterOrEqual(entitiesResponse.Entities.Count, 1);

            foreach (Entity entity in entitiesResponse.Entities)
            {
                Assert.IsNotNull(entity.Name);
                Assert.IsNotEmpty(entity.Name);

                Assert.IsNotNull(entity.Mentions);
                Assert.GreaterOrEqual(entity.Mentions.Count, 1);

                Assert.GreaterOrEqual(entity.Salience, 0);
                Assert.LessOrEqual(entity.Salience, 1);

                Assert.IsNotNull(entity.Type);
                Assert.AreNotEqual(entity.Type, EntityResponseType.UNKNOWN);
            }
        }
 /// <summary>Snippet for AnalyzeEntities</summary>
 public void AnalyzeEntities2()
 {
     // Snippet: AnalyzeEntities(Document, CallSettings)
     // Create client
     LanguageServiceClient languageServiceClient = LanguageServiceClient.Create();
     // Initialize request argument(s)
     Document document = new Document();
     // Make the request
     AnalyzeEntitiesResponse response = languageServiceClient.AnalyzeEntities(document);
     // End snippet
 }
Esempio n. 3
0
 public void AnalyzeEntities_RequestObject()
 {
     // Snippet: AnalyzeEntities(AnalyzeEntitiesRequest,CallSettings)
     // Create client
     LanguageServiceClient languageServiceClient = LanguageServiceClient.Create();
     // Initialize request argument(s)
     AnalyzeEntitiesRequest request = new AnalyzeEntitiesRequest
     {
         Document = new Document(),
     };
     // Make the request
     AnalyzeEntitiesResponse response = languageServiceClient.AnalyzeEntities(request);
     // End snippet
 }
Esempio n. 4
0
        public async Task AnalyzeEntitiesAsync()
        {
            // Snippet: AnalyzeEntitiesAsync(Document,EncodingType,CallSettings)
            // Additional: AnalyzeEntitiesAsync(Document,EncodingType,CancellationToken)
            // Create client
            LanguageServiceClient languageServiceClient = LanguageServiceClient.Create();
            // Initialize request argument(s)
            Document     document     = new Document();
            EncodingType encodingType = EncodingType.None;
            // Make the request
            AnalyzeEntitiesResponse response = await languageServiceClient.AnalyzeEntitiesAsync(document, encodingType);

            // End snippet
        }
 // [START language_entities_gcs_core]
 /// <summary>
 /// Analyze entities in text stored in GCS
 /// </summary>
 public static void SampleAnalyzeEntities(string gcsUr)
 {
     LanguageServiceClient languageServiceClient = LanguageServiceClient.Create();
     // string gcsUr = "gs://cloud-samples-data/language/entity.txt"
     AnalyzeEntitiesRequest request = new AnalyzeEntitiesRequest
     {
         Document = new Document
         {
             Type          = Document.Types.Type.PlainText,
             GcsContentUri = "gs://cloud-samples-data/language/entity.txt",
         },
     };
     AnalyzeEntitiesResponse response = languageServiceClient.AnalyzeEntities(request);
     // FIXME: inspect the results
 }
 // [START language_entities_text_core]
 /// <summary>
 /// Analyze entities in text
 /// </summary>
 public static void SampleAnalyzeEntities(string textContent)
 {
     LanguageServiceClient languageServiceClient = LanguageServiceClient.Create();
     // string textContent = "California is a state."
     AnalyzeEntitiesRequest request = new AnalyzeEntitiesRequest
     {
         Document = new Document
         {
             Type    = Document.Types.Type.PlainText,
             Content = "California is a state.",
         },
     };
     AnalyzeEntitiesResponse response = languageServiceClient.AnalyzeEntities(request);
     // FIXME: inspect the results
 }
Esempio n. 7
0
        public async Task AnalyzeEntitiesAsync_RequestObject()
        {
            // Snippet: AnalyzeEntitiesAsync(AnalyzeEntitiesRequest,CallSettings)
            // Create client
            LanguageServiceClient languageServiceClient = await LanguageServiceClient.CreateAsync();

            // Initialize request argument(s)
            AnalyzeEntitiesRequest request = new AnalyzeEntitiesRequest
            {
                Document = new Document(),
            };
            // Make the request
            AnalyzeEntitiesResponse response = await languageServiceClient.AnalyzeEntitiesAsync(request);

            // End snippet
        }
        /// <summary>Snippet for AnalyzeEntitiesAsync</summary>
        public async Task AnalyzeEntitiesRequestObjectAsync()
        {
            // Snippet: AnalyzeEntitiesAsync(AnalyzeEntitiesRequest, CallSettings)
            // Additional: AnalyzeEntitiesAsync(AnalyzeEntitiesRequest, CancellationToken)
            // Create client
            LanguageServiceClient languageServiceClient = await LanguageServiceClient.CreateAsync();

            // Initialize request argument(s)
            AnalyzeEntitiesRequest request = new AnalyzeEntitiesRequest
            {
                Document     = new Document(),
                EncodingType = EncodingType.None,
            };
            // Make the request
            AnalyzeEntitiesResponse response = await languageServiceClient.AnalyzeEntitiesAsync(request);

            // End snippet
        }
Esempio n. 9
0
        public void AnalyzeEntities()
        {
            // Sample: AnalyzeEntities
            // Additional: AnalyzeEntities(Document,*)
            LanguageServiceClient client     = LanguageServiceClient.Create();
            Document document                = Document.FromPlainText("Richard of York gave battle in vain.");
            AnalyzeEntitiesResponse response = client.AnalyzeEntities(document);

            Console.WriteLine($"Detected language: {response.Language}");
            Console.WriteLine("Detected entities:");
            foreach (Entity entity in response.Entities)
            {
                Console.WriteLine($"  {entity.Name} ({(int) (entity.Salience * 100)}%)");
            }
            // End sample

            Assert.Equal(3, response.Entities.Count);
            Assert.Equal("Richard of York", response.Entities[0].Name);
        }
        public async Task AnalyzeEntitiesAsync2()
        {
            Mock <LanguageService.LanguageServiceClient> mockGrpcClient = new Mock <LanguageService.LanguageServiceClient>(MockBehavior.Strict);
            AnalyzeEntitiesRequest request = new AnalyzeEntitiesRequest
            {
                Document = new Document(),
            };
            AnalyzeEntitiesResponse expectedResponse = new AnalyzeEntitiesResponse
            {
                Language = "language-1613589672",
            };

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

            Assert.Same(expectedResponse, response);
            mockGrpcClient.VerifyAll();
        }
        public void AnalyzeEntities2()
        {
            Mock <LanguageService.LanguageServiceClient> mockGrpcClient = new Mock <LanguageService.LanguageServiceClient>(MockBehavior.Strict);
            AnalyzeEntitiesRequest request = new AnalyzeEntitiesRequest
            {
                Document = new Document(),
            };
            AnalyzeEntitiesResponse expectedResponse = new AnalyzeEntitiesResponse
            {
                Language = "language-1613589672",
            };

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

            Assert.Same(expectedResponse, response);
            mockGrpcClient.VerifyAll();
        }
        public void AnalyzeEntities1()
        {
            moq::Mock <LanguageService.LanguageServiceClient> mockGrpcClient = new moq::Mock <LanguageService.LanguageServiceClient>(moq::MockBehavior.Strict);
            AnalyzeEntitiesRequest request = new AnalyzeEntitiesRequest
            {
                Document     = new Document(),
                EncodingType = EncodingType.None,
            };
            AnalyzeEntitiesResponse expectedResponse = new AnalyzeEntitiesResponse
            {
                Entities = { new Entity(), },
                Language = "language7dae1285",
            };

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

            xunit::Assert.Same(expectedResponse, response);
            mockGrpcClient.VerifyAll();
        }
        public async stt::Task AnalyzeEntities1Async()
        {
            moq::Mock <LanguageService.LanguageServiceClient> mockGrpcClient = new moq::Mock <LanguageService.LanguageServiceClient>(moq::MockBehavior.Strict);
            AnalyzeEntitiesRequest request = new AnalyzeEntitiesRequest
            {
                Document     = new Document(),
                EncodingType = EncodingType.None,
            };
            AnalyzeEntitiesResponse expectedResponse = new AnalyzeEntitiesResponse
            {
                Entities = { new Entity(), },
                Language = "language7dae1285",
            };

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

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

            xunit::Assert.Same(expectedResponse, responseCancellationToken);
            mockGrpcClient.VerifyAll();
        }
Esempio n. 14
0
        public void AnalyzeEntitySentiment_ValidQueryWithDocumentContent()
        {
            Document document = new Document(DocumentType.PLAIN_TEXT, content: File.ReadAllText(VALID_FILE_LOCATION));

            Task <Tuple <AnalyzeEntitiesResponse, ResponseStatus> > response = nlIntelligence.AnalyzeEntitySentiment(document, EncodingType.UTF8);

            response.Wait();

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

            AnalyzeEntitiesResponse entitiesResponse = response.Result.Item1;

            Assert.IsNotNull(entitiesResponse.Entities);
            Assert.GreaterOrEqual(entitiesResponse.Entities.Count, 1);

            foreach (Entity entity in entitiesResponse.Entities)
            {
                Assert.IsNotNull(entity.Name);
                Assert.IsNotEmpty(entity.Name);

                Assert.IsNotNull(entity.Mentions);
                Assert.GreaterOrEqual(entity.Mentions.Count, 1);

                Assert.GreaterOrEqual(entity.Salience, 0);
                Assert.LessOrEqual(entity.Salience, 1);

                Assert.IsNotNull(entity.Sentiment);
                Assert.GreaterOrEqual(entity.Sentiment.Magnitude, 0);
                Assert.GreaterOrEqual(entity.Sentiment.Score, -1);
                Assert.LessOrEqual(entity.Sentiment.Score, 1);

                Assert.IsNotNull(entity.Type);
                Assert.AreNotEqual(entity.Type, EntityResponseType.UNKNOWN);
            }
        }
Esempio n. 15
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;
            }
        }