public static dynamic NaturalLanguageClassifierTraining(AppSettings appSettings, IFormFile trainingDataFile, IFormCollection requestBody)
        {
            string  methodName = "NaturalLanguageClassifierTraining";
            dynamic result     = new ExpandoObject();

            try
            {
                WatsonSettings   settings      = appSettings.WatsonServices.NaturalLanguageClassifier;
                IamAuthenticator authenticator = new IamAuthenticator(apikey: $"{requestBody["apikey"].ToString()}");
                IBM.Watson.NaturalLanguageClassifier.v1.NaturalLanguageClassifierService naturalLanguageClassifier = new IBM.Watson.NaturalLanguageClassifier.v1.NaturalLanguageClassifierService(authenticator);
                naturalLanguageClassifier.SetServiceUrl($"{requestBody["endpoint"].ToString()}");
                JObject metadatajson = JObject.FromObject(new
                {
                    language = "es",
                    name     = requestBody["modelname"].ToString() ?? "NLCModel"
                });
                using (MemoryStream trainingData = new MemoryStream(), metadata = new MemoryStream(Encoding.Default.GetBytes(metadatajson.ToString())))
                {
                    trainingDataFile.CopyTo(trainingData);
                    result = naturalLanguageClassifier.CreateClassifier(
                        trainingMetadata: metadata,
                        trainingData: trainingData
                        ).Result;
                    return(result);
                }
            }
            catch (Exception e)
            {
                Log.Write(appSettings, LogEnum.ERROR.ToString(), label, className, methodName, $"ERROR: {trainingDataFile.FileName}");
                Log.Write(appSettings, LogEnum.ERROR.ToString(), label, className, methodName, $"ERROR: {e.Source + Environment.NewLine + e.Message + Environment.NewLine + e.StackTrace}");
                throw e;
            }
        }
        public static dynamic LanguageTranslator(AppSettings appSettings, LanguageTranslatorRequest requestBody)
        {
            string  methodName = "LanguageTranslator";
            dynamic result     = new ExpandoObject();

            try
            {
                WatsonSettings   settings      = appSettings.WatsonServices.LanguageTranslator;
                IamAuthenticator authenticator = new IamAuthenticator(apikey: $"{requestBody.Apikey}");
                IBM.Watson.LanguageTranslator.v3.LanguageTranslatorService languageTranslator = new IBM.Watson.LanguageTranslator.v3.LanguageTranslatorService($"{settings.Version}", authenticator);
                languageTranslator.SetServiceUrl($"{requestBody.Endpoint}");
                List <string> text = new List <string>();
                text.Add(requestBody.Text);
                result = languageTranslator.Translate(
                    text: text,
                    modelId: requestBody.ModelId,
                    source: requestBody.Source,
                    target: requestBody.Target != null ? requestBody.Target : "en"
                    ).Result;
                return(result);
            }
            catch (Exception e)
            {
                Log.Write(appSettings, LogEnum.ERROR.ToString(), label, className, methodName, $"ERROR: {JsonConvert.SerializeObject(requestBody)}");
                Log.Write(appSettings, LogEnum.ERROR.ToString(), label, className, methodName, $"ERROR: {e.Source + Environment.NewLine + e.Message + Environment.NewLine + e.StackTrace}");
                throw e;
            }
        }
