Esempio n. 1
0
        internal LinkedUtterance LinkUtterance(string utterance)
        {
            var parts = new List <LinkedUtterancePart>();
            var words = utterance.Split(' ');

            foreach (var word in words)
            {
                if (_indexedNodes.ContainsKey(word))
                {
                    var entityInfo = new EntityInfo(word, word, 1, 1);
                    parts.Add(LinkedUtterancePart.Entity(word, new[] { entityInfo }));
                }
                else
                {
                    parts.Add(LinkedUtterancePart.Word(word));
                }
            }

            return(new LinkedUtterance(parts));
        }
Esempio n. 2
0
        internal LinkedUtterance LinkedUtterance_Hungry()
        {
            var usedMatches = new List <EntityMatch>();

            while (true)
            {
                EntityMatch bestMatch = null;
                foreach (var match in _matches.SelectMany(m => m))
                {
                    var hasCollision = usedMatches.Any(u => testCollision(match, u));
                    if (hasCollision)
                    {
                        continue;
                    }

                    // var bestScore = bestMatch == null ? 0 : bestMatch.Entity.InBounds + bestMatch.Entity.OutBounds;
                    // var currentScore = match.Entity.InBounds + match.Entity.OutBounds;

                    var bestScore    = bestMatch == null ? 0 : bestMatch.Entity.Score;
                    var currentScore = match.Entity.Score;
                    if (bestScore < currentScore)
                    {
                        bestMatch = match;
                    }
                }
                if (bestMatch == null)
                {
                    break;
                }

                usedMatches.Add(bestMatch);
            }

            var linkedParts = new List <LinkedUtterancePart>();

            for (var i = 0; i < _words.Length; ++i)
            {
                var match = getIndexedMatch(i, usedMatches);
                if (match == null)
                {
                    linkedParts.Add(LinkedUtterancePart.Word(_words[i]));
                }
                else
                {
                    var ngram    = string.Join(" ", _words.Skip(i).Take(match.Length));
                    var entities = new List <EntityInfo>();
                    foreach (var ambigMatch in _matches[i])
                    {
                        if (ambigMatch.Length == match.Length)
                        {
                            entities.Add(ambigMatch.Entity);
                        }
                    }

                    linkedParts.Add(LinkedUtterancePart.Entity(ngram, entities.ToArray()));
                    i += match.Length - 1;
                }
            }

            return(new LinkedUtterance(linkedParts));
        }