コード例 #1
0
        public void ToneChat()
        {
            IamConfig config = new IamConfig(
                apikey: apikey
                );

            ToneAnalyzerService service = new ToneAnalyzerService(versionDate, config);

            service.SetEndpoint(url);

            var utterances = new List <Utterance>()
            {
                new Utterance()
                {
                    Text = "Hello! Welcome to IBM Watson! How can I help you?",
                    User = "******"
                }
            };

            var result = service.ToneChat(
                utterances: utterances,
                contentLanguage: "en-US",
                acceptLanguage: "en-US"
                );

            Console.WriteLine(result.Response);
        }
コード例 #2
0
        public void ConvertToHtml()
        {
            IamConfig config = new IamConfig(
                apikey: apikey
                );

            CompareComplyService service = new CompareComplyService(versionDate, config);

            service.SetEndpoint(url);

            using (FileStream fs = File.OpenRead(contractAFilePath))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    fs.CopyTo(ms);
                    var result = service.ConvertToHtml(
                        file: ms,
                        filename: Path.GetFileName(contractAFilePath),
                        fileContentType: "application/pdf"
                        );

                    Console.WriteLine(result.Response);
                }
            }
        }
コード例 #3
0
        public void TranslateDocument()
        {
            IamConfig config = new IamConfig(
                apikey: apikey
                );

            LanguageTranslatorService service = new LanguageTranslatorService(versionDate, config);

            DetailedResponse <DocumentStatus> result;

            using (FileStream fs = File.OpenRead(documentToTranslatePath))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    fs.CopyTo(ms);
                    result = service.TranslateDocument(
                        file: ms,
                        filename: Path.GetFileName(documentToTranslatePath),
                        fileContentType: "text/plain",
                        modelId: "en-es"
                        );
                }
            }

            Console.WriteLine(result.Response);
            documentId = result.Result.DocumentId;
        }
コード例 #4
0
        public void CreateClassifier()
        {
            IamConfig config = new IamConfig(
                apikey: apikey
                );

            VisualRecognitionService service = new VisualRecognitionService(versionDate, config);

            service.SetEndpoint(url);

            DetailedResponse <Classifier> result = null;

            using (FileStream positiveExamplesFileStream = File.OpenRead(localGiraffePositiveExamplesFilePath), negativeExamplesFileStream = File.OpenRead(localNegativeExamplesFilePath))
            {
                using (MemoryStream positiveExamplesMemoryStream = new MemoryStream(), negativeExamplesMemoryStream = new MemoryStream())
                {
                    positiveExamplesFileStream.CopyTo(positiveExamplesMemoryStream);
                    negativeExamplesFileStream.CopyTo(negativeExamplesMemoryStream);
                    Dictionary <string, MemoryStream> positiveExamples = new Dictionary <string, MemoryStream>();
                    positiveExamples.Add(giraffeClassname, positiveExamplesMemoryStream);
                    result = service.CreateClassifier(
                        name: createdClassifierName,
                        positiveExamples: positiveExamples,
                        negativeExamples: negativeExamplesMemoryStream,
                        negativeExamplesFilename: Path.GetFileName(localNegativeExamplesFilePath)
                        );
                }
            }

            Console.WriteLine(result.Response);
            classifierId = result.Result.ClassifierId;
        }
コード例 #5
0
        public void AddTrainingData()
        {
            IamConfig config = new IamConfig(
                apikey: apikey
                );

            DiscoveryService service = new DiscoveryService(versionDate, config);

            service.SetEndpoint(url);

            var examples = new List <TrainingExample>()
            {
                new TrainingExample()
                {
                    DocumentId     = "documentId",
                    CrossReference = "crossReference",
                    Relevance      = 1
                }
            };

            var result = service.AddTrainingData(
                environmentId: environmentId,
                collectionId: collectionId,
                naturalLanguageQuery: naturalLanguageQuery,
                filter: "filter",
                examples: examples
                );

            queryId = result.Result.QueryId;

            Console.WriteLine(result.Response);
        }