Exemplo n.º 3
0
        public static async Task <dynamic> SpeechToText(AppSettings appSettings, SpeechToTextRequest requestBody)
        {
            string  methodName = "SpeechToText";
            dynamic result     = new ExpandoObject();

            try
            {
                WatsonSettings   settings      = appSettings.WatsonServices.SpeechToText;
                IamAuthenticator authenticator = new IamAuthenticator(apikey: $"{requestBody.Apikey}");
                IBM.Watson.SpeechToText.v1.SpeechToTextService speechToText = new IBM.Watson.SpeechToText.v1.SpeechToTextService(authenticator);
                speechToText.SetServiceUrl($"{requestBody.Endpoint}");
                List <string> audioWavList = await CommonService.AudioToWav(appSettings, requestBody.Url);

                List <SpeechRecognitionResults> speechRecognitionResultsList = new List <SpeechRecognitionResults>();
                string text = "";
                foreach (var audioWav in audioWavList)
                {
                    string     audioWavTemp = CommonService.GetExternalPlatforms(appSettings).STTAudioFileUrl + audioWav;
                    HttpClient client       = new HttpClient();
                    byte[]     audio        = client.GetByteArrayAsync(audioWavTemp).Result;
                    SpeechRecognitionResults speechRecognitionResults = new SpeechRecognitionResults();
                    //HttpClient httpClient = new HttpClient();
                    //httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("audio/wav"));
                    //httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", $"apikey:{requestBody.Apikey}");
                    //HttpContent httpContent = new ByteArrayContent(audio);
                    //var r = await httpClient.PostAsync("https://stream.watsonplatform.net/speech-to-text/api/v1/recognize", httpContent);
                    try
                    {
                        speechRecognitionResults = speechToText.Recognize(
                            audio: new MemoryStream(audio),
                            contentType: "audio/wav",
                            model: requestBody.Model != null ? requestBody.Model : "es-ES_NarrowbandModel"
                            ).Result;
                        speechRecognitionResultsList.Add(speechRecognitionResults);
                        foreach (var item in speechRecognitionResults.Results)
                        {
                            text += item.Alternatives[0].Transcript;
                        }
                        SpeechRecognitionResultsDTO speechRecognitionResultsDTO = new SpeechRecognitionResultsDTO();
                        speechRecognitionResultsDTO.Text = text;
                        speechRecognitionResultsDTO.SpeechRecognitionResults = speechRecognitionResultsList;
                        result = speechRecognitionResultsDTO;
                    }
                    catch (Exception e)
                    {
                        Log.Write(appSettings, LogEnum.ERROR.ToString(), label, className, methodName, $"ERROR: {JsonConvert.SerializeObject(requestBody)}");
                        Log.Write(appSettings, LogEnum.ERROR.ToString(), label, className, methodName, $"ERROR: {e.Source + Environment.NewLine + e.Message + Environment.NewLine + e.StackTrace}");
                        result = e.Message;
                    }
                }
                return(result);
            }
            catch (Exception e)
            {
                Log.Write(appSettings, LogEnum.ERROR.ToString(), label, className, methodName, $"ERROR: {JsonConvert.SerializeObject(requestBody)}");
                Log.Write(appSettings, LogEnum.ERROR.ToString(), label, className, methodName, $"ERROR: {e.Source + Environment.NewLine + e.Message + Environment.NewLine + e.StackTrace}");
                throw e;
            }
        }
Exemplo n.º 4
0
    // Pings PhotonServerSettings and makes it selected (show in Inspector)
    private static void HighlightSettings()
    {
        WatsonSettings settings =
            (WatsonSettings)Resources.Load("WatsonSettings", typeof(WatsonSettings));

        Selection.objects = new UnityEngine.Object[] { settings };
        EditorGUIUtility.PingObject(settings);
    }
        public void Constructor_WithNullParameters_ThrowsArgumentNullException(string username, string password,
            HttpClient httpClient, WatsonSettings settings)
        {
            var exception =
                Record.Exception(() => new MockWatsonService(username, password, httpClient, settings));

            Assert.NotNull(exception);
            Assert.IsType<ArgumentNullException>(exception);
        }
        public static dynamic NaturalLanguageClassifier(AppSettings appSettings, NaturalLanguageClassifierRequest requestBody)
        {
            string  methodName = "NaturalLanguageClassifier";
            dynamic result     = new ExpandoObject();

            try
            {
                WatsonSettings   settings      = appSettings.WatsonServices.NaturalLanguageClassifier;
                IamAuthenticator authenticator = new IamAuthenticator(apikey: $"{requestBody.Apikey}");
                IBM.Watson.NaturalLanguageClassifier.v1.NaturalLanguageClassifierService naturalLanguageClassifier = new IBM.Watson.NaturalLanguageClassifier.v1.NaturalLanguageClassifierService(authenticator);
                naturalLanguageClassifier.SetServiceUrl($"{requestBody.Endpoint}");

                // Preprocesando el texto
                string normalizedtext = requestBody.Text;
                normalizedtext = Regex.Replace(normalizedtext, @"[:][\\]", " ");
                normalizedtext = Regex.Replace(normalizedtext, @"[.\!?,\'/():<>|][\s]", " ");
                normalizedtext = Regex.Replace(normalizedtext, @"[.\!?,\'/():<>|]", " ");
                normalizedtext = Regex.Replace(normalizedtext, @"[\\][\s]", " ");
                normalizedtext = normalizedtext.Replace("\r\n", "\n");
                normalizedtext = Regex.Replace(normalizedtext, @"[\s]{2,}", " ");
                normalizedtext = normalizedtext.Replace("\"", "");
                normalizedtext = normalizedtext.ToLower();
                normalizedtext = Regex.Replace(normalizedtext, @"á", "a");
                normalizedtext = Regex.Replace(normalizedtext, @"é", "e");
                normalizedtext = Regex.Replace(normalizedtext, @"í", "i");
                normalizedtext = Regex.Replace(normalizedtext, @"ó", "o");
                normalizedtext = Regex.Replace(normalizedtext, @"ú", "u");
                normalizedtext = Regex.Replace(normalizedtext, @"à", "a");
                normalizedtext = Regex.Replace(normalizedtext, @"è", "e");
                normalizedtext = Regex.Replace(normalizedtext, @"ì", "i");
                normalizedtext = Regex.Replace(normalizedtext, @"ò", "o");
                normalizedtext = Regex.Replace(normalizedtext, @"ù", "u");
                normalizedtext = Regex.Replace(normalizedtext, @"â", "a");
                normalizedtext = Regex.Replace(normalizedtext, @"ê", "e");
                normalizedtext = Regex.Replace(normalizedtext, @"î", "i");
                normalizedtext = Regex.Replace(normalizedtext, @"ô", "o");
                normalizedtext = Regex.Replace(normalizedtext, @"û", "u");
                result         = naturalLanguageClassifier.Classify(
                    classifierId: requestBody.ModelId,
                    text: normalizedtext
                    ).Result;
                return(result);
            }
            catch (Exception e)
            {
                Log.Write(appSettings, LogEnum.ERROR.ToString(), label, className, methodName, $"ERROR: {JsonConvert.SerializeObject(requestBody)}");
                Log.Write(appSettings, LogEnum.ERROR.ToString(), label, className, methodName, $"ERROR: {e.Source + Environment.NewLine + e.Message + Environment.NewLine + e.StackTrace}");
                throw e;
            }
        }
