Rasa NLU:实体同义词检测不一致

时间:2017-11-15 05:17:49

标签: rasa-nlu

我和我的团队已经使用Rasa NLU作为MS LUIS的替代品已经超过2个月了,到目前为止它对我们来说已经很好了。现在我们有大约900个条目作为实体同义词(因为我们在LUIS中使用List实体)。

只有一些话语,实体被检测为同义词,对于大多数话语,它无法检测到实体同义词。为了检测同义词,我必须创建另一个简单的实体,我们再次使用所有同义词值进行手动训练,一旦使用这个简单的实体训练意图,Rasa似乎将此意图的实体检测为简单和同义词。

另一个快速问题,Rasa中的实体同义词是否只设计为返回一个匹配的实体(与用于返回所有匹配实体值的LUIS不同)?

Rasa的列表实体是否可以替代Rasa?

2 个答案:

答案 0 :(得分:5)

实体Rasa中的同义词会导致一些混乱。它们提供的实际功能非常简单。对于由模型解析的每个实体,将根据实体同义词列表检查该实体的值。如果值与实体同义词匹配,则将其替换为同义词值。

上述声明中的重要一点是,该实体必须先由模型识别,然后才能用同义词替换

因此,请将此作为简化示例。这是我的实体同义词定义:

{
  "value": "New York City",
  "synonyms": ["NYC", "nyc", "the big apple"]
}

如果我的训练数据仅提供此示例:

{
  "text": "in the center of NYC",
  "intent": "search",
  "entities": [
    {
      "start": 17,
      "end": 20,
      "value": "New York City",
      "entity": "city"
    }
  ]
}

我的模型不太可能在In the center of the big apple.这样的句子中检测实体如上所述,如果the big apple未被模型解析为实体,则不能被实体同义词取代,以读纽约市。

出于这个原因,您应该在实际的common_examples培训数据中包含更多示例,并标注实体。一旦实体的所有变体被正确分类,然后将这些值添加到实体同义词,它们将被替换。

[
  {
    "text": "in the center of NYC",
    "intent": "search",
    "entities": [
      {
        "start": 17,
        "end": 20,
        "value": "New York City",
        "entity": "city"
      }
    ]
  },
  {
    "text": "in the centre of New York City",
    "intent": "search",
    "entities": [
      {
        "start": 17,
        "end": 30,
        "value": "New York City",
        "entity": "city"
      }
    ]
  }
]

我已在“Rasa”文档页面中打开了pull request,以便为此效果添加注释。

答案 1 :(得分:0)

首先,我已经下载了一些LUIS模型JSON来执行此操作,如以下屏幕截图所示:

enter image description here

接下来,我编写了一个示例C#控制台应用程序,用于将LUIS模型架构转换为RASA。

这是LUISModel模型类。

using Newtonsoft.Json;
using System;
using System.Collections.Generic;

    namespace JSONConversion.Models
    {

        public class LuisSchema
        {
            public string luis_schema_version { get; set; }
            public string versionId { get; set; }
            public string name { get; set; }
            public string desc { get; set; }
            public string culture { get; set; }
            public List<Intent> intents { get; set; }
            public List<entity> entities { get; set; }
            public object[] composites { get; set; }
            public List<Closedlist> closedLists { get; set; }
            public List<string> bing_entities { get; set; }
            public object[] actions { get; set; }
            public List<Model_Features> model_features { get; set; }
            public List<regex_Features> regex_features { get; set; }
            public List<Utterance> utterances { get; set; }
        }


        public class regex_Features
        {
            public string name { get; set; }
            public string pattern { get; set; }
            public bool activated { get; set; }
        }
        public class Intent
        {
            public string name { get; set; }
        }

        public class entity
        {
            public string name { get; set; }
        }

        public class Closedlist
        {
            public string name { get; set; }
            public List<Sublist> subLists { get; set; }
        }

        public class Sublist
        {
            public string canonicalForm { get; set; }
            public List<string> list { get; set; }
        }

        public class Model_Features
        {
            public string name { get; set; }
            public bool mode { get; set; }
            public string words { get; set; }
            public bool activated { get; set; }
        }

        public class Utterance
        {
            public string text { get; set; }
            public string intent { get; set; }

            [JsonProperty("entities")]
            public List<Entities> Entities { get; set; }
        }

        public class Entities
        {
            [JsonProperty("entity")]
            public string Entity { get; set; }
            public int startPos { get; set; }
            public int endPos { get; set; }
        }
    }