コード例 #6
0
        public void Tone()
        {
            IamConfig config = new IamConfig(
                apikey: apikey
                );

            ToneAnalyzerService service = new ToneAnalyzerService(versionDate, config);

            service.SetEndpoint(url);

            ToneInput toneInput = new ToneInput()
            {
                Text = "Hello! Welcome to IBM Watson! How can I help you?"
            };

            var result = service.Tone(
                toneInput: toneInput,
                contentType: "text/html",
                sentences: true,
                contentLanguage: "en-US",
                acceptLanguage: "en-US"
                );

            Console.WriteLine(result.Response);
        }
コード例 #7
0
        public void CreateExpansions()
        {
            IamConfig config = new IamConfig(
                apikey: apikey
                );

            DiscoveryService service = new DiscoveryService(versionDate, config);

            service.SetEndpoint(url);

            var expansions = new List <Expansion>()
            {
                new Expansion()
                {
                    InputTerms = new List <string>()
                    {
                        "input-term"
                    },
                    ExpandedTerms = new List <string>()
                    {
                        "expanded-term"
                    }
                }
            };

            var result = service.CreateExpansions(
                environmentId: environmentId,
                collectionId: collectionId,
                expansions: expansions
                );

            Console.WriteLine(result.Response);
        }
コード例 #8
0
        public void CreateStopwordList()
        {
            IamConfig config = new IamConfig(
                apikey: apikey
                );

            DiscoveryService service = new DiscoveryService(versionDate, config);

            service.SetEndpoint(url);

            DetailedResponse <TokenDictStatusResponse> result;

            using (FileStream fs = File.OpenRead(stopwordFileToIngest))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    fs.CopyTo(ms);
                    result = service.CreateStopwordList(
                        environmentId: environmentId,
                        collectionId: collectionId,
                        stopwordFile: ms,
                        stopwordFilename: Path.GetFileName(stopwordFileToIngest)
                        );
                }
            }

            Console.WriteLine(result.Response);
        }
コード例 #9
0
        public void AddWord()
        {
            IamConfig config = new IamConfig(
                apikey: apikey
                );

            SpeechToTextService service = new SpeechToTextService(config);

            service.SetEndpoint(url);

            var customWord = new CustomWord()
            {
                DisplayAs  = ".NET",
                SoundsLike = new List <string>()
                {
                    "dotnet"
                },
                Word = "dotnet"
            };

            var result = service.AddWord(
                customizationId: customizationId,
                wordName: "dotnet",
                word: "dotnet",
                soundsLike: new List <string>()
            {
                "dotnet"
            },
                displayAs: ".NET"
                );

            Console.WriteLine(result.Response);
        }
コード例 #10
0
        public void UpdateCredentials()
        {
            IamConfig config = new IamConfig(
                apikey: apikey
                );

            DiscoveryService service = new DiscoveryService(versionDate, config);

            service.SetEndpoint(url);

            string privateKey       = "privatekey";
            var    privateKeyBytes  = System.Text.Encoding.UTF8.GetBytes(privateKey);
            var    base64PrivateKey = Convert.ToBase64String(privateKeyBytes);

            var updatedCredentialDetails = new CredentialDetails()
            {
                CredentialType = CredentialDetails.CredentialTypeEnumValue.OAUTH2,
                EnterpriseId   = "myEnterpriseIdUpdated",
                ClientId       = "myClientIdUpdated",
                ClientSecret   = "myClientSecretUpdated",
                PublicKeyId    = "myPublicIdKeyUpdated",
                Passphrase     = "myPassphraseUpdated",
                PrivateKey     = base64PrivateKey
            };

            var result = service.UpdateCredentials(
                environmentId: environmentId,
                credentialId: credentialId,
                sourceType: Credentials.SourceTypeEnumValue.BOX,
                credentialDetails: updatedCredentialDetails
                );

            Console.WriteLine(result.Response);
        }
コード例 #11
0
        public void Message()
        {
            IamConfig config = new IamConfig(
                apikey: apikey
                );

            AssistantService service = new AssistantService(versionDate, config);

            service.SetEndpoint(url);

            MessageInput input = new MessageInput()
            {
                MessageType = MessageInput.MessageTypeEnumValue.TEXT,
                Text        = inputString,
                Options     = new MessageInputOptions()
                {
                    ReturnContext    = true,
                    AlternateIntents = true
                }
            };

            var result = service.Message(
                assistantId: assistantId,
                sessionId: sessionId,
                input: input
                );

            Console.WriteLine(result.Response);
        }
