Initial Commit
This commit is contained in:
+28
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
using Meta.WitAi.Interfaces;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Meta.WitAi.Data.Entities
|
||||
{
|
||||
public class DynamicEntityDataProvider : MonoBehaviour, IDynamicEntitiesProvider
|
||||
{
|
||||
[SerializeField] internal WitDynamicEntitiesData[] entitiesDefinition;
|
||||
public WitDynamicEntities GetDynamicEntities()
|
||||
{
|
||||
WitDynamicEntities entities = new WitDynamicEntities();
|
||||
foreach (var entity in entitiesDefinition)
|
||||
{
|
||||
entities.Merge(entity);
|
||||
}
|
||||
|
||||
return entities;
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4b4e835dcc764fee99862c3c535ab5fa
|
||||
timeCreated: 1646775302
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
using Meta.WitAi.Interfaces;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Meta.WitAi.Data.Entities
|
||||
{
|
||||
public class DynamicEntityProvider : MonoBehaviour, IDynamicEntitiesProvider
|
||||
{
|
||||
[SerializeField] protected WitDynamicEntities entities;
|
||||
|
||||
public WitDynamicEntities GetDynamicEntities()
|
||||
{
|
||||
return entities;
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b789a872764a60545972d2f4505ead38
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6b9f5c6540348cd41a3f48d0e20057db
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
using UnityEngine;
|
||||
using Meta.WitAi.Data.Info;
|
||||
using Meta.WitAi.Interfaces;
|
||||
|
||||
namespace Meta.WitAi.Data.Entities
|
||||
{
|
||||
/// <summary>
|
||||
/// Singleton registry for tracking any objects owned defined in entities in
|
||||
/// a scene
|
||||
/// </summary>
|
||||
public class DynamicEntityKeywordRegistry : MonoBehaviour, IDynamicEntitiesProvider
|
||||
{
|
||||
private static DynamicEntityKeywordRegistry instance;
|
||||
|
||||
private WitDynamicEntities entities = new WitDynamicEntities();
|
||||
|
||||
public static bool HasDynamicEntityRegistry => Instance;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the instance in the scene if there is one
|
||||
/// </summary>
|
||||
public static DynamicEntityKeywordRegistry Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!instance)
|
||||
{
|
||||
instance = FindObjectOfType<DynamicEntityKeywordRegistry>();
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
instance = this;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
instance = null;
|
||||
}
|
||||
|
||||
public void RegisterDynamicEntity(string entity, WitEntityKeywordInfo keyword)
|
||||
{
|
||||
entities.AddKeyword(entity, keyword);
|
||||
}
|
||||
|
||||
public void UnregisterDynamicEntity(string entity, WitEntityKeywordInfo keyword)
|
||||
{
|
||||
entities.RemoveKeyword(entity, keyword);
|
||||
}
|
||||
|
||||
public WitDynamicEntities GetDynamicEntities()
|
||||
{
|
||||
return entities;
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7faff460c1b46b34481ecb99327be2b0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
using UnityEngine;
|
||||
using Meta.WitAi.Data.Info;
|
||||
|
||||
namespace Meta.WitAi.Data.Entities
|
||||
{
|
||||
/// <summary>
|
||||
/// A configured dynamic entity meant to be placed on dynamic objects.
|
||||
/// when the object is enabled this entity will be registered with active
|
||||
/// voice services on activation.
|
||||
/// </summary>
|
||||
public class RegisteredDynamicEntityKeyword : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private string entity;
|
||||
[SerializeField] private WitEntityKeywordInfo keyword;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
if (string.IsNullOrEmpty(keyword.keyword)) return;
|
||||
if (string.IsNullOrEmpty(entity)) return;
|
||||
|
||||
if (DynamicEntityKeywordRegistry.HasDynamicEntityRegistry)
|
||||
{
|
||||
DynamicEntityKeywordRegistry.Instance.RegisterDynamicEntity(entity, keyword);
|
||||
}
|
||||
else
|
||||
{
|
||||
VLog.W($"Cannot register {name}: No dynamic entity registry present in the scene." +
|
||||
$"Please add one and try again.");
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
if (string.IsNullOrEmpty(keyword.keyword)) return;
|
||||
if (string.IsNullOrEmpty(entity)) return;
|
||||
|
||||
if (DynamicEntityKeywordRegistry.HasDynamicEntityRegistry)
|
||||
{
|
||||
DynamicEntityKeywordRegistry.Instance.UnregisterDynamicEntity(entity, keyword);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 314acb2f0d34f4d4b9a72bed29e990d4
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Meta.WitAi.Interfaces;
|
||||
using Meta.WitAi.Json;
|
||||
using Meta.WitAi.Data.Info;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Meta.WitAi.Data.Entities
|
||||
{
|
||||
[Serializable]
|
||||
public class WitDynamicEntities : IDynamicEntitiesProvider, IEnumerable<WitDynamicEntity>
|
||||
{
|
||||
public List<WitDynamicEntity> entities = new List<WitDynamicEntity>();
|
||||
|
||||
public WitDynamicEntities()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public WitDynamicEntities(IEnumerable<WitDynamicEntity> entity)
|
||||
{
|
||||
entities.AddRange(entity);
|
||||
}
|
||||
|
||||
public WitDynamicEntities(params WitDynamicEntity[] entity)
|
||||
{
|
||||
entities.AddRange(entity);
|
||||
}
|
||||
|
||||
public WitResponseClass AsJson
|
||||
{
|
||||
get
|
||||
{
|
||||
WitResponseClass json = new WitResponseClass();
|
||||
foreach (var entity in entities)
|
||||
{
|
||||
json.Add(entity.entity, entity.AsJson);
|
||||
}
|
||||
|
||||
return json;
|
||||
}
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return AsJson.ToString();
|
||||
}
|
||||
|
||||
public IEnumerator<WitDynamicEntity> GetEnumerator() => entities.GetEnumerator();
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
public WitDynamicEntities GetDynamicEntities()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
public void Merge(IDynamicEntitiesProvider provider)
|
||||
{
|
||||
if (null == provider) return;
|
||||
|
||||
entities.AddRange(provider.GetDynamicEntities());
|
||||
}
|
||||
|
||||
public void Merge(IEnumerable<WitDynamicEntity> mergeEntities)
|
||||
{
|
||||
if (null == mergeEntities) return;
|
||||
|
||||
entities.AddRange(mergeEntities);
|
||||
}
|
||||
|
||||
public void Add(WitDynamicEntity dynamicEntity)
|
||||
{
|
||||
int index = entities.FindIndex(e => e.entity == dynamicEntity.entity);
|
||||
if(index < 0) entities.Add(dynamicEntity);
|
||||
else VLog.W($"Cannot add entity, registry already has an entry for {dynamicEntity.entity}");
|
||||
}
|
||||
|
||||
public void Remove(WitDynamicEntity dynamicEntity)
|
||||
{
|
||||
entities.Remove(dynamicEntity);
|
||||
}
|
||||
|
||||
public void AddKeyword(string entityName, WitEntityKeywordInfo keyword)
|
||||
{
|
||||
var entity = entities.Find(e => entityName == e.entity);
|
||||
if (null == entity)
|
||||
{
|
||||
entity = new WitDynamicEntity(entityName);
|
||||
entities.Add(entity);
|
||||
}
|
||||
entity.keywords.Add(keyword);
|
||||
}
|
||||
|
||||
public void RemoveKeyword(string entityName, WitEntityKeywordInfo keyword)
|
||||
{
|
||||
int index = entities.FindIndex(e => e.entity == entityName);
|
||||
if (index >= 0)
|
||||
{
|
||||
entities[index].keywords.Remove(keyword);
|
||||
if(entities[index].keywords.Count == 0) entities.RemoveAt(index);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a296282c67dc244dd974baca4699c554
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
using Meta.WitAi.Interfaces;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Meta.WitAi.Data.Entities
|
||||
{
|
||||
public class WitDynamicEntitiesData : ScriptableObject, IDynamicEntitiesProvider
|
||||
{
|
||||
public WitDynamicEntities entities;
|
||||
public WitDynamicEntities GetDynamicEntities()
|
||||
{
|
||||
return entities;
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 86f432dec1a4be34792c2afdea52bb2d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Meta.WitAi.Interfaces;
|
||||
using Meta.WitAi.Json;
|
||||
using Meta.WitAi.Data.Info;
|
||||
|
||||
namespace Meta.WitAi.Data.Entities
|
||||
{
|
||||
[Serializable]
|
||||
public class WitDynamicEntity : IDynamicEntitiesProvider
|
||||
{
|
||||
public string entity;
|
||||
public List<WitEntityKeywordInfo> keywords = new List<WitEntityKeywordInfo>();
|
||||
|
||||
public WitDynamicEntity()
|
||||
{
|
||||
}
|
||||
|
||||
public WitDynamicEntity(string entity, WitEntityKeywordInfo keyword)
|
||||
{
|
||||
this.entity = entity;
|
||||
this.keywords.Add(keyword);
|
||||
}
|
||||
|
||||
public WitDynamicEntity(string entity, params string[] keywords)
|
||||
{
|
||||
this.entity = entity;
|
||||
foreach (var keyword in keywords)
|
||||
{
|
||||
this.keywords.Add(new WitEntityKeywordInfo()
|
||||
{
|
||||
keyword = keyword,
|
||||
synonyms = new List<string>(new string[] { keyword })
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public WitDynamicEntity(string entity, Dictionary<string, List<string>> keywordsToSynonyms)
|
||||
{
|
||||
this.entity = entity;
|
||||
|
||||
foreach (var synonym in keywordsToSynonyms)
|
||||
{
|
||||
keywords.Add(new WitEntityKeywordInfo()
|
||||
{
|
||||
keyword = synonym.Key,
|
||||
synonyms = synonym.Value
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public WitResponseArray AsJson
|
||||
{
|
||||
get
|
||||
{
|
||||
return JsonConvert.SerializeToken(keywords).AsArray;
|
||||
}
|
||||
}
|
||||
|
||||
public WitDynamicEntities GetDynamicEntities()
|
||||
{
|
||||
return new WitDynamicEntities()
|
||||
{
|
||||
entities = new List<WitDynamicEntity>
|
||||
{
|
||||
this
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: efe24d6c4ed424d25a19396c7a2eec38
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,145 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using Meta.WitAi.Json;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Scripting;
|
||||
|
||||
namespace Meta.WitAi.Data.Entities
|
||||
{
|
||||
public abstract class WitEntityDataBase<T>
|
||||
{
|
||||
public WitResponseNode responseNode;
|
||||
public string id;
|
||||
public string name;
|
||||
public string role;
|
||||
|
||||
public int start;
|
||||
public int end;
|
||||
|
||||
public string type;
|
||||
|
||||
public string body;
|
||||
public T value;
|
||||
|
||||
public float confidence;
|
||||
|
||||
public bool hasData;
|
||||
|
||||
public WitResponseArray entities;
|
||||
|
||||
[Preserve]
|
||||
public WitEntityDataBase<T> FromEntityWitResponseNode(WitResponseNode node)
|
||||
{
|
||||
responseNode = node;
|
||||
WitEntityDataBase<T> result = this;
|
||||
JsonConvert.DeserializeIntoObject(ref result, node);
|
||||
return result;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return value.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
public class WitEntityData : WitEntityDataBase<string>
|
||||
{
|
||||
[Preserve]
|
||||
public WitEntityData() {}
|
||||
|
||||
[Preserve]
|
||||
public WitEntityData(WitResponseNode node)
|
||||
{
|
||||
FromEntityWitResponseNode(node);
|
||||
}
|
||||
|
||||
public static implicit operator bool(WitEntityData data) => null != data && !string.IsNullOrEmpty(data.value);
|
||||
public static implicit operator string(WitEntityData data) => data.value;
|
||||
public static bool operator ==(WitEntityData data, object value) => Equals(data?.value, value);
|
||||
public static bool operator !=(WitEntityData data, object value) => !Equals(data?.value, value);
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj is string s) return s == value;
|
||||
return base.Equals(obj);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return base.GetHashCode();
|
||||
}
|
||||
}
|
||||
|
||||
public class WitEntityFloatData : WitEntityDataBase<float>
|
||||
{
|
||||
[Preserve]
|
||||
public WitEntityFloatData() {}
|
||||
|
||||
[Preserve]
|
||||
public WitEntityFloatData(WitResponseNode node)
|
||||
{
|
||||
FromEntityWitResponseNode(node);
|
||||
}
|
||||
|
||||
public static implicit operator bool(WitEntityFloatData data) =>
|
||||
null != data && data.hasData;
|
||||
|
||||
public bool Approximately(float v, float tolerance = .001f) => Math.Abs(v - value) < tolerance;
|
||||
public static bool operator ==(WitEntityFloatData data, float value) => data?.value == value;
|
||||
public static bool operator !=(WitEntityFloatData data, float value) => !(data == value);
|
||||
public static bool operator ==(WitEntityFloatData data, int value) => data?.value == value;
|
||||
public static bool operator !=(WitEntityFloatData data, int value) => !(data == value);
|
||||
public static bool operator ==(float value, WitEntityFloatData data) => data?.value == value;
|
||||
public static bool operator !=(float value, WitEntityFloatData data) => !(data == value);
|
||||
public static bool operator ==(int value, WitEntityFloatData data) => data?.value == value;
|
||||
public static bool operator !=(int value, WitEntityFloatData data) => !(data == value);
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj is float f) return f == value;
|
||||
return base.Equals(obj);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return base.GetHashCode();
|
||||
}
|
||||
}
|
||||
|
||||
public class WitEntityIntData : WitEntityDataBase<int>
|
||||
{
|
||||
[Preserve]
|
||||
public WitEntityIntData() {}
|
||||
|
||||
[Preserve]
|
||||
public WitEntityIntData(WitResponseNode node)
|
||||
{
|
||||
FromEntityWitResponseNode(node);
|
||||
}
|
||||
|
||||
public static implicit operator bool(WitEntityIntData data) =>
|
||||
null != data && data.hasData;
|
||||
public static bool operator ==(WitEntityIntData data, int value) => data?.value == value;
|
||||
public static bool operator !=(WitEntityIntData data, int value) => !(data == value);
|
||||
public static bool operator ==(int value, WitEntityIntData data) => data?.value == value;
|
||||
public static bool operator !=(int value, WitEntityIntData data) => !(data == value);
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj is int i) return i == value;
|
||||
return base.Equals(obj);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return base.GetHashCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 50471c3e54af4dc1adc8cab506b1e28e
|
||||
timeCreated: 1646062595
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
using Meta.WitAi.Interfaces;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Meta.WitAi.Data.Entities
|
||||
{
|
||||
public class WitSimpleDynamicEntity : MonoBehaviour, IDynamicEntitiesProvider
|
||||
{
|
||||
[SerializeField] private string entityName;
|
||||
[SerializeField] private string[] keywords;
|
||||
|
||||
public WitDynamicEntities GetDynamicEntities()
|
||||
{
|
||||
var entity = new WitDynamicEntity(entityName, keywords);
|
||||
var entities = new WitDynamicEntities(entity);
|
||||
return entities;
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e15f31f7f3b6c49fb96d0553d125fe22
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user