Beispiel #1
0
        private static bool testCollision(EntityMatch match, EntityMatch u)
        {
            var first  = u.Position < match.Position ? u : match;
            var second = first == match ? u : match;

            var beginingOverlap = first.Position <second.Position && first.Position + first.Length> second.Position;
            var borderOverlap = first.Position == second.Position || first.Position + first.Length == second.Position + 1;

            return(beginingOverlap || borderOverlap);
        }
Beispiel #2
0
        private void initialize(UtteranceLinker linker, int entityHypCount)
        {
            for (var wordIndex = 0; wordIndex < _words.Length; ++wordIndex)
            {
                var missingWords = _words.Skip(wordIndex);
                for (var ngramLength = 1; ngramLength <= 4; ++ngramLength)
                {
                    if (wordIndex + ngramLength > _words.Length)
                    {
                        break;
                    }

                    var ngram = string.Join(" ", missingWords.Take(ngramLength));
                    foreach (var entity in linker.GetValidEntities(ngram, entityHypCount))
                    {
                        var match = new EntityMatch(entity, wordIndex, ngramLength);
                        _matches[wordIndex].Add(match);
                    }
                }
            }
        }
Beispiel #3
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));
        }