コード例 #12
0
        public void AddCorpus()
        {
            IamConfig config = new IamConfig(
                apikey: apikey
                );

            SpeechToTextService service = new SpeechToTextService(config);

            service.SetEndpoint(url);

            DetailedResponse <object> result = null;

            using (FileStream fs = File.OpenRead(corpusPath))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    fs.CopyTo(ms);
                    result = service.AddCorpus(
                        customizationId: customizationId,
                        corpusName: corpusName,
                        corpusFile: ms
                        );
                }
            }

            Console.WriteLine(result.Response);
        }
コード例 #13
0
        public void Profile()
        {
            IamConfig config = new IamConfig(
                apikey: apikey
                );

            PersonalityInsightsService service = new PersonalityInsightsService(versionDate, config);

            service.SetEndpoint(url);

            Content content = new Content()
            {
                ContentItems = new List <ContentItem>()
                {
                    new ContentItem()
                    {
                        Contenttype = ContentItem.ContenttypeEnumValue.TEXT_PLAIN,
                        Language    = ContentItem.LanguageEnumValue.EN,
                        Content     = contentToProfile
                    }
                }
            };

            var result = service.Profile(
                content: content,
                contentType: "text/plain",
                contentLanguage: "en",
                acceptLanguage: "en",
                rawScores: true,
                consumptionPreferences: true,
                csvHeaders: true
                );

            Console.WriteLine(result.Response);
        }
コード例 #14
0
        public void DetectFaces()
        {
            IamConfig config = new IamConfig(
                apikey: apikey
                );

            VisualRecognitionService service = new VisualRecognitionService(versionDate, config);

            service.SetEndpoint(url);

            DetailedResponse <DetectedFaces> result;

            using (FileStream fs = File.OpenRead(localFaceFilePath))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    fs.CopyTo(ms);
                    result = service.DetectFaces(
                        imagesFile: ms,
                        imagesFilename: Path.GetFileName(localFaceFilePath),
                        imagesFileContentType: "image/jpeg",
                        acceptLanguage: "en"
                        );
                }
            }
            Console.WriteLine(result.Response);
        }
コード例 #15
0
        public void UpdateClassifier()
        {
            IamConfig config = new IamConfig(
                apikey: apikey
                );

            VisualRecognitionService service = new VisualRecognitionService(versionDate, config);

            service.SetEndpoint(url);

            DetailedResponse <Classifier> result = null;

            using (FileStream positiveExamplesStream = File.OpenRead(localTurtlePositiveExamplesFilePath))
            {
                using (MemoryStream positiveExamplesMemoryStream = new MemoryStream())
                {
                    Dictionary <string, MemoryStream> positiveExamples = new Dictionary <string, MemoryStream>();
                    positiveExamples.Add(turtleClassname, positiveExamplesMemoryStream);
                    result = service.UpdateClassifier(
                        classifierId: classifierId,
                        positiveExamples: positiveExamples
                        );
                }
            }

            Console.WriteLine(result.Response);
        }
コード例 #16
0
        public void CreateEvent()
        {
            IamConfig config = new IamConfig(
                apikey: apikey
                );

            DiscoveryService service = new DiscoveryService(versionDate, config);

            service.SetEndpoint(url);

            var data = new EventData()
            {
                EnvironmentId = environmentId,
                SessionToken  = sessionToken,
                CollectionId  = collectionId,
                DocumentId    = documentId
            };

            var result = service.CreateEvent(
                type: CreateEventResponse.TypeEnumValue.CLICK,
                data: data
                );

            Console.WriteLine(result.Response);
        }
コード例 #17
0
        public void CreateClassifier()
        {
            IamConfig config = new IamConfig(
                apikey: apikey
                );

            NaturalLanguageClassifierService service = new NaturalLanguageClassifierService(config);

            service.SetEndpoint(url);

            DetailedResponse <Classifier> result = null;

            using (FileStream trainingDataFile = File.OpenRead(classifierDataFilePath), metadataFile = File.OpenRead(metadataDataFilePath))
            {
                using (MemoryStream trainingData = new MemoryStream(), metadata = new MemoryStream())
                {
                    trainingDataFile.CopyTo(trainingData);
                    metadataFile.CopyTo(metadata);
                    result = service.CreateClassifier(
                        metadata: metadata,
                        trainingData: trainingData
                        );
                }
            }

            Console.WriteLine(result.Response);

            classifierId = result.Result.ClassifierId;
        }
