public IEnumerable <TextClassificationTag> GetTags(TextClassifierContext context) { if (!context.Colorize) { yield break; } foreach (var spanData in context.Colors) { var ct = spanData.Data as IClassificationType ?? themeClassificationTypeService.GetClassificationType(spanData.Data as TextColor? ?? TextColor.Text); yield return(new TextClassificationTag(spanData.Span, ct)); } }
public IEnumerable <TextClassificationTag> GetTags(TextClassifierContext context) { if (!context.Colorize) { yield break; } foreach (var spanData in context.Colors) { var ct = ColorUtils.GetClassificationType(classificationTypeRegistryService, themeClassificationTypeService, spanData.Data); yield return(new TextClassificationTag(spanData.Span, ct)); } }
public IEnumerable <TextClassificationTag> GetTags(TextClassifierContext context) { var list = new List <TextClassificationTag>(); int textLength = context.Text.Length; foreach (var classifier in textClassifiers) { foreach (var tagTmp in classifier.GetTags(context)) { var tag = tagTmp; if (tag.Span.End > textLength) { tag = new TextClassificationTag(Span.FromBounds(Math.Min(textLength, tag.Span.Start), Math.Min(textLength, tag.Span.End)), tag.ClassificationType); } if (tag.Span.Length == 0) { continue; } list.Add(tag); } } if (list.Count <= 1) { return(list); } list.Sort(TextClassificationTagComparer.Instance); // Common case if (!HasOverlaps(list)) { return(Merge(list)); } int min = 0; int minOffset = 0; var newList = new List <TextClassificationTag>(); var ctList = new List <IClassificationType>(); while (min < list.Count) { while (min < list.Count && minOffset >= list[min].Span.End) { min++; } if (min >= list.Count) { break; } var cspan = list[min]; minOffset = Math.Max(minOffset, cspan.Span.Start); int end = cspan.Span.End; ctList.Clear(); ctList.Add(cspan.ClassificationType); for (int i = min + 1; i < list.Count; i++) { cspan = list[i]; int cspanStart = cspan.Span.Start; if (cspanStart > minOffset) { if (cspanStart < end) { end = cspanStart; } break; } int cspanEnd = cspan.Span.End; if (minOffset >= cspanEnd) { continue; } if (cspanEnd < end) { end = cspanEnd; } if (!ctList.Contains(cspan.ClassificationType)) { ctList.Add(cspan.ClassificationType); } } Debug.Assert(minOffset < end); var newSpan = new Span(minOffset, end - minOffset); var ct = ctList.Count == 1 ? ctList[0] : classificationTypeRegistryService.CreateTransientClassificationType(ctList); newList.Add(new TextClassificationTag(newSpan, ct)); minOffset = end; } Debug.Assert(!HasOverlaps(newList)); return(Merge(newList)); }
public FrameworkElement CreateTextElement(IClassificationFormatMap classificationFormatMap, TextClassifierContext context, string contentType, TextElementFlags flags) { if (classificationFormatMap == null) { throw new ArgumentNullException(nameof(classificationFormatMap)); } if (context == null) { throw new ArgumentNullException(nameof(context)); } if (contentType == null) { throw new ArgumentNullException(nameof(contentType)); } var ct = contentTypeRegistryService.GetContentType(contentType); if (ct == null) { throw new ArgumentException($"Invalid content type: {contentType}"); } ITextClassifierAggregator aggregator; if (!toAggregator.TryGetValue(ct, out aggregator)) { toAggregator.Add(ct, aggregator = textClassifierAggregatorService.Create(ct)); } try { tagsList.AddRange(aggregator.GetTags(context)); return(TextElementFactory.Create(classificationFormatMap, context.Text, tagsList, flags)); } finally { tagsList.Clear(); } }