首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rasa NLU:实体同义词检测不一致

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

Stack Overflow用户
提问于 2017-11-15 05:17:48
回答 2查看 5K关注 0票数 3

我和我的团队已经用Rasa NLU来代替路易斯女士2个多月了,到目前为止,我们已经取得了很好的效果。现在,我们有大约900个条目作为实体同义词(就像我们在LUIS中使用列表实体一样)。

只有对某些话语,实体被检测为同义词,而对于大多数话语来说,它无法检测到实体同义词。为了检测同义词,我必须创建另一个简单的实体,它再次使用所有的同义词值进行手动培训,一旦意图用这个简单的实体进行训练,Rasa似乎就可以检测到这个意图的实体,它既是简单的,也是同义词的。

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

在拉萨,除了路易斯的实体名单之外,还有什么别的办法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-11-15 06:17:09

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

上述语句中的一个大问题是,在用同义词替换实体之前,实体必须由模型标识。

因此,把它作为一个简化的例子。以下是我的实体同义词定义:

代码语言:javascript
复制
{
  "value": "New York City",
  "synonyms": ["NYC", "nyc", "the big apple"]
}

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

代码语言:javascript
复制
{
  "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中包含更多的例子,这些实体都是标记的。一旦对实体的所有变体进行了正确的分类,然后将这些值添加到实体同义词中,它们将被替换。

代码语言:javascript
复制
[
  {
    "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页面中,以便为此添加一个注释。

票数 8
EN

Stack Overflow用户

发布于 2017-12-06 07:37:39

首先,我下载了一些用于执行此操作的LUIS模型JSON,如下所示:

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

这里是LUISModel模型类.

代码语言:javascript
复制
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模型类:

代码语言:javascript
复制
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; }
    }
}

和我编写了两个方法,解析短语部分中同义词的LUISModel模型类,并将它们添加到RASA_NLU训练对象中的common_examples对象中。

代码语言:javascript
复制
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模型.

代码语言:javascript
复制
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训练成同义词。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47299882

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档