コード例 #18
0
        public void CreateCredentials()
        {
            IamConfig config = new IamConfig(
                apikey: apikey
                );

            DiscoveryService service = new DiscoveryService(versionDate, config);

            service.SetEndpoint(url);

            var credentialDetails = new CredentialDetails()
            {
                CredentialType = CredentialDetails.CredentialTypeEnumValue.OAUTH2,
                EnterpriseId   = "myEnterpriseId",
                ClientId       = "myClientId",
                ClientSecret   = "myClientSecret",
                PublicKeyId    = "myPublicIdKey",
                Passphrase     = "myPassphrase",
                PrivateKey     = "myPrivateKey"
            };

            var result = service.CreateCredentials(
                environmentId: environmentId,
                sourceType: Credentials.SourceTypeEnumValue.BOX,
                credentialDetails: credentialDetails
                );

            Console.WriteLine(result.Response);

            credentialId = result.Result.CredentialId;
        }
コード例 #19
0
        public void ClassifyCollection()
        {
            IamConfig config = new IamConfig(
                apikey: apikey
                );

            NaturalLanguageClassifierService service = new NaturalLanguageClassifierService(config);

            service.SetEndpoint(url);

            var collection = new List <ClassifyInput>()
            {
                new ClassifyInput()
                {
                    Text = "Will it be hot today?"
                },
                new ClassifyInput()
                {
                    Text = "Is it raining?"
                }
            };

            var result = service.ClassifyCollection(
                classifierId: classifierId,
                collection: collection
                );

            Console.WriteLine(result.Response);
        }
コード例 #20
0
        public void TestConfigurationInEnvironment()
        {
            IamConfig config = new IamConfig(
                apikey: apikey
                );

            DiscoveryService service = new DiscoveryService(versionDate, config);

            service.SetEndpoint(url);

            using (FileStream fs = File.OpenRead(filepathToIngest))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    fs.CopyTo(ms);
                    var result = service.TestConfigurationInEnvironment(
                        environmentId: environmentId,
                        configurationId: configurationId,
                        file: ms, filename: "watson_beats_jeopardy.html",
                        fileContentType: "text/html"
                        );

                    Console.WriteLine(result.Response);
                }
            }
        }
コード例 #21
0
        public void Synthesize()
        {
            IamConfig config = new IamConfig(
                apikey: apikey
                );

            TextToSpeechService service = new TextToSpeechService(config);

            service.SetEndpoint(url);

            var result = service.Synthesize(
                text: "Hello, welcome to the Watson dotnet SDK!",
                accept: "audio/wav",
                voice: "en-US_AllisonVoice"
                );

            //  Save file
            using (FileStream fs = File.Create("synthesize.wav"))
            {
                result.Result.WriteTo(fs);
                fs.Close();
                result.Result.Close();
            }

            Console.WriteLine(result.Result);
        }
コード例 #22
0
        public void CreateTokenizationDictionary()
        {
            IamConfig config = new IamConfig(
                apikey: apikey
                );

            DiscoveryService service = new DiscoveryService(versionDate, config);

            service.SetEndpoint(url);

            var tokenizationRules = new List <TokenDictRule>()
            {
                new TokenDictRule()
                {
                    Text   = "すしネコ",
                    Tokens = new List <string>()
                    {
                        "すし", "ネコ"
                    },
                    Readings = new List <string>()
                    {
                        "寿司", "ネコ"
                    },
                    PartOfSpeech = "カスタム名詞"
                }
            };

            var result = service.CreateTokenizationDictionary(
                environmentId: environmentId,
                collectionId: collectionId,
                tokenizationRules: tokenizationRules
                );

            Console.WriteLine(result.Response);
        }
コード例 #23
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);
        }