Exemplo n.º 7
0
        public static dynamic CollectionFields(AppSettings appSettings, DiscoveryRequest requestBody)
        {
            string  methodName = "CollectionFields";
            dynamic result     = new ExpandoObject();

            try
            {
                WatsonSettings   settings      = appSettings.WatsonServices.Discovery;
                IamAuthenticator authenticator = new IamAuthenticator(apikey: $"{requestBody.Apikey}");
                IBM.Watson.Discovery.v1.DiscoveryService discovery = new IBM.Watson.Discovery.v1.DiscoveryService($"{settings.Version}", authenticator);
                discovery.SetServiceUrl($"{requestBody.ApiUrl}");
                result = discovery.ListCollectionFields(requestBody.EnvironmentId, requestBody.CollectionId).Result;
                return(result);
            }
            catch (Exception e)
            {
                Log.Write(appSettings, LogEnum.ERROR.ToString(), label, className, methodName, $"ERROR: {JsonConvert.SerializeObject(requestBody)}");
                Log.Write(appSettings, LogEnum.ERROR.ToString(), label, className, methodName, $"ERROR: {e.Source + Environment.NewLine + e.Message + Environment.NewLine + e.StackTrace}");
                throw e;
            }
        }
        public static dynamic NaturalLanguageClassifierList(AppSettings appSettings, NaturalLanguageClassifierRequest requestBody)
        {
            string  methodName = "NaturalLanguageClassifierList";
            dynamic result     = new ExpandoObject();

            try
            {
                WatsonSettings   settings      = appSettings.WatsonServices.NaturalLanguageClassifier;
                IamAuthenticator authenticator = new IamAuthenticator(apikey: $"{requestBody.Apikey}");
                IBM.Watson.NaturalLanguageClassifier.v1.NaturalLanguageClassifierService naturalLanguageClassifier = new IBM.Watson.NaturalLanguageClassifier.v1.NaturalLanguageClassifierService(authenticator);
                naturalLanguageClassifier.SetServiceUrl($"{requestBody.Endpoint}");
                result = naturalLanguageClassifier.ListClassifiers().Result;
                return(result);
            }
            catch (Exception e)
            {
                Log.Write(appSettings, LogEnum.ERROR.ToString(), label, className, methodName, $"ERROR: {JsonConvert.SerializeObject(requestBody)}");
                Log.Write(appSettings, LogEnum.ERROR.ToString(), label, className, methodName, $"ERROR: {e.Source + Environment.NewLine + e.Message + Environment.NewLine + e.StackTrace}");
                throw e;
            }
        }
