Added support for compisite entities

This commit is contained in:
Ahmed Elhinidy 2016-08-23 14:30:13 +02:00
Родитель 2d3def8608
Коммит 4f46f1be24
6 изменённых файлов: 228 добавлений и 7 удалений

Просмотреть файл

@ -37,6 +37,8 @@
<Compile Include="IntentHandlerAttribute.cs" />
<Compile Include="IntentRouter.cs" />
<Compile Include="Structures\Action.cs" />
<Compile Include="Structures\CompositeEntity.cs" />
<Compile Include="Structures\CompositeEntityChild.cs" />
<Compile Include="Structures\Dialog.cs" />
<Compile Include="Structures\DialogStatus.cs" />
<Compile Include="Structures\Entity.cs" />

Просмотреть файл

@ -0,0 +1,98 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license.
//
// Microsoft Cognitive Services (formerly Project Oxford): https://www.microsoft.com/cognitive-services
//
// Microsoft Cognitive Services (formerly Project Oxford) GitHub:
// https://github.com/Microsoft/ProjectOxford-ClientSDK
//
// Copyright (c) Microsoft Corporation
// All rights reserved.
//
// MIT License:
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
namespace Microsoft.Cognitive.LUIS
{
/// <summary
/// Represents a composite entity recognised by LUIS
/// </summary>
public class CompositeEntity
{
/// <summary>
/// The name of the type of parent entity.
/// </summary>
public string ParentType { get; set; }
/// <summary>
/// The composite entity value.
/// </summary>
public string Value { get; set; }
/// <summary>
/// A list of child entities of the composite entity.
/// </summary>
public CompositeEntityChild[] CompositeEntityChildren { get; set; }
/// <summary>
/// Loads the json object into the properties of the object.
/// </summary>
/// <param name="compositeEntity">Json object containing the composite entity</param>
public void Load(JObject compositeEntity)
{
ParentType = (string)compositeEntity["parentType"];
Value = (string)compositeEntity["value"];
try
{
var values = (JArray)compositeEntity["children"] ?? new JArray();
CompositeEntityChildren = ParseValuesArray(values);
}
catch (Exception e)
{
CompositeEntityChildren = null;
}
}
/// <summary>
/// Parses Json array of composite entity children into composite entity child array.
/// </summary>
/// <param name="array"></param>
/// <returns>entities array</returns>
private CompositeEntityChild[] ParseValuesArray(JArray array)
{
var count = array.Count;
var a = new CompositeEntityChild[count];
for (var i = 0; i < count; i++)
{
var t = new CompositeEntityChild();
t.Load((JObject)array[i]);
a[i] = t;
}
return a;
}
}
}

Просмотреть файл

@ -0,0 +1,66 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license.
//
// Microsoft Cognitive Services (formerly Project Oxford): https://www.microsoft.com/cognitive-services
//
// Microsoft Cognitive Services (formerly Project Oxford) GitHub:
// https://github.com/Microsoft/ProjectOxford-ClientSDK
//
// Copyright (c) Microsoft Corporation
// All rights reserved.
//
// MIT License:
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
namespace Microsoft.Cognitive.LUIS
{
/// <summary
/// Represents a composite entity recognised by LUIS
/// </summary>
public class CompositeEntityChild
{
/// <summary>
/// The name of the type of parent entity.
/// </summary>
public string ParentType { get; set; }
/// <summary>
/// The composite entity value.
/// </summary>
public string Value { get; set; }
/// <summary>
/// Loads the json object into the properties of the object.
/// </summary>
/// <param name="compositeEntityChild">Json object containing the composite entity child</param>
public void Load(JObject compositeEntityChild)
{
ParentType = (string)compositeEntityChild["type"];
Value = (string)compositeEntityChild["value"];
}
}
}

Просмотреть файл

@ -82,6 +82,11 @@ namespace Microsoft.Cognitive.LUIS
/// </summary>
public IDictionary<string, IList<Entity>> Entities { get; set; }
/// <summary>
/// Collection of <see cref="CompositeEntities"/> objects parsed accessed though a dictionary for easy access.
/// </summary>
public IDictionary<string, IList<CompositeEntity>> CompositeEntities { get; set; }
/// <summary>
/// Construct an empty result set.
/// </summary>
@ -130,6 +135,12 @@ namespace Microsoft.Cognitive.LUIS
}
var entities = (JArray)result["entities"] ?? new JArray();
Entities = ParseEntityArrayToDictionary(entities);
if (result["compositeEntities"] != null)
{
var compositeEntities = (JArray)result["compositeEntities"] ?? new JArray();
CompositeEntities = ParseCompositeEntityArrayToDictionary(compositeEntities);
}
}
/// <summary>
@ -149,6 +160,23 @@ namespace Microsoft.Cognitive.LUIS
return entities;
}
/// <summary>
/// gets all composite entities returned by the LUIS service
/// </summary>
/// <returns>a list of all composite entities</returns>
public List<CompositeEntity> GetAllCompositeEntities()
{
List<CompositeEntity> compositeEntities = new List<CompositeEntity>();
foreach (var compositeEntityList in CompositeEntities)
{
foreach (CompositeEntity compositeEntity in compositeEntityList.Value)
{
compositeEntities.Add(compositeEntity);
}
}
return compositeEntities;
}
/// <summary>
/// Parses a json array of intents into an intent array
/// </summary>
@ -169,10 +197,10 @@ namespace Microsoft.Cognitive.LUIS
}
/// <summary>
/// Parses a json array of entities into an entity array
/// Parses a json array of entities into an entity dictionary.
/// </summary>
/// <param name="array"></param>
/// <returns>The object containing the list of entities</returns>
/// <returns>The object containing the dictionary of entities</returns>
private IDictionary<string, IList<Entity>> ParseEntityArrayToDictionary(JArray array)
{
var count = array.Count;
@ -196,5 +224,34 @@ namespace Microsoft.Cognitive.LUIS
return dict;
}
/// <summary>
/// Parses a json array of composite entities into a composite entity dictionary.
/// </summary>
/// <param name="array"></param>
/// <returns>The object containing the dictionary of composite entities</returns>
private IDictionary<string, IList<CompositeEntity>> ParseCompositeEntityArrayToDictionary(JArray array)
{
var count = array.Count;
var dict = new Dictionary<string, IList<CompositeEntity>>();
foreach (var item in array)
{
var e = new CompositeEntity();
e.Load((JObject)item);
IList<CompositeEntity> compositeEntityList;
if (!dict.TryGetValue(e.ParentType, out compositeEntityList))
{
dict[e.ParentType] = new List<CompositeEntity>() { e };
}
else
{
compositeEntityList.Add(e);
}
}
return dict;
}
}
}

Просмотреть файл

@ -60,7 +60,7 @@ namespace Microsoft.Cognitive.LUIS
public ParameterValue[] ParameterValues { get; set; }
/// <summary>
/// Loads the json object into the properties of the object
/// Loads the json object into the properties of the object.
/// </summary>
/// <param name="parameterValue">Json object containing the parameter value</param>
public void Load(JObject parameter)
@ -80,10 +80,10 @@ namespace Microsoft.Cognitive.LUIS
}
/// <summary>
/// Parses Json array of entities into entity array
/// Parses Json array of parameter values into parameter value array.
/// </summary>
/// <param name="array"></param>
/// <returns>entities array</returns>
/// <returns>parameter value array</returns>
private ParameterValue[] ParseValuesArray(JArray array)
{
var count = array.Count;

Просмотреть файл

@ -75,7 +75,5 @@ namespace Microsoft.Cognitive.LUIS
Resolution = new Dictionary<string, Object>();
}
}
}
}