コード例 #1
0
 protected internal virtual bool ExtractAnnotation <_T0>(IList <_T0> source, CoreMapAggregator aggregator)
     where _T0 : ICoreMap
 {
     annotation   = aggregator.Merge(source, chunkOffsets.GetBegin(), chunkOffsets.GetEnd());
     charOffsets  = Interval.ToInterval(annotation.Get(typeof(CoreAnnotations.CharacterOffsetBeginAnnotation)), annotation.Get(typeof(CoreAnnotations.CharacterOffsetEndAnnotation)), Interval.IntervalOpenEnd);
     tokenOffsets = Interval.ToInterval(annotation.Get(typeof(CoreAnnotations.TokenBeginAnnotation)), annotation.Get(typeof(CoreAnnotations.TokenEndAnnotation)), Interval.IntervalOpenEnd);
     text         = annotation.Get(typeof(CoreAnnotations.TextAnnotation));
     extractFunc.Annotate(this, source.SubList(chunkOffsets.GetBegin(), chunkOffsets.GetEnd()));
     return(true);
 }
コード例 #2
0
        private bool ExtractAnnotation(ICoreMap sourceAnnotation, CoreMapAggregator aggregator)
        {
            Type tokensAnnotationKey = extractFunc.tokensAnnotationField;

            if (chunkOffsets != null)
            {
                annotation = aggregator.Merge((IList <ICoreMap>)sourceAnnotation.Get(tokensAnnotationKey), chunkOffsets.GetBegin(), chunkOffsets.GetEnd());
                if (sourceAnnotation.ContainsKey(typeof(CoreAnnotations.TextAnnotation)))
                {
                    ChunkAnnotationUtils.AnnotateChunkText(annotation, sourceAnnotation);
                }
                if (tokenOffsets != null)
                {
                    if (annotation.Get(typeof(CoreAnnotations.TokenBeginAnnotation)) == null)
                    {
                        annotation.Set(typeof(CoreAnnotations.TokenBeginAnnotation), tokenOffsets.GetBegin());
                    }
                    if (annotation.Get(typeof(CoreAnnotations.TokenEndAnnotation)) == null)
                    {
                        annotation.Set(typeof(CoreAnnotations.TokenEndAnnotation), tokenOffsets.GetEnd());
                    }
                }
                charOffsets  = Interval.ToInterval(annotation.Get(typeof(CoreAnnotations.CharacterOffsetBeginAnnotation)), annotation.Get(typeof(CoreAnnotations.CharacterOffsetEndAnnotation)));
                tokenOffsets = Interval.ToInterval(annotation.Get(typeof(CoreAnnotations.TokenBeginAnnotation)), annotation.Get(typeof(CoreAnnotations.TokenEndAnnotation)), Interval.IntervalOpenEnd);
            }
            else
            {
                int baseCharOffset = sourceAnnotation.Get(typeof(CoreAnnotations.CharacterOffsetBeginAnnotation));
                if (baseCharOffset == null)
                {
                    baseCharOffset = 0;
                }
                chunkOffsets = ChunkAnnotationUtils.GetChunkOffsetsUsingCharOffsets((IList <ICoreMap>)sourceAnnotation.Get(tokensAnnotationKey), charOffsets.GetBegin() + baseCharOffset, charOffsets.GetEnd() + baseCharOffset);
                ICoreMap annotation2 = aggregator.Merge((IList <ICoreMap>)sourceAnnotation.Get(tokensAnnotationKey), chunkOffsets.GetBegin(), chunkOffsets.GetEnd());
                annotation   = ChunkAnnotationUtils.GetAnnotatedChunkUsingCharOffsets(sourceAnnotation, charOffsets.GetBegin(), charOffsets.GetEnd());
                tokenOffsets = Interval.ToInterval(annotation.Get(typeof(CoreAnnotations.TokenBeginAnnotation)), annotation.Get(typeof(CoreAnnotations.TokenEndAnnotation)), Interval.IntervalOpenEnd);
                annotation.Set(tokensAnnotationKey, annotation2.Get(tokensAnnotationKey));
            }
            text = annotation.Get(typeof(CoreAnnotations.TextAnnotation));
            extractFunc.Annotate(this, (IList <ICoreMap>)annotation.Get(tokensAnnotationKey));
            return(true);
        }
コード例 #3
0
            public override ISequenceMatchResult <ICoreMap> Apply(ISequenceMatchResult <ICoreMap> matchResult, params int[] groups)
            {
                BasicSequenceMatchResult <ICoreMap> res = matchResult.ToBasicSequenceMatchResult();
                IList <ICoreMap> elements       = matchResult.Elements();
                IList <ICoreMap> mergedElements = new List <ICoreMap>();

                res.elements = mergedElements;
                int         last          = 0;
                int         mergedGroup   = 0;
                int         offset        = 0;
                IList <int> orderedGroups = CollectionUtils.AsList(groups);

                orderedGroups.Sort();
                foreach (int group in orderedGroups)
                {
                    int groupStart = matchResult.Start(group);
                    if (groupStart >= last)
                    {
                        // Add elements from last to start of group to merged elements
                        Sharpen.Collections.AddAll(mergedElements, elements.SubList(last, groupStart));
                        // Fiddle with matched group indices
                        for (; mergedGroup < group; mergedGroup++)
                        {
                            if (res.matchedGroups[mergedGroup] != null)
                            {
                                res.matchedGroups[mergedGroup].matchBegin -= offset;
                                res.matchedGroups[mergedGroup].matchEnd   -= offset;
                            }
                        }
                        // Get merged element
                        int groupEnd = matchResult.End(group);
                        if (groupEnd - groupStart >= 1)
                        {
                            ICoreMap merged = aggregator.Merge(elements, groupStart, groupEnd);
                            mergedElements.Add(merged);
                            last = groupEnd;
                            // Fiddle with matched group indices
                            res.matchedGroups[mergedGroup].matchBegin = mergedElements.Count - 1;
                            res.matchedGroups[mergedGroup].matchEnd   = mergedElements.Count;
                            mergedGroup++;
                            while (mergedGroup < res.matchedGroups.Length)
                            {
                                if (res.matchedGroups[mergedGroup] != null)
                                {
                                    if (res.matchedGroups[mergedGroup].matchBegin == matchResult.Start(group) && res.matchedGroups[mergedGroup].matchEnd == matchResult.End(group))
                                    {
                                        res.matchedGroups[mergedGroup].matchBegin = res.matchedGroups[group].matchBegin;
                                        res.matchedGroups[mergedGroup].matchEnd   = res.matchedGroups[group].matchEnd;
                                    }
                                    else
                                    {
                                        if (res.matchedGroups[mergedGroup].matchEnd <= matchResult.End(group))
                                        {
                                            res.matchedGroups[mergedGroup] = null;
                                        }
                                        else
                                        {
                                            break;
                                        }
                                    }
                                }
                                mergedGroup++;
                            }
                            offset = matchResult.End(group) - res.matchedGroups[group].matchEnd;
                        }
                    }
                }
                // Add rest of elements
                Sharpen.Collections.AddAll(mergedElements, elements.SubList(last, elements.Count));
                // Fiddle with matched group indices
                for (; mergedGroup < res.matchedGroups.Length; mergedGroup++)
                {
                    if (res.matchedGroups[mergedGroup] != null)
                    {
                        res.matchedGroups[mergedGroup].matchBegin -= offset;
                        res.matchedGroups[mergedGroup].matchEnd   -= offset;
                    }
                }
                return(res);
            }