Exemplo n.º 9
0
        public static dynamic TextToSpeech(AppSettings appSettings, TextToSpeechRequest requestBody)
        {
            string  methodName = "TextToSpeech";
            dynamic result     = new ExpandoObject();

            try
            {
                WatsonSettings   settings      = appSettings.WatsonServices.TextToSpeech;
                IamAuthenticator authenticator = new IamAuthenticator(apikey: $"{requestBody.Apikey}");
                IBM.Watson.TextToSpeech.v1.TextToSpeechService textToSpeech = new IBM.Watson.TextToSpeech.v1.TextToSpeechService(authenticator);
                textToSpeech.SetServiceUrl($"{requestBody.Endpoint}");
                DetailedResponse <MemoryStream> ms = new DetailedResponse <MemoryStream>();
                ms = textToSpeech.Synthesize(
                    text: requestBody.Text,
                    accept: requestBody.Accept != null ? requestBody.Accept : "audio/mp3",
                    voice: requestBody.Voice != null ? requestBody.Voice : "es-ES_LauraV3Voice"
                    );
                string filename = CommonService.GetExternalPlatforms(appSettings).STTAudioFilePath + Guid.NewGuid() + ".mp3";
                if (Directory.Exists(Path.Combine(CommonService.GetExternalPlatforms(appSettings).STTAudioFilePath)) == false)
                {
                    Directory.CreateDirectory(CommonService.GetExternalPlatforms(appSettings).STTAudioFilePath + Guid.NewGuid());
                }
                using (FileStream fs = File.Create(filename))
                {
                    ms.Result.WriteTo(fs);
                    fs.Close();
                    ms.Result.Close();
                }
                result = filename.Replace(CommonService.GetExternalPlatforms(appSettings).STTAudioFilePath, CommonService.GetExternalPlatforms(appSettings).STTAudioFileUrl);
                return(result);
            }
            catch (Exception e)
            {
                Log.Write(appSettings, LogEnum.ERROR.ToString(), label, className, methodName, $"ERROR: {JsonConvert.SerializeObject(requestBody)}");
                Log.Write(appSettings, LogEnum.ERROR.ToString(), label, className, methodName, $"ERROR: {e.Source + Environment.NewLine + e.Message + Environment.NewLine + e.StackTrace}");
                throw e;
            }
        }
Exemplo n.º 10
0
        public static dynamic QueryCollection(AppSettings appSettings, DiscoveryRequest requestBody)
        {
            string  methodName = "QueryCollection";
            dynamic result     = new ExpandoObject();

            try
            {
                WatsonSettings   settings      = appSettings.WatsonServices.Discovery;
                IamAuthenticator authenticator = new IamAuthenticator(apikey: $"{requestBody.Apikey}");
                IBM.Watson.Discovery.v1.DiscoveryService discovery = new IBM.Watson.Discovery.v1.DiscoveryService($"{settings.Version}", authenticator);
                discovery.SetServiceUrl($"{requestBody.ApiUrl}");

                string[] text = requestBody.Query.Split(":");

                result = discovery.Query(
                    environmentId: requestBody.EnvironmentId,
                    collectionId: requestBody.CollectionId,
                    filter: requestBody.Filter,
                    query: requestBody.Query,
                    naturalLanguageQuery: text[text.Length - 1],
                    aggregation: requestBody.Aggregation,
                    passages: requestBody.Passages ?? false,
                    passagesFields: requestBody.PassagesFields ?? "text",
                    passagesCount: requestBody.PassagesCount ?? 5,
                    highlight: requestBody.Highlight ?? false,
                    count: requestBody.Count ?? 5,
                    offset: requestBody.Offset ?? 0,
                    _return: requestBody._return
                    ).Result;

                if (requestBody.Highlight == true)
                {
                    ((IBM.Watson.Discovery.v1.Model.QueryResponse)result).Results.ForEach(x =>
                    {
                        if (x.AdditionalProperties["highlight"]["subtitle"] != null)
                        {
                            List <string> subtitles = x.AdditionalProperties["highlight"]["subtitle"].ToObject <List <string> >();
                            for (int i = 0; i < subtitles.Count; i++)
                            {
                                subtitles[i] = subtitles[i].Replace("<em>", "").Replace("</em>", "");
                                subtitles[i] = Regex.Replace(subtitles[i], @"[$][_][{].{1,2}[}][$]", "");
                            }
                            x.AdditionalProperties["highlight"]["subtitle"] = JToken.FromObject(subtitles);
                        }
                        if (x.AdditionalProperties["highlight"]["text"] != null)
                        {
                            List <string> texts = x.AdditionalProperties["highlight"]["text"].ToObject <List <string> >();
                            for (int i = 0; i < texts.Count; i++)
                            {
                                texts[i] = texts[i].Replace("<em>", "").Replace("</em>", "");
                                texts[i] = Regex.Replace(texts[i], @"[$][_][{].{1,2}[}][$]", "");
                            }
                            x.AdditionalProperties["highlight"]["text"] = JToken.FromObject(texts);
                        }
                        else
                        {
                            List <string> texts = new List <string>();
                            string txt          = x.AdditionalProperties["text"].ToString();
                            txt = Regex.Replace(txt, @"[$][_][{].{1,2}[}][$]", "");
                            texts.Add(txt);
                            x.AdditionalProperties["highlight"]["text"] = JToken.FromObject(texts);
                        }
                    });
                }

                return(result);
            }
            catch (Exception e)
            {
                Log.Write(appSettings, LogEnum.ERROR.ToString(), label, className, methodName, $"ERROR: {JsonConvert.SerializeObject(requestBody)}");
                Log.Write(appSettings, LogEnum.ERROR.ToString(), label, className, methodName, $"ERROR: {e.Source + Environment.NewLine + e.Message + Environment.NewLine + e.StackTrace}");
                throw e;
            }
        }
