Exemple #1
0
        private static void UpdateSegmentHandler(Document doc)
#endif
        {
            //This method may be fired through docChanged event or through settings change.

            OpusCatProvider.ClearSegmentHandlers();

            OpusCatProvider.activeDocument = doc;

            var project     = doc.Project;
            var projectInfo = project.GetProjectInfo();

            LanguageDirection langDir;

            //Check whether document contains files
            if (doc.Files.Any())
            {
                //only files of same language can be merged, so taking the langdir of first file is enough
                langDir = doc.Files.First().GetLanguageDirection();
            }
            else
            {
                return;
            }

            var activeOpusCatOptions = OpusCatProvider.GetProjectOpusCatOptions(project, langDir);

            if (activeOpusCatOptions != null)
            {
                if (activeOpusCatOptions.Any(x => x.pregenerateMt))
                {
                    //The previous solution for pregeneration was to start translating the
                    //whole document as soon as the doc changes. This has a problem:
                    //if you have a massive document, just opening the document will cause a massive
                    //load on the translation service.
                    //So instead this was changed to add a segment changed handler which order only a certain
                    //amount on new translations for the next n segments whenever segment changes.
                    //Previous solution is provided below, commented out.

                    //Assign the handler to field to make it possible to remove it later
                    if (!OpusCatProvider.activeSegmentHandlers.ContainsKey(doc as Document))
                    {
                        OpusCatProvider.activeSegmentHandlers[doc as Document] = new List <EventHandler>();
                    }

                    var handler = new EventHandler((x, y) => segmentChanged(langDir, x, y));
                    OpusCatProvider.activeSegmentHandlers[doc as Document].Add(handler);

                    doc.ActiveSegmentChanged += handler;
                }
            }
            else
            {
                return;
            }
        }
Exemple #2
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);
            }
        }