SimpleIntentHandler.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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;
  9. using Meta.WitAi.Json;
  10. using UnityEngine;
  11. using UnityEngine.Events;
  12. namespace Meta.WitAi.CallbackHandlers
  13. {
  14. [AddComponentMenu("Wit.ai/Response Matchers/Simple Intent Handler")]
  15. public class SimpleIntentHandler : WitIntentMatcher
  16. {
  17. [SerializeField] private UnityEvent onIntentTriggered = new UnityEvent();
  18. [Tooltip("Confidence ranges are executed in order. If checked, all confidence values will be checked instead of stopping on the first one that matches.")]
  19. [SerializeField] public bool allowConfidenceOverlap;
  20. #if UNITY_2021_3_2 || UNITY_2021_3_3 || UNITY_2021_3_4 || UNITY_2021_3_5
  21. [NonReorderable]
  22. #endif
  23. [SerializeField] public ConfidenceRange[] confidenceRanges;
  24. public UnityEvent OnIntentTriggered => onIntentTriggered;
  25. protected override void OnResponseSuccess(WitResponseNode response)
  26. {
  27. onIntentTriggered.Invoke();
  28. UpdateRanges(response);
  29. }
  30. protected override void OnResponseInvalid(WitResponseNode response, string error)
  31. {
  32. UpdateRanges(response);
  33. }
  34. private void UpdateRanges(WitResponseNode response)
  35. {
  36. // Find intents if possible
  37. var intents = response?.GetIntents();
  38. if (intents == null)
  39. {
  40. return;
  41. }
  42. // Iterate intents
  43. foreach (var intentData in intents)
  44. {
  45. if (string.Equals(intent, intentData.name, StringComparison.CurrentCultureIgnoreCase))
  46. {
  47. // Found intent
  48. RefreshConfidenceRange(intentData.confidence, confidenceRanges, allowConfidenceOverlap);
  49. return;
  50. }
  51. }
  52. // Not matched
  53. RefreshConfidenceRange(0, confidenceRanges, allowConfidenceOverlap);
  54. }
  55. }
  56. }