Exemplo n.º 11
0
        public static dynamic NaturalLanguageUnderstanding(AppSettings appSettings, NaturalLanguageUnderstandingRequest requestBody, bool personalized = false)
        {
            string  methodName = "NaturalLanguageUnderstanding";
            dynamic result     = new ExpandoObject();

            try
            {
                WatsonSettings   settings      = appSettings.WatsonServices.NaturalLanguageUnderstanding;
                IamAuthenticator authenticator = new IamAuthenticator(apikey: $"{requestBody.Apikey}");
                IBM.Watson.NaturalLanguageUnderstanding.v1.NaturalLanguageUnderstandingService naturalLanguageUnderstanding = new IBM.Watson.NaturalLanguageUnderstanding.v1.NaturalLanguageUnderstandingService($"{settings.Version}", authenticator);
                naturalLanguageUnderstanding.SetServiceUrl($"{requestBody.Endpoint}");
                Features features = new Features();
                if (requestBody.Concepts != null)
                {
                    features.Concepts       = new ConceptsOptions();
                    features.Concepts.Limit = requestBody.Concepts.Limit | 5;
                }
                if (requestBody.Emotion != null)
                {
                    features.Emotion = new EmotionOptions();
                }
                if (requestBody.Entities != null)
                {
                    features.Entities           = new EntitiesOptions();
                    features.Entities.Model     = personalized ? requestBody.Entities.Model : null;
                    features.Entities.Limit     = requestBody.Entities.Limit | 5;
                    features.Entities.Sentiment = requestBody.Entities.Sentiment | true;
                    features.Entities.Emotion   = requestBody.Entities.Emotion | true;
                }
                if (requestBody.Keywords != null)
                {
                    features.Keywords           = new KeywordsOptions();
                    features.Keywords.Limit     = requestBody.Keywords.Limit | 5;
                    features.Keywords.Sentiment = requestBody.Keywords.Sentiment | true;
                    features.Keywords.Emotion   = requestBody.Keywords.Emotion | true;
                }
                if (requestBody.SemanticRoles != null)
                {
                    features.SemanticRoles          = new SemanticRolesOptions();
                    features.SemanticRoles.Limit    = requestBody.SemanticRoles.Limit | 5;
                    features.SemanticRoles.Keywords = requestBody.SemanticRoles.Keywords | true;
                    features.SemanticRoles.Entities = requestBody.SemanticRoles.Entities | true;
                }
                if (requestBody.Sentiment != null)
                {
                    features.Sentiment = new SentimentOptions();
                }
                if (requestBody.Categories != null)
                {
                    features.Categories             = new CategoriesOptions();
                    features.Categories.Explanation = requestBody.Categories.Explanation | true;
                    features.Categories.Limit       = requestBody.Categories.Limit | 5;
                }
                result = naturalLanguageUnderstanding.Analyze(
                    features: features,
                    text: requestBody.Text,
                    returnAnalyzedText: requestBody.ReturnAnalyzedText,
                    language: requestBody.Language
                    ).Result;
                return(result);
            }
            catch (Exception e)
            {
                Log.Write(appSettings, LogEnum.ERROR.ToString(), label, className, methodName, $"ERROR: {JsonConvert.SerializeObject(requestBody)}");
                Log.Write(appSettings, LogEnum.ERROR.ToString(), label, className, methodName, $"ERROR: {e.Source + Environment.NewLine + e.Message + Environment.NewLine + e.StackTrace}");
                throw e;
            }
        }