private SearchResult CreateSearchResult(Segment segment, Segment translation)
        {
            var tu = new TranslationUnit
            {
                SourceSegment = segment.Duplicate(),                //this makes the original source segment, with tags, appear in the search window
                TargetSegment = translation ?? new Segment()
            };

            tu.ResourceId = new PersistentObjectToken(tu.GetHashCode(), Guid.Empty);

            const int score = 0;             //score to 0...change if needed to support scoring

            tu.Origin = TranslationUnitOrigin.Nmt;

            var searchResult = new SearchResult(tu)
            {
                ScoringResult = new ScoringResult
                {
                    BaseScore = score
                },
                TranslationProposal = tu
            };

            tu.ConfirmationLevel = ConfirmationLevel.Draft;

            return(searchResult);
        }
 private bool IsSameSourceTarget(TranslationUnit corespondingTu)
 {
     if (corespondingTu.TargetSegment == null || corespondingTu.SourceSegment == null)
     {
         return(false);
     }
     return(corespondingTu.SourceSegment.ToString().Equals(corespondingTu.TargetSegment.ToString()));
 }
Exemple #3
0
        /// <summary>
        /// Creates a consumable SearchResult using the source segment and translated segment
        /// </summary>
        /// <param name="searchSegment"></param>
        /// <param name="translation"></param>
        /// <param name="sourceSegment"></param>
        /// <returns></returns>
        private static SearchResult CreateSearchResult(Segment searchSegment, Segment translation)
        {
            _logger.Trace("");
            var unit = new TranslationUnit
            {
                SourceSegment     = searchSegment,
                TargetSegment     = translation,
                ConfirmationLevel = ConfirmationLevel.Translated,
                Origin            = TranslationUnitOrigin.Nmt
            };

            unit.ResourceId = new PersistentObjectToken(unit.GetHashCode(), Guid.Empty);
            var searchResult = new SearchResult(unit);

            // We do not currently support scoring, so always say that we're 25% sure on this translation.
            searchResult.ScoringResult = new ScoringResult()
            {
                BaseScore = 25
            };

            return(searchResult);
        }
 public ImportResult UpdateTranslationUnit(TranslationUnit translationUnit)
 {
     throw new NotImplementedException();
 }
 public ImportResult AddTranslationUnit(TranslationUnit translationUnit, ImportSettings settings)
 {
     throw new NotImplementedException();
 }
 public SearchResults SearchTranslationUnit(SearchSettings settings, TranslationUnit translationUnit)
 {
     return(SearchSegment(settings, translationUnit.SourceSegment));
 }
Exemple #7
0
 /// <summary>
 /// Not required for this implementation. This is used to "train" the source, which if it's readonly
 /// doesn't work.
 /// </summary>
 /// <param name="translationUnit"></param>
 /// <returns></returns>
 public ImportResult UpdateTranslationUnit(TranslationUnit translationUnit)
 {
     _logger.Trace("");
     throw new NotImplementedException();
 }
        /// <summary>
        /// Translate an array of segments.
        /// </summary>
        /// <param name="settings"></param>
        /// <param name="segments">Array of segments to be translated (depending on the truthfulness of
        /// corresponding mask)</param>
        /// <param name="mask">Whether to translate a segment or not</param>
        /// <returns></returns>
        public SearchResults[] SearchSegments(SearchSettings settings, Segment[] segments, bool[] mask)
        {
            var results                   = new SearchResults[segments.Length];
            var beGlobalSegments          = new List <BeGlobalSegment>();
            var alreadyTranslatedSegments = new List <BeGlobalSegment>();

            if (!_options.ResendDrafts)
            {
                // Re-send draft segment logic
                for (var segmentIndex = 0; segmentIndex < segments.Length; segmentIndex++)
                {
                    if (mask != null && !mask[segmentIndex])
                    {
                        results[segmentIndex] = null;
                        continue;
                    }

                    var activeSegmentPair    = _editorController?.ActiveDocument?.ActiveSegmentPair;
                    var existsMergedSegments = CheckMergedSegments(results, activeSegmentPair, segmentIndex);
                    if (existsMergedSegments)
                    {
                        continue;
                    }

                    TranslationUnit correspondingTu = null;
                    if (segments.Length > _translationUnits.Count)
                    {
                        // Set translation unit based on the correct segment index: when 11 segments are sent as bulk,the first segment is ignored,
                        // because it was translated previewsly and the translation mask is false
                        correspondingTu = _translationUnits[segmentIndex - 1];
                    }
                    else
                    {
                        // Set translation unit based on segment index: when the first 10 segments are translated for the first time,
                        // then the TU index is the same as segment index
                        correspondingTu = _translationUnits[segmentIndex];
                    }

                    // If activeSegmentPair is not null, it means the user translates segments through Editor
                    // If activeSegmentPair is null, it means the user executes Pre-Translate Batch task, so he does not navigate through segments in editor
                    var documentLastOpenPath = _translationUnits[0]?.DocumentProperties?.LastOpenedAsPath;
                    if (documentLastOpenPath == null || (documentLastOpenPath != null && documentLastOpenPath.Equals(_editorController?.ActiveDocument?.ActiveFile?.LocalFilePath)))
                    {
                        if (activeSegmentPair != null && (activeSegmentPair.Target.Count > 0 || activeSegmentPair.Properties.IsLocked))
                        {
                            CreateTranslatedSegment(segments, segmentIndex, alreadyTranslatedSegments);
                        }
                        // In case user copies the source to target and run the pre-translation, do nothing and continue the flow.
                        else if (correspondingTu != null && IsSameSourceTarget(correspondingTu))
                        {
                            continue;
                        }
                        // If is already translated or is locked, then the request to server should not be done and it should not be translated
                        else if (activeSegmentPair == null && correspondingTu != null && (correspondingTu.DocumentSegmentPair.Target.Count > 0 || correspondingTu.DocumentSegmentPair.Properties.IsLocked))
                        {
                            CreateTranslatedSegment(segments, segmentIndex, alreadyTranslatedSegments);
                        }
                        else
                        {
                            CreateBeGlobalSegments(beGlobalSegments, segments, segmentIndex);
                        }
                    }
                    else
                    {
                        CreateBeGlobalSegments(beGlobalSegments, segments, segmentIndex);
                    }
                }
                if (beGlobalSegments.Count > 0)
                {
                    var hasTranslations = GetTranslations(beGlobalSegments);
                    if (hasTranslations)
                    {
                        SetSearchResults(results, beGlobalSegments);
                    }
                }
                if (alreadyTranslatedSegments.Count > 0)
                {
                    SetSearchResults(results, alreadyTranslatedSegments);
                }
            }
            else
            {
                var translations = TranslateSegments(segments.Where((seg, i) => mask == null || mask[i]).ToArray());

                if (translations.Any(translation => translation != null))
                {
                    var translationIndex = 0;
                    for (var i = 0; i < segments.Length; i++)
                    {
                        if (mask != null && !mask[i])
                        {
                            results[i] = null;
                            continue;
                        }
                        results[i] = new SearchResults();
                        if (segments[i] != null)
                        {
                            results[i].SourceSegment = segments[i].Duplicate();
                            results[i].Add(CreateSearchResult(segments[i], translations[translationIndex]));
                            translationIndex++;
                        }
                        else
                        {
                            results[i].SourceSegment = new Segment();
                            results[i].Add(CreateSearchResult(new Segment(), new Segment()));
                        }
                    }
                }
            }
            return(results);
        }