Esempio n. 1
0
        /// <summary>
        /// Imported json data into an existing TTSPreloadSettings asset
        /// </summary>
        public static bool ImportData(TTSPreloadSettings preloadSettings, string textFilePath)
        {
            // Check for file
            if (!File.Exists(textFilePath))
            {
                Debug.LogError($"TTS Preload Utility - Preload file does not exist\nPath: {textFilePath}");
                return(false);
            }
            // Load file
            string textFileContents = File.ReadAllText(textFilePath);

            if (string.IsNullOrEmpty(textFileContents))
            {
                Debug.LogError($"TTS Preload Utility - Preload file load failed\nPath: {textFilePath}");
                return(false);
            }
            // Parse file
            WitResponseNode node = WitResponseNode.Parse(textFileContents);

            if (node == null)
            {
                Debug.LogError($"TTS Preload Utility - Preload file parse failed\nPath: {textFilePath}");
                return(false);
            }
            // Iterate children for texts
            WitResponseClass data = node.AsObject;
            Dictionary <string, List <string> > textsByVoice = new Dictionary <string, List <string> >();

            foreach (var voiceName in data.ChildNodeNames)
            {
                // Get texts list
                List <string> texts;
                if (textsByVoice.ContainsKey(voiceName))
                {
                    texts = textsByVoice[voiceName];
                }
                else
                {
                    texts = new List <string>();
                }

                // Add text phrases
                string[] voicePhrases = data[voiceName].AsStringArray;
                if (voicePhrases != null)
                {
                    foreach (var phrase in voicePhrases)
                    {
                        if (!string.IsNullOrEmpty(phrase) && !texts.Contains(phrase))
                        {
                            texts.Add(phrase);
                        }
                    }
                }

                // Apply
                textsByVoice[voiceName] = texts;
            }
            // Import
            return(ImportData(preloadSettings, textsByVoice));
        }
Esempio n. 2
0
        public string ToJSON()
        {
            KeyValuePair <string, WitResponseArray> pair = this.GetEntityPair();
            var root = new WitResponseClass();

            root.Add(pair.Key, pair.Value);
            return(root.ToString());
        }
Esempio n. 3
0
        private static void HandleWitRequestOptions(WitRequestOptions requestOptions,
                                                    IDynamicEntitiesProvider[] additionalEntityProviders,
                                                    List <WitRequest.QueryParam> queryParams)
        {
            WitResponseClass entities    = new WitResponseClass();
            bool             hasEntities = false;

            if (null != additionalEntityProviders)
            {
                foreach (var provider in additionalEntityProviders)
                {
                    foreach (var providerEntity in provider.GetDynamicEntities())
                    {
                        hasEntities = true;
                        MergeEntities(entities, providerEntity);
                    }
                }
            }

            if (DynamicEntityKeywordRegistry.HasDynamicEntityRegistry)
            {
                foreach (var providerEntity in DynamicEntityKeywordRegistry.Instance.GetDynamicEntities())
                {
                    hasEntities = true;
                    MergeEntities(entities, providerEntity);
                }
            }

            if (null != requestOptions)
            {
                if (!string.IsNullOrEmpty(requestOptions.tag))
                {
                    queryParams.Add(QueryParam("tag", requestOptions.tag));
                }

                if (null != requestOptions.dynamicEntities)
                {
                    foreach (var entity in requestOptions.dynamicEntities.GetDynamicEntities())
                    {
                        hasEntities = true;
                        MergeEntities(entities, entity);
                    }
                }
            }

            if (hasEntities)
            {
                queryParams.Add(QueryParam("entities", entities.ToString()));
            }
        }
Esempio n. 4
0
        public KeyValuePair <string, WitResponseArray> GetEntityPair()
        {
            var keywordEntries = new WitResponseArray();

            foreach (string keyword in keywords)
            {
                var synonyms = new WitResponseArray();
                synonyms.Add(new WitResponseData(keyword));

                var keywordEntry = new WitResponseClass();
                keywordEntry.Add("keyword", new WitResponseData(keyword));
                keywordEntry.Add("synonyms", synonyms);

                keywordEntries.Add(keywordEntry);
            }
            return(new KeyValuePair <string, WitResponseArray>(entity, keywordEntries));
        }
