How to do Translation Memory Lookups (SearchSegment method) with ISegement ?

Maybe someone has already asked about this question but I haven't found the answer yet.

Using Trados 2015 SDK, I have tried to search TM by source segment (ISegment) that come from segmentpair. (mixed with tag and string)

But the TM.LanguageDirection.SearchSegment(settings, segment) method required Sdl.LanguagePlatform.Core.Segment argument, I cannot use source segment (ISegment) directly.

So should I have to convert ISegment to Segment manually?

Element by element (tag, string, ...) ?

Or is there other simpler method to lookup TM by ISegment?

 

Thank you.

  • Hi Arthit,

    As you said, I think you have to convert the ISegment to a Segment manually.
    I would do something like the following code:
    github.com/.../PlainTextExtractor.cs

    However, instead of PlainText, you would convert to a Segment.
  • Thank you Jesse.
    I just hope that maybe there is the method in SDK that lookups TM by ISegment (active segmentpair) like Trados's standard function itself.
    I will try to create ISegment->Segment conversion function later.
  • Answer provided by Jesse is correct and it is exactly what Studio does internally. However I raised internally to expose Studio implementation as part of the API. Until that will happen the approach suggested by Jesse is your only option.
  • I have just finished coding of "ISegment to Segment" conversion function.

    I  modified sample code from Jesse and added tag conversion part.

    So the accuracy of fuzzy matching score is improved when there are tags in isegment.

    The code is below, by using function "ConvertToSegment(ISegment)" then it will return Segment.

    class ISegmentToSegmentConvert : IMarkupDataVisitor

       {

           public static Segment ConvertToSegment(ISegment segment)

           {

               ISegmentToSegmentConvert e = new ISegmentToSegmentConvert();

               e.VisitChildren(segment);

               return e.segmentNew;

           }

           public ISegmentToSegmentConvert()

           {

               AnchorCnt = 0;

               segmentNew = new Segment();

           }

           public Segment segmentNew

           {

               get;

               set;

           }

           public int AnchorCnt

           {

               get;

               set;

           }

           /// <summary>

           /// Iterates all sub items of container (IAbstractMarkupDataContainer)

           /// </summary>

           /// <param name="container"></param>

           private void VisitChildren(IEnumerable<IAbstractMarkupData> container)

           {

               foreach (var item in container)

               {

                   if (item is IStructureTag)

                   {

                       // skip structure tags

                       continue;

                   }

                   item.AcceptVisitor(this);

               }

           }

           #region IMarkupDataVisitor Members

           public void VisitCommentMarker(ICommentMarker commentMarker)

           {

               VisitChildren(commentMarker);

           }

           public void VisitLocationMarker(ILocationMarker location)

           {

           }

           public void VisitLockedContent(ILockedContent lockedContent)

           {

               VisitChildren(lockedContent.Content);

           }

           public void VisitOtherMarker(IOtherMarker marker)

           {

               VisitChildren(marker);

           }

           public void VisitPlaceholderTag(IPlaceholderTag tag)

           {

               AnchorCnt++;

               SegmentElement t = new Tag(TagType.Standalone, tag.TagProperties.TagId.ToString(), AnchorCnt, AnchorCnt, null);

               segmentNew.Add(t);

           }

           public void VisitRevisionMarker(IRevisionMarker revisionMarker)

           {

               //ignore

           }

           public void VisitSegment(ISegment segment)

           {

               VisitChildren(segment);

           }

           public void VisitTagPair(ITagPair tagPair)

           {

               AnchorCnt++;

               int n = AnchorCnt;

               SegmentElement t = new Tag(TagType.Start, tagPair.TagProperties.TagId.ToString(), n, n, null);

               segmentNew.Add(t);

               VisitChildren(tagPair);

               t = new Tag(TagType.End, tagPair.TagProperties.TagId.ToString(), n, n, null);

               segmentNew.Add(t);

           }

           public void VisitText(IText text)

           {

               segmentNew.Add(text.ToString());

           }

           #endregion

       }

    }

  • This is awesome thanks for posting. I would modify this slightly because when I try to perform a TM lookup using the Segment object that results from calling ConvertToSegment(), I get the TM complaining that the culture-info of my Segment does not match the culture info of the TM that I am using. So, I added a CultureInfo argument, to be used when instantiating new Segment(sourceLang);

    public static Segment ConvertToSegment(ISegment segment, CultureInfo sourceLang)

            {

                ISegmentToSegmentConvert e = new ISegmentToSegmentConvert(sourceLang);

                e.VisitChildren(segment);

                return e.segmentNew;

            }

            public ISegmentToSegmentConvert(CultureInfo sourceLang)

            {

                AnchorCnt = 0;

                segmentNew = new Segment(sourceLang);

            }