Beispiel #1
0
        public void Analyze()
        {
            IamConfig config = new IamConfig(
                apikey: apikey
                );

            NaturalLanguageUnderstandingService service = new NaturalLanguageUnderstandingService(versionDate, config);

            service.SetEndpoint(url);

            var features = new Features()
            {
                Keywords = new KeywordsOptions()
                {
                    Limit     = 8,
                    Sentiment = true,
                    Emotion   = true
                },
                Categories = new CategoriesOptions()
                {
                    Limit = 10
                }
            };

            var result = service.Analyze(
                features: features,
                text: text,
                clean: true,
                fallbackToRaw: true,
                returnAnalyzedText: true,
                language: "en"
                );

            Console.WriteLine(result.Response);
        }
Beispiel #2
0
        public NaturalLanguageUnderstandingExample(string url, string username, string password)
        {
            _naturalLanguageUnderstandingService = new NaturalLanguageUnderstandingService(username, password, "2017-02-27");
            _naturalLanguageUnderstandingService.SetEndpoint(url);

            Analyze();
            ListModels();
            DeleteModel();
            Console.WriteLine("\n~ Natural Language Understanding examples complete.");
        }
Beispiel #3
0
        public WatsonIBMProvider(AppSettings appSettings)
        {
            var token = new TokenOptions()
            {
                IamApiKey = appSettings.WatsonApiKey()
            };

            _service = new NaturalLanguageUnderstandingService(token, appSettings.WatsonVersionDate());
            _service.SetEndpoint(appSettings.WatsonUrl());
        }
Beispiel #4
0
        public void ListModels()
        {
            IamConfig config = new IamConfig(
                apikey: apikey
                );

            NaturalLanguageUnderstandingService service = new NaturalLanguageUnderstandingService(versionDate, config);

            service.SetEndpoint(url);

            var result = service.ListModels();

            Console.WriteLine(result.Response);
        }
Beispiel #5
0
        //[TestMethod]
        public void TestIcp4d_Success()
        {
            var         url         = "";
            var         username    = "";
            var         password    = "";
            var         versionDate = "";
            Icp4dConfig config      = new Icp4dConfig(url: url, username: username, password: password, disableSslVerification: true);
            NaturalLanguageUnderstandingService service = new NaturalLanguageUnderstandingService(versionDate: versionDate, config: config);

            service.SetEndpoint("");
            var listWorkspaceResult = service.ListModels();

            Assert.IsNotNull(listWorkspaceResult);
            Assert.IsNotNull(listWorkspaceResult.Result);
            Assert.IsNotNull(listWorkspaceResult.Result.Models);
        }
Beispiel #6
0
        public void Setup()
        {
            #region Get Credentials
            if (string.IsNullOrEmpty(credentials))
            {
                var    parentDirectory     = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.Parent.Parent.FullName;
                string credentialsFilepath = parentDirectory + Path.DirectorySeparatorChar + "sdk-credentials" + Path.DirectorySeparatorChar + "credentials.json";
                if (File.Exists(credentialsFilepath))
                {
                    try
                    {
                        credentials = File.ReadAllText(credentialsFilepath);
                        credentials = Utility.AddTopLevelObjectToJson(credentials, "VCAP_SERVICES");
                    }
                    catch (Exception e)
                    {
                        throw new Exception(string.Format("Failed to load credentials: {0}", e.Message));
                    }
                }
                else
                {
                    Console.WriteLine("Credentials file does not exist.");
                }

                VcapCredentials vcapCredentials = JsonConvert.DeserializeObject <VcapCredentials>(credentials);
                var             vcapServices    = JObject.Parse(credentials);

                Credential credential = vcapCredentials.GetCredentialByname("natural-language-understanding-sdk")[0].Credentials;
                endpoint = credential.Url;
                apikey   = credential.IamApikey;
            }
            #endregion

            TokenOptions tokenOptions = new TokenOptions()
            {
                IamApiKey  = apikey,
                ServiceUrl = endpoint
            };

            service = new NaturalLanguageUnderstandingService(tokenOptions, "2017-02-27");
            service.SetEndpoint(endpoint);
        }
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure <ApiBehaviorOptions>(options =>
            {
                options.InvalidModelStateResponseFactory = DevKnowledgeBookModelStateValidator.ValidateModelState;
            });
            services.Configure <DevKnowledgeBookConfiguration>(Configuration.GetSection("DevKnowledgeBook"));
            services.Configure <IbmConfiguration>(Configuration.GetSection("IBM"));

            services.AddRouting(options =>
            {
                options.LowercaseUrls = true;
            });

            services.AddControllers(options =>
            {
                options.EnableEndpointRouting = false;
                options.Filters.Add <ApiKeyAuthorizationFilter>();
                options.Filters.Add <ApiExceptionFilter>();
            })
            .SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
            .ConfigureApiBehaviorOptions(options =>
            {
                options.InvalidModelStateResponseFactory = DevKnowledgeBookModelStateValidator.ValidateModelState;
            })
            .AddJsonOptions(options =>
            {
                options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
            });

            //TODO: Read about singleton vs transient vs scoped https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-3.0
            services.AddSingleton <FirestoreDb>(serviceProvider =>
            {
                return(FirestoreDb.Create(Configuration["Firebase:ProjectID"])); //TODO: Add google environment variable https://cloud.google.com/docs/authentication/getting-started
            });

            services.AddSingleton <INaturalLanguageUnderstandingService>(serviceProvider =>
            {
                var IbmConfiguration       = serviceProvider.GetService <IOptions <IbmConfiguration> >().Value;
                var naturalLanguageService = new NaturalLanguageUnderstandingService
                {
                    UserName    = "******",
                    Password    = IbmConfiguration.ApiKey, //TODO: change for production
                    ApiKey      = IbmConfiguration.ApiKey, //TODO: change for production
                    VersionDate = IbmConfiguration.Version,
                };

                naturalLanguageService.SetEndpoint(IbmConfiguration.Url);

                return(naturalLanguageService);
            });

            services.AddSingleton <IFirestoreDbContext, FirestoreDbContext>();
            services.AddSingleton <INaturalLanguageService, NaturalLanguageService>();

            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "DevKnowledgeBook API", Version = "v1"
                });
                options.AddSecurityDefinition("Api Key", ApiKeySecurityScheme.Instance());
                options.AddSecurityRequirement(new OpenApiSecurityRequirement
                {
                    { ApiKeySecurityScheme.Instance(), new List <string>() },
                });

                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                options.IncludeXmlComments(xmlPath);
            });
        }
Beispiel #8
0
 public NaturalLanguageUnderstandingExample(string url, string username, string password)
 {
     _naturalLanguageUnderstandingService = new NaturalLanguageUnderstandingService(username, password, "2018-03-19");
     _naturalLanguageUnderstandingService.SetEndpoint(url);
 }