WitRequestFactory.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (c) Meta Platforms, Inc. and affiliates.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the license found in the
  6. * LICENSE file in the root directory of this source tree.
  7. */
  8. using System.Text;
  9. using System.Collections.Generic;
  10. using System.Web;
  11. using Meta.Voice;
  12. using Meta.WitAi.Configuration;
  13. using Meta.WitAi.Data.Configuration;
  14. using Meta.WitAi.Data.Entities;
  15. using Meta.WitAi.Interfaces;
  16. using Meta.WitAi.Json;
  17. using Meta.WitAi.Requests;
  18. namespace Meta.WitAi
  19. {
  20. public static class WitRequestFactory
  21. {
  22. private static VoiceServiceRequestOptions.QueryParam QueryParam(string key, string value)
  23. {
  24. return new VoiceServiceRequestOptions.QueryParam() { key = key, value = value };
  25. }
  26. private static void HandleWitRequestOptions(WitRequestOptions requestOptions,
  27. IDynamicEntitiesProvider[] additionalEntityProviders)
  28. {
  29. WitResponseClass entities = new WitResponseClass();
  30. bool hasEntities = false;
  31. if (null != additionalEntityProviders)
  32. {
  33. foreach (var provider in additionalEntityProviders)
  34. {
  35. foreach (var providerEntity in provider.GetDynamicEntities())
  36. {
  37. hasEntities = true;
  38. MergeEntities(entities, providerEntity);
  39. }
  40. }
  41. }
  42. if (DynamicEntityKeywordRegistry.HasDynamicEntityRegistry)
  43. {
  44. foreach (var providerEntity in DynamicEntityKeywordRegistry.Instance.GetDynamicEntities())
  45. {
  46. hasEntities = true;
  47. MergeEntities(entities, providerEntity);
  48. }
  49. }
  50. if (null != requestOptions)
  51. {
  52. if (!string.IsNullOrEmpty(requestOptions.tag))
  53. {
  54. requestOptions.QueryParams["tag"] = requestOptions.tag;
  55. }
  56. if (null != requestOptions.dynamicEntities)
  57. {
  58. foreach (var entity in requestOptions.dynamicEntities.GetDynamicEntities())
  59. {
  60. hasEntities = true;
  61. MergeEntities(entities, entity);
  62. }
  63. }
  64. }
  65. if (hasEntities)
  66. {
  67. requestOptions.QueryParams["entities"] = entities.ToString();
  68. }
  69. }
  70. private static void MergeEntities(WitResponseClass entities, WitDynamicEntity providerEntity)
  71. {
  72. if (!entities.HasChild(providerEntity.entity))
  73. {
  74. entities[providerEntity.entity] = new WitResponseArray();
  75. }
  76. var mergedArray = entities[providerEntity.entity];
  77. Dictionary<string, WitResponseClass> map = new Dictionary<string, WitResponseClass>();
  78. HashSet<string> synonyms = new HashSet<string>();
  79. var existingKeywords = mergedArray.AsArray;
  80. for (int i = 0; i < existingKeywords.Count; i++)
  81. {
  82. var keyword = existingKeywords[i].AsObject;
  83. var key = keyword["keyword"].Value;
  84. if(!map.ContainsKey(key))
  85. {
  86. map[key] = keyword;
  87. }
  88. }
  89. foreach (var keyword in providerEntity.keywords)
  90. {
  91. if (map.TryGetValue(keyword.keyword, out var keywordObject))
  92. {
  93. foreach (var synonym in keyword.synonyms)
  94. {
  95. keywordObject["synonyms"].Add(synonym);
  96. }
  97. }
  98. else
  99. {
  100. keywordObject = JsonConvert.SerializeToken(keyword).AsObject;
  101. map[keyword.keyword] = keywordObject;
  102. mergedArray.Add(keywordObject);
  103. }
  104. }
  105. }
  106. private static WitRequestOptions GetSetupOptions(WitRequestOptions newOptions,
  107. IDynamicEntitiesProvider[] additionalDynamicEntities)
  108. {
  109. // Generate options exist
  110. WitRequestOptions options = newOptions != null ? newOptions : new WitRequestOptions();
  111. // Set intents
  112. if (-1 != options.nBestIntents)
  113. {
  114. options.QueryParams["n"] = options.nBestIntents.ToString();
  115. }
  116. // Set dynamic entities
  117. HandleWitRequestOptions(options, additionalDynamicEntities);
  118. // Set tag
  119. if (!string.IsNullOrEmpty(options.tag))
  120. {
  121. options.QueryParams["tag"] = options.tag;
  122. }
  123. return options;
  124. }
  125. /// <summary>
  126. /// Creates a message request that will process a query string with NLU
  127. /// </summary>
  128. /// <param name="config"></param>
  129. /// <param name="query">Text string to process with the NLU</param>
  130. /// <returns></returns>
  131. public static VoiceServiceRequest CreateMessageRequest(this WitConfiguration config, WitRequestOptions requestOptions, VoiceServiceRequestEvents requestEvents, IDynamicEntitiesProvider[] additionalEntityProviders = null)
  132. {
  133. var options = GetSetupOptions(requestOptions, additionalEntityProviders);
  134. return new WitUnityRequest(config, NLPRequestInputType.Text, options, requestEvents);
  135. }
  136. /// <summary>
  137. /// Creates a request for nlu processing that includes a data stream for mic data
  138. /// </summary>
  139. /// <param name="config"></param>
  140. /// <returns></returns>
  141. public static WitRequest CreateSpeechRequest(this WitConfiguration config, WitRequestOptions requestOptions, VoiceServiceRequestEvents requestEvents, IDynamicEntitiesProvider[] additionalEntityProviders = null)
  142. {
  143. var options = GetSetupOptions(requestOptions, additionalEntityProviders);
  144. var path = config.GetEndpointInfo().Speech;
  145. return new WitRequest(config, path, options, requestEvents);
  146. }
  147. /// <summary>
  148. /// Creates a request for getting the transcription from the mic data
  149. /// </summary>
  150. ///<param name="config"></param>
  151. /// <param name="requestOptions"></param>
  152. /// <returns>WitRequest</returns>
  153. public static WitRequest CreateDictationRequest(this WitConfiguration config, WitRequestOptions requestOptions, VoiceServiceRequestEvents requestEvents = null)
  154. {
  155. var options = GetSetupOptions(requestOptions, null);
  156. var path = config.GetEndpointInfo().Dictation;
  157. return new WitRequest(config, path, options, requestEvents);
  158. }
  159. }
  160. }