Example #1
0
 private static void MatchChoiceByIndex(List <Choice> list, List <ModelResult <FoundChoice> > matched, ModelResult <FoundChoice> match)
 {
     try
     {
         var index = int.Parse(match.Resolution.Value) - 1;
         if (index >= 0 && index < list.Count)
         {
             var choice = list[index];
             matched.Add(new ModelResult <FoundChoice>
             {
                 Start      = match.Start,
                 End        = match.End,
                 TypeName   = "choice",
                 Text       = match.Text,
                 Resolution = new FoundChoice
                 {
                     Value = choice.Value,
                     Index = index,
                     Score = 1.0f
                 }
             });
         }
     }
     catch (Exception) { }
 }
Example #2
0
        private static ModelResult <FoundValue> MatchValue(List <Token> tokens, int maxDistance, FindValuesOptions options, int index, string value, List <Token> vTokens, int startPos)
        {
            // Match value to utterance and calculate total deviation.
            // - The tokens are matched in order so "second last" will match in
            //   "the second from last one" but not in "the last from the second one".
            // - The total deviation is a count of the number of tokens skipped in the
            //   match so for the example above the number of tokens matched would be
            //   2 and the total deviation would be 1.
            var matched        = 0;
            var totalDeviation = 0;
            var start          = -1;
            var end            = -1;

            foreach (var token in vTokens)
            {
                // Find the position of the token in the utterance.
                var pos = IndexOfToken(tokens, token, startPos);
                if (pos >= 0)
                {
                    // Calculate the distance between the current tokens position and the previous tokens distance.
                    var distance = matched > 0 ? pos - startPos : 0;
                    if (distance <= maxDistance)
                    {
                        // Update count of tokens matched and move start pointer to search for next token after
                        // the current token.
                        matched++;
                        totalDeviation += distance;
                        startPos        = pos + 1;

                        // Update start & end position that will track the span of the utterance that's matched.
                        if (start < 0)
                        {
                            start = pos;
                        }
                        end = pos;
                    }
                }
            }

            // Calculate score and format result
            // - The start & end positions and the results text field will be corrected by the caller.
            ModelResult <FoundValue> result = null;

            if (matched > 0 && (matched == vTokens.Count || options.AllowPartialMatches))
            {
                // Percentage of tokens matched. If matching "second last" in
                // "the second from last one" the completeness would be 1.0 since
                // all tokens were found.
                var completeness = matched / vTokens.Count;

                // Accuracy of the match. The accuracy is reduced by additional tokens
                // occurring in the value that weren't in the utterance. So an utterance
                // of "second last" matched against a value of "second from last" would
                // result in an accuracy of 0.5.
                var accuracy = (matched / (matched + totalDeviation));

                // The final score is simply the completeness multiplied by the accuracy.
                var score = completeness * accuracy;

                // Format result
                result = new ModelResult <FoundValue>
                {
                    Start      = start,
                    End        = end,
                    TypeName   = "value",
                    Resolution = new FoundValue
                    {
                        Value = value,
                        Index = index,
                        Score = score
                    }
                };
            }
            return(result);
        }