Search code examples
c#botframeworkazure-language-understanding

LUIS buitin datetime.v2 entity unable to parse daterange type of values


I have used pre-built datetime.v2 entity to handle all my datetime related stuff in LUIS.

I have this utterance: March to June 2017

The expected output from LUIS should be, in the entities:

"resolution": {
        "values": [
          {
            "timex": "XXXX-03",
            "type": "daterange",
            "start": "2017-03-01",
            "end": "2017-06-01"
          }
        ]
      }

But this is what I am getting when I query LUIS:

{
  "query": "march to june 2017",
  "topScoringIntent": {
    "intent": "TestIntent",
    "score": 1.0
  },
  "intents": [
    {
      "intent": "TestIntent",
      "score": 1.0
    },
    {
      "intent": "None",
      "score": 0.05487651
    }
  ],
  "entities": [
    {
      "entity": "march",
      "type": "builtin.datetimeV2.daterange",
      "startIndex": 0,
      "endIndex": 4,
      "resolution": {
        "values": [
          {
            "timex": "XXXX-03",
            "type": "daterange",
            "start": "2017-03-01",
            "end": "2017-04-01"
          },
          {
            "timex": "XXXX-03",
            "type": "daterange",
            "start": "2018-03-01",
            "end": "2018-04-01"
          }
        ]
      }
    },
    {
      "entity": "june 2017",
      "type": "builtin.datetimeV2.daterange",
      "startIndex": 9,
      "endIndex": 17,
      "resolution": {
        "values": [
          {
            "timex": "2017-06",
            "type": "daterange",
            "start": "2017-06-01",
            "end": "2017-07-01"
          }
        ]
      }
    }
  ]
}

I have written the following C# code, to query the date range in my LUIS intent

        [LuisIntent("TestIntent")]
        public async Task TestIntentHandler(IDialogContext context, LuisResult result)
        {
            EntityRecommendation dateTimeEntity, dateRangeEntity;
            if(result.TryFindEntity("builtin.datetimeV2.date", out dateTimeEntity))
            {
               var s = dateTimeEntity.Resolution.Values.Select(x => x).OfType<List<object>>().SelectMany(i => i).ToList();
            }
            if(result.TryFindEntity("builtin.datetimeV2.daterange", out dateRangeEntity))
            {
                var s = dateRangeEntity.Resolution.Values.Select(x => x).OfType<List<object>>().SelectMany(i => i).FirstOrDefault();
                var type = s.GetType();
            }
        }

Can anyone tell me how to query the range of months like the above utterance in LUIS using the prebuilt datetime.v2 type of entity.


Solution

  • I can reproduce the issue on my side, but in my opinion, March (3/1 to 3/31) is date range and June (6/1 to 6/30) is date range, which is easy to be recognized as two date range (as LUIS did). In this article, it gives us an example date range may 2nd to may 5th, if possible, please specify the day of start month and end month, such as March 1st to June 1st 2017.

    enter image description here

    Besides, I checked the source of TryFindEntity, and I find it assign null or FirstOrDefault entity to EntityRecommendation entity, so your dateRangeEntity will be the first entity even though LUIS return two entities (march and june 2017). If you want to extract two entities when you query march to june 2017, please operate LuisResult result directly, not call TryFindEntity method.

    Source code of TryFindEntity:

    // Microsoft.Bot.Builder.Luis.Extensions
    public static bool TryFindEntity(this LuisResult result, string type, out EntityRecommendation entity)
    {
        IList<EntityRecommendation> expr_14 = result.Entities;
        entity = ((expr_14 != null) ? expr_14.FirstOrDefault((EntityRecommendation e) => e.Type == type) : null);
        return entity != null;
    }