Beispiel #1
0
 public FileReader(FinetuneBatchTaskSettings settings)
 {
     this.settings         = settings;
     this.FileTranslations = new List <Tuple <string, string> >();
     this.FileNewSegments  = new List <string>();
     this.sourceVisitor    = new OpusCatMarkupDataVisitor();
     this.targetVisitor    = new OpusCatMarkupDataVisitor();
 }
Beispiel #2
0
        //THIS IS DEPRECATED, REPLACED WITH SEGMENT CHANGE HANDLER EVENT
        //This function starts translating all segments in the document once the document is opened,
        //so that the translator won't have to wait for the translation to finish when opening a segment.
        //Note that Studio contains a feature called LookAhead which attempts to do a similar thing, but
        //LookAhead appears to be buggy with TMs etc., so it's better to rely on a custom caching system.
        private static void TranslateDocumentSegments(Document doc, LanguageDirection langPair, OpusCatOptions options)
        {
            var visitor = new OpusCatMarkupDataVisitor();
            EditorController editorController = SdlTradosStudio.Application.GetController <EditorController>();

            foreach (var segmentPair in doc.SegmentPairs)
            {
                if (segmentPair.Properties.ConfirmationLevel == Sdl.Core.Globalization.ConfirmationLevel.Unspecified)
                {
                    visitor.Reset();
                    segmentPair.Source.AcceptVisitor(visitor);
                    var sourceText = visitor.PlainText;

                    var sourceCode = langPair.SourceLanguage.CultureInfo.TwoLetterISOLanguageName;
                    var targetCode = langPair.TargetLanguage.CultureInfo.TwoLetterISOLanguageName;
                    var langpair   = $"{sourceCode}-{targetCode}";

                    //This will generate the translation and cache it for later use
                    OpusCatMTServiceHelper.Translate(options, sourceText, sourceCode, targetCode, options.modelTag);
                }
            }

            //processedDocuments[langPair].Add(doc);
        }
Beispiel #3
0
        private static void segmentChanged(LanguageDirection langDir, object sender, EventArgs e)
        {
            var doc = (Document)sender;

            //There are some "segments" the Trados editor view which are not proper segments, like
            //the start of document tag
            if (doc.ActiveSegmentPair == null)
            {
                return;
            }
            var visitor = new OpusCatMarkupDataVisitor();

            var activeOpusCatOptions = OpusCatProvider.GetProjectOpusCatOptions(doc.Project, langDir);

            IEnumerable <OpusCatOptions> activeOpusCatOptionsWithPregenerate;

            if (activeOpusCatOptions == null)
            {
                activeOpusCatOptionsWithPregenerate = null;
            }
            else
            {
                activeOpusCatOptionsWithPregenerate = activeOpusCatOptions.Where(x => x.pregenerateMt);
            }
            //If there is no active OPUS CAT provider, unsubscribe this handler (there's probably no event in Trados
            //API for removing a translation provider from a project, so this is the only way to unsubscribe
            //after translation provider has been removed.
            if (activeOpusCatOptionsWithPregenerate == null || !activeOpusCatOptionsWithPregenerate.Any())
            {
                OpusCatProvider.ClearSegmentHandlers();
                return;
            }

            var sourceSegmentTexts = new List <string>();

            var nextSegmentPairs = doc.SegmentPairs.SkipWhile(x =>
                                                              !(x.Properties.Id == doc.ActiveSegmentPair.Properties.Id &&
                                                                x.GetParagraphUnitProperties().ParagraphUnitId == doc.ActiveSegmentPair.GetParagraphUnitProperties().ParagraphUnitId));

            var segmentsNeeded = activeOpusCatOptionsWithPregenerate.Max(x => x.pregenerateSegmentCount);

            foreach (var segmentPair in nextSegmentPairs)
            {
                if (sourceSegmentTexts.Count == segmentsNeeded)
                {
                    break;
                }

                //Also preorder translations for Draft segments, since quite often there will be draft content
                //provided in segments where having MT is still desirable. This could also be an option.
                if (segmentPair.Properties.ConfirmationLevel == Sdl.Core.Globalization.ConfirmationLevel.Unspecified ||
                    segmentPair.Properties.ConfirmationLevel == Sdl.Core.Globalization.ConfirmationLevel.Draft)
                {
                    visitor.Reset();
                    segmentPair.Source.AcceptVisitor(visitor);
                    var sourceText = visitor.PlainText;
                    sourceSegmentTexts.Add(sourceText);
                }
            }

            var sourceCode = langDir.SourceLanguage.CultureInfo.TwoLetterISOLanguageName;
            var targetCode = langDir.TargetLanguage.CultureInfo.TwoLetterISOLanguageName;

            foreach (var options in activeOpusCatOptionsWithPregenerate)
            {
                //The preorder method doesn't wait for the translation, so the requests return quicker
                var sourceSegmentTextsNeeded = sourceSegmentTexts.Take(options.pregenerateSegmentCount).ToList();
                OpusCatMTServiceHelper.PreOrderBatch(options, sourceSegmentTextsNeeded, sourceCode, targetCode, options.modelTag);
            }
        }