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.

Parents
  • 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);

            }

Reply
  • 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);

            }

Children
No Data