Esempio n. 5
0
        // Cast to voice data
        public static TTSWitVoiceData AsTTSWitVoiceData(this WitResponseNode responseNode)
        {
            // Get result
            object result    = new TTSWitVoiceData();
            Type   voiceType = typeof(TTSWitVoiceData);

            // Get root & field names
            WitResponseClass voiceRoot = responseNode.AsObject;

            string[] voiceFieldNames = voiceRoot.ChildNodeNames;
            foreach (var voiceFieldName in voiceFieldNames)
            {
                FieldInfo field = voiceType.GetField(voiceFieldName);
                if (field != null && field.IsPublic && !field.IsStatic)
                {
                    // Get value
                    object val = null;
                    // String
                    if (field.FieldType == typeof(string))
                    {
                        val = voiceRoot[voiceFieldName].Value;
                    }
                    // String[]
                    else if (field.FieldType == typeof(string[]))
                    {
                        val = voiceRoot[voiceFieldName].AsStringArray;
                    }
                    // Set value
                    if (val != null)
                    {
                        field.SetValue(result, val);
                    }
                }
                else
                {
                    Log($"Decode Warning\nUnknown field: {voiceFieldName}", true);
                }
            }

            // Return result
            return((TTSWitVoiceData)result);
        }
Esempio n. 6
0
        private static void MergeEntities(WitResponseClass entities, WitDynamicEntity providerEntity)
        {
            if (!entities.HasChild(providerEntity.entity))
            {
                entities[providerEntity.entity] = new WitResponseArray();
            }
            var mergedArray = entities[providerEntity.entity];
            Dictionary <string, WitResponseClass> map = new Dictionary <string, WitResponseClass>();
            HashSet <string> synonyms = new HashSet <string>();
            var existingKeywords      = mergedArray.AsArray;

            for (int i = 0; i < existingKeywords.Count; i++)
            {
                var keyword = existingKeywords[i].AsObject;
                var key     = keyword["keyword"].Value;
                if (!map.ContainsKey(key))
                {
                    map[key] = keyword;
                }
            }
            foreach (var keyword in providerEntity.keywords)
            {
                if (map.TryGetValue(keyword.keyword, out var keywordObject))
                {
                    foreach (var synonym in keyword.synonyms)
                    {
                        keywordObject["synonyms"].Add(synonym);
                    }
                }
                else
                {
                    keywordObject        = keyword.AsJson;
                    map[keyword.keyword] = keywordObject;
                    mergedArray.Add(keywordObject);
                }
            }
        }
Esempio n. 7
0
        // Decode voices
        private static void DecodeVoices(string json, Action <bool> onComplete)
        {
            // Decode
            WitResponseNode response = WitResponseNode.Parse(json);

            if (response == null)
            {
                Log($"Decode Failure\nCould not parse", true);
                _loading = false;
                onComplete?.Invoke(false);
                return;
            }
            // Get locales
            WitResponseClass localeRoot = response.AsObject;

            string[] locales = localeRoot.ChildNodeNames;
            if (locales == null)
            {
                Log($"Decode Failure\nNo locales found", true);
                _loading = false;
                onComplete?.Invoke(false);
            }
            // Iterate locales
            List <TTSWitVoiceData> voiceList = new List <TTSWitVoiceData>();

            foreach (var locale in locales)
            {
                WitResponseArray localeChildren = localeRoot[locale].AsArray;
                foreach (WitResponseNode voice in localeChildren)
                {
                    voiceList.Add(voice.AsTTSWitVoiceData());
                }
            }

            // Finish
            OnDecodeComplete(voiceList.ToArray(), onComplete);
        }
Esempio n. 8
0
 public WitDynamicEntities()
 {
     entities = new WitResponseClass();
 }