コード例 #24
0
        public void UpdateDocument()
        {
            IamConfig config = new IamConfig(
                apikey: apikey
                );

            DiscoveryService service = new DiscoveryService(versionDate, config);

            service.SetEndpoint(url);

            DetailedResponse <DocumentAccepted> result;

            using (FileStream fs = File.OpenRead(filepathToIngest))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    fs.CopyTo(ms);
                    result = service.UpdateDocument(
                        environmentId: environmentId,
                        collectionId: collectionId,
                        documentId: documentId,
                        file: ms,
                        filename: "watson_beats_jeopardy.html",
                        fileContentType: "text/html",
                        metadata: metadata
                        );
                }
            }

            Console.WriteLine(result.Response);
        }
コード例 #25
0
        public NaturalLanguageClassifierService(TokenOptions options) : base(SERVICE_NAME, URL)
        {
            if (string.IsNullOrEmpty(options.IamApiKey) && string.IsNullOrEmpty(options.IamAccessToken))
            {
                throw new ArgumentNullException(nameof(options.IamAccessToken) + ", " + nameof(options.IamApiKey));
            }
            if (!string.IsNullOrEmpty(options.ServiceUrl))
            {
                this.Endpoint = options.ServiceUrl;
            }
            else
            {
                options.ServiceUrl = this.Endpoint;
            }

            IamConfig iamConfig = null;

            if (!string.IsNullOrEmpty(options.IamAccessToken))
            {
                iamConfig = new IamConfig(
                    userManagedAccessToken: options.IamAccessToken
                    );
            }
            else
            {
                iamConfig = new IamConfig(
                    apikey: options.IamApiKey,
                    iamUrl: options.IamUrl
                    );
            }

            SetAuthenticator(iamConfig);
        }
コード例 #26
0
        public void CreateModel()
        {
            IamConfig config = new IamConfig(
                apikey: apikey
                );

            LanguageTranslatorService service = new LanguageTranslatorService(versionDate, config);

            DetailedResponse <TranslationModel> result;

            using (FileStream fs = File.OpenRead(glossaryPath))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    fs.CopyTo(ms);
                    result = service.CreateModel(
                        baseModelId: "en-fr",
                        forcedGlossary: ms,
                        name: "dotnetExampleModel"
                        );
                }
            }

            Console.WriteLine(result.Response);
            modelId = result.Result.ModelId;
        }
コード例 #27
0
        public void CompareDocuments()
        {
            IamConfig config = new IamConfig(
                apikey: apikey
                );

            CompareComplyService service = new CompareComplyService(versionDate, config);

            service.SetEndpoint(url);

            using (FileStream fs0 = File.OpenRead(contractAFilePath))
            {
                using (FileStream fs1 = File.OpenRead(contractBFilePath))
                {
                    using (MemoryStream ms0 = new MemoryStream())
                    {
                        using (MemoryStream ms1 = new MemoryStream())
                        {
                            fs0.CopyTo(ms0);
                            fs1.CopyTo(ms1);
                            var result = service.CompareDocuments(
                                file1: ms0,
                                file2: ms1,
                                file1ContentType: "application/pdf",
                                file2ContentType: "application/pdf",
                                file1Label: "Contract A",
                                file2Label: "Contract B"
                                );

                            Console.WriteLine(result.Response);
                        }
                    }
                }
            }
        }
コード例 #28
0
        public void ListIdentifiableLanguages()
        {
            IamConfig config = new IamConfig(
                apikey: apikey
                );

            LanguageTranslatorService service = new LanguageTranslatorService(versionDate, config);

            var result = service.ListIdentifiableLanguages();

            Console.WriteLine(result.Response);
        }
コード例 #29
0
        public void Setup()
        {
            Utility.LoadExternalCredentials();
            var       apikey    = Environment.GetEnvironmentVariable("ASSISTANT_APIKEY");
            var       url       = Environment.GetEnvironmentVariable("ASSISTANT_URL");
            IamConfig iamConfig = new IamConfig(
                apikey: apikey
                );

            service = new AssistantService(versionDate, iamConfig);
            service.SetEndpoint(url);
            assistantId = Environment.GetEnvironmentVariable("ASSISTANT_ASSISTANT_ID");
        }
コード例 #30
0
        public void ListBatches()
        {
            IamConfig config = new IamConfig(
                apikey: apikey
                );

            CompareComplyService service = new CompareComplyService(versionDate, config);

            service.SetEndpoint(url);

            var result = service.ListBatches();

            Console.WriteLine(result.Response);
        }