Пример #1
0
        //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
        //this feature 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, FiskmoOptions options)
        {
            var visitor = new FiskmoMarkupDataVisitor();
            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
                    FiskmöMTServiceHelper.Translate(options, sourceText, sourceCode, targetCode, options.modelTag);
                }
            }

            processedDocuments[langPair].Add(doc);
        }
Пример #2
0
        private List <SearchResult> GenerateSystemResult(string sourceText, SearchMode mode, Segment segment, string sourceCode, string targetCode)
        {
            List <SearchResult> systemResults = new List <SearchResult>();
            string translatedSentence         = FiskmöMTServiceHelper.Translate(this._options, sourceText, sourceCode, targetCode, this._options.modelTag);

            if (String.IsNullOrEmpty(translatedSentence))
            {
                return(systemResults);
            }

            // Look up the currently selected segment in the collection (normal segment lookup).
            if (mode == SearchMode.FullSearch || mode == SearchMode.NormalSearch)
            {
                Segment translation = new Segment(_languageDirection.TargetCulture);
                if (_visitor.Placeholders.Any() || _visitor.TagStarts.Any() || _visitor.TagEnds.Any())
                {
                    var split = Regex.Split(translatedSentence, @"\b(PLACEHOLDER|TAGPAIRSTART ?| ?TAGPAIREND)\b");

                    //Tag starts and ends must match, so need a stack to keep track of what tags
                    //have been applied
                    var tagStack = new Stack <Tag>();

                    foreach (var part in split)
                    {
                        //Remove potential spaces from after TAGPAIRSTARTS and before TAGPAIREND
                        var normalpart = part.Replace("TAGPAIRSTART ", "TAGPAIRSTART");
                        normalpart = normalpart.Replace(" TAGPAIREND", "TAGPAIREND");

                        switch (normalpart)
                        {
                        case "PLACEHOLDER":
                            if (_visitor.Placeholders.Count != 0)
                            {
                                translation.Add(_visitor.Placeholders.Dequeue());
                            }
                            break;

                        case "TAGPAIRSTART":
                            if (_visitor.TagStarts.Count != 0)
                            {
                                var startTag = _visitor.TagStarts.Dequeue();
                                tagStack.Push(startTag);
                                translation.Add(startTag);
                            }
                            break;

                        case "TAGPAIREND":
                            if (tagStack.Count != 0)
                            {
                                var correspondingStartTag = tagStack.Pop();
                                var endTag = _visitor.TagEnds[correspondingStartTag.TagID];
                                translation.Add(endTag);
                            }
                            break;

                        default:
                            translation.Add(part);
                            break;
                        }
                    }

                    //Insert missing end tags
                    foreach (var excessStartTag in tagStack)
                    {
                        var nonEndedTagIndex = translation.Elements.IndexOf(excessStartTag);
                        var endTag           = _visitor.TagEnds[excessStartTag.TagID];
                        translation.Elements.Insert(nonEndedTagIndex + 1, endTag);
                    }
                }
                else
                {
                    translation.Add(translatedSentence);
                }


                systemResults.Add(CreateSearchResult(segment, translation, segment.HasTags, "fiskmö"));
            }
            return(systemResults);
        }