以下是RASAModel模型类:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;

namespace JSONConversion.Models
{
    public class RASASchema
    {
        public Rasa_Nlu_Data rasa_nlu_data { get; set; }
    }

    public class Rasa_Nlu_Data
    {
        public List<Entity_Synonyms> entity_synonyms { get; set; }

        public List<Regex_Features> regex_features { get; set; }
        public List<Common_Examples> common_examples { get; set; }

    }

    public class Entity_Synonyms
    {
        public string value { get; set; }
        public List<string> synonyms { get; set; }
    }

    public class Common_Examples
    {
        public string text { get; set; }
        public string intent { get; set; }
        public List<Entity> entities { get; set; }
    }


    public class Entity
    {
        public string entity { get; set; }
        public string value { get; set; }
        public int start { get; set; }
        public int end { get; set; }
    }

    public class Regex_Features
    {
        public string name { get; set; }
        public string pattern { get; set; }
    }
}

我编写了2个方法,用于解析来自phraselist部分的同义词的LUISModel模型类,并将它们添加到RASA_NLU训练对象的common_examples对象中。

using JSONConversion.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace JSONConversion.Services
{
    public static class JSONHelper
    {
        public static Task<string> ReadFromFile(string FilePath)
        {
            try
            {
                Task<string> readFromFileTask = Task.Run<string>(() => 
                {
                    return File.ReadAllText(FilePath);
                });
                return readFromFileTask;
            }
            catch(Exception ex)
            {
                throw;
            }
        }

        public static RASASchema ConvertLUISJSON(string StringifiedLUISJson)
        {
            try
            {
                LuisSchema luisSchema = JsonConvert.DeserializeObject<LuisSchema>(StringifiedLUISJson);

                RASASchema rasaSchema = new RASASchema();
                rasaSchema.rasa_nlu_data = new Rasa_Nlu_Data();
                rasaSchema.rasa_nlu_data.common_examples = new List<Common_Examples>();
                rasaSchema.rasa_nlu_data.entity_synonyms = new List<Entity_Synonyms>();
                rasaSchema.rasa_nlu_data.regex_features = new List<Regex_Features>();


                luisSchema.closedLists.ForEach(x =>
                {
                    x.subLists.ForEach(y =>
                    {
                        rasaSchema.rasa_nlu_data.entity_synonyms.Add(new Entity_Synonyms()
                        {
                            value = y.canonicalForm,
                            synonyms = y.list
                        });
                    });
                });

                luisSchema.model_features.ForEach(x =>
                {
                    rasaSchema.rasa_nlu_data.entity_synonyms.Add(new Entity_Synonyms()
                    {
                        value = x.name,
                        synonyms = x.words.Split(',').ToList()
                    });
                });

                luisSchema.regex_features.ForEach(x =>
                {
                    rasaSchema.rasa_nlu_data.regex_features.Add(new Regex_Features()
                    {
                        name = x.name,
                        pattern = x.pattern
                    });
                });

                luisSchema.utterances.ForEach(x =>
                {
                    Common_Examples rasaUtterances = new Common_Examples();
                    rasaUtterances.text = x.text;
                    rasaUtterances.intent = x.intent;

                    List<Entity> listOfRASAEntity = new List<Entity>();

                    x.Entities.ForEach(y =>
                    {
                        listOfRASAEntity.Add(new Entity()
                        {
                            start = y.startPos,
                            end = y.endPos,
                            entity = y.Entity,
                            value = x.text.Substring(y.startPos, (y.endPos - y.startPos) + 1)
                        }); 
                    });

                    rasaUtterances.entities = listOfRASAEntity;
                    rasaSchema.rasa_nlu_data.common_examples.Add(rasaUtterances);
                });

                return rasaSchema;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
    }
}

刚刚调用那些JSON转换方法将LUIS模型转换为RASA模型。

using System.Text;
using JSONConversion.Services;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace JSONConversion
{
    class Program
    {
        static void Main(string[] args)
        {

            string json = JsonConvert.SerializeObject(JSONConversion.Services.JSONHelper.ConvertLUISJSON(JSONHelper.ReadFromFile(@"C:\Users\xyz\Documents\luis.json").Result), new JsonSerializerSettings()
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver(),
                Formatting = Formatting.Indented
            });

            File.WriteAllText(@"C:\Users\xyz\Desktop\RASA\data\examples\RasaFormat.json", json, Encoding.UTF8);

        }
    }
}

获得RASA模型后,您只需简单地训练RASA同义词。