Text Formatting/Highlighting in the Concordance Results window.

I am unable to locate a reference from the SDL SDK that helps me understand how to apply formatting on the (TranslationUnit) text that I send to the Concordance (and/or Lookup) result windows; can you please advise how to do this with an example?

I am assuming that I need to encapsulate the text/words that I want highlighted with a tag reference; after a day of routing around with this, I am not able to understand what tag reference is to be used to accomplish this from the TranslationUnit / SegmentElements.  I have completed development of returning TranslationUnits to the Concordance (and Lookup) result windows in Studio, but I would also like to format the individual words, as seen from the attached snapshot

Example:
If I do a concordance search for the word “Location”, then I would like to format the word “Location” with a yellow background in TranslationUnit Source that is returned to the Concordance result window.  Make reference to the attached snapshot.

Note: I am using the example “Translation Memory Provider example” that is provided with the SDK 2.0 to try understand this.  From this example provided by SDL, the results are returned in a TranslationUnit; I am assuming that it is possible to apply the font formatting on the SegmentElements in the TranslationUnit.SourceSegment, so that highlights/formats the words accordingly that are returned in the Concordance (and/or Lookup) result windows in Studio.

Thank you for your support,

Patrick.

  • The returned SearchResult.Tokens can provide word information about the segment, and searchResult.ScoringResult.MatchingConcordanceRanges can give information where the concordance matches. These information could be used in Concordance view pane to highlight the matches. Here I created a simple example to demonstrate how this works.

    I just modify the concordance search implementation in file ListTranslationProviderLanguageDirection.cs as the following:

    public SearchResults SearchText(SearchSettings settings, string searchString)
    {
        Segment s = new Sdl.LanguagePlatform.Core.Segment(_languageDirection.SourceCulture);
        s.Add(searchString);
        var searchResults = SearchSegment(settings, s);
        foreach (var searchResult in searchResults)
        {
            var segment = searchResult.MemoryTranslationUnit.SourceSegment;
            segment.Tokens = Tokenize(segment);
            searchResult.ScoringResult.MatchingConcordanceRanges = CollectConcordanceMatchRanges(segment, searchString);
        }
        return searchResults;
    }


    // a naive tokenizer implementation to treat a single text run as one token.
    private List<Token> Tokenize(Segment segment)
    {
        var tokens = new List<Token>();
        int run = 0;
        foreach (var element in segment.Elements)
        {
            if (element == null)
                continue;
            var text = element as Text;
            if (text != null && !string.IsNullOrEmpty(text.Value))
            {
                var token = new SimpleToken(text.Value);
                token.Span = new SegmentRange(run, 0, text.Value.Length -1);
                tokens.Add(token);
                run++;
            }
        }
        return tokens;
    }


    private List<SegmentRange> CollectConcordanceMatchRanges(Segment segment, string searchString)
    {
        var concordanceMatchRanges = new List<SegmentRange>();
        int searchLength = searchString.Length;
        int run = 0;
        foreach (var element in segment.Elements)
        {
            var text = element as Text;
            if (text != null && !string.IsNullOrEmpty(text.Value))
            {
                int index = text.Value.IndexOf(searchString, StringComparison.OrdinalIgnoreCase);
                while (index >= 0 && index < text.Value.Length )
                {
                    var segmentRange = new SegmentRange(run, index, index + searchLength -1);
                    concordanceMatchRanges.Add(segmentRange);
                           
                    index += searchLength;
                    if (index < text.Value.Length)
                        index = text.Value.IndexOf(searchString, index, StringComparison.OrdinalIgnoreCase);
                }
                run ++;
            }
        }
        return concordanceMatchRanges;
    }

    Then I test the concordance with this translation provider. I can get the result as shown in the screenshot:

     

  • Hi Xingzeng,

    thank you very much for this; really appreciated

    this is an excellent example of how to implement this type of functionality; I almost only needed to copy and paste the complete example into my code to get it to work :-)

    well done :-)

  • Hi,

    Do I understand correctly that this will only work if the searched string contains one word only? Or maybe several words but which are within the same element of the source segment?

    Thanks for the clarification.

    Regards,

    Laurent