Ejemplo n.º 1
0
        public SearchResults SearchText(SearchSettings settings, string segment)
        {
            Segment s = new Sdl.LanguagePlatform.Core.Segment(_languageDirection.SourceCulture);

            s.Add(segment);
            return(SearchSegment(settings, s));
        }
Ejemplo n.º 2
0
        public SearchResults SearchText(SearchSettings settings, string segment)
        {
            var s = new Segment(_languageDirection.SourceCulture);

            s.Add(segment);
            return(SearchSegment(settings, s));
        }
 public SearchResults SearchText(SearchSettings settings, string segment)
 {
     Segment s = new Sdl.LanguagePlatform.Core.Segment(_languageDirection.SourceCulture);
     s.Add(segment);
     return SearchSegment(settings, s);
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates the translation unit as it is later shown in the Translation Results
        /// window of SDL Trados Studio. This member also determines the match score
        /// (in our implementation always 100%, as only exact matches are supported)
        /// as well as the confirmation lelvel, i.e. Translated.
        /// </summary>
        /// <param name="settings"></param>
        /// <param name="searchSegment"></param>
        /// <param name="searchResultSegment"></param>
        /// <param name="sourceSegment"></param>
        /// <param name="formattingPenalty"></param>
        /// <returns></returns>

        #region "CreateSearchResult"
        private SearchResult CreateSearchResult(SearchSettings settings, Segment searchSegment, Sdl.Community.Taus.TM.Provider.Segment.Segment searchResultSegment,
                                                string sourceSegment, bool formattingPenalty)
        {
            #region "TranslationUnit"
            var tu = new TranslationUnit();



            var searchSegmentSource = new Segment();
            var searchSegmentTarget = new Segment();



            searchSegmentSource.Add(searchResultSegment.SourceText);
            searchSegmentTarget.Add(searchResultSegment.TargetText);


            tu.SourceSegment = searchSegmentSource;
            tu.TargetSegment = searchSegmentTarget;

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

            #endregion


            #region "TuProperties"
            var score = Convert.ToInt32(searchResultSegment.MatchPercentage);
            tu.Origin = TranslationUnitOrigin.TM;

            #region  |  properties  |
            if (searchResultSegment.Industry.Name.Trim() != string.Empty)
            {
                var fieldValue = new MultiplePicklistFieldValue("industry");
                fieldValue.Add(searchResultSegment.Industry.Name + " [" + searchResultSegment.Industry.Id + "]");
                tu.FieldValues.Add(fieldValue);
            }
            if (searchResultSegment.ContentType.Name.Trim() != string.Empty)
            {
                var fieldValue = new MultiplePicklistFieldValue("contentType");
                fieldValue.Add(searchResultSegment.ContentType.Name + " [" + searchResultSegment.ContentType.Id + "]");
                tu.FieldValues.Add(fieldValue);
            }
            if (searchResultSegment.Owner.Name.Trim() != string.Empty)
            {
                var fieldValue = new MultiplePicklistFieldValue("owner");
                fieldValue.Add(searchResultSegment.Owner.Name + " [" + searchResultSegment.Owner.Id + "]");
                tu.FieldValues.Add(fieldValue);
            }
            if (searchResultSegment.Product.Name.Trim() != string.Empty)
            {
                var fieldValue = new MultiplePicklistFieldValue("product");
                fieldValue.Add(searchResultSegment.Product.Name + " [" + searchResultSegment.Product.Id + "]");
                tu.FieldValues.Add(fieldValue);
            }
            if (searchResultSegment.Provider.Name.Trim() != string.Empty)
            {
                var fieldValue = new MultiplePicklistFieldValue("provider");
                fieldValue.Add(searchResultSegment.Provider.Name + " [" + searchResultSegment.Provider.Id + "]");
                tu.FieldValues.Add(fieldValue);
            }

            #endregion



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


            if (settings.Mode == SearchMode.ConcordanceSearch)
            {
                searchSegmentSource.Tokens = Tokenize(searchSegmentSource);
                searchResult.ScoringResult.MatchingConcordanceRanges = CollectConcordanceMatchRanges(searchSegmentSource, _visitor.PlainText);
            }
            else if (settings.Mode == SearchMode.TargetConcordanceSearch)
            {
                searchSegmentTarget.Tokens = Tokenize(searchSegmentTarget);
                searchResult.ScoringResult.MatchingConcordanceRanges = CollectConcordanceMatchRanges(searchSegmentTarget, _visitor.PlainText);
            }

            var providerPenalty = settings.FindPenalty(PenaltyType.ProviderPenalty);
            if (providerPenalty != null && providerPenalty.Malus > 0)
            {
                var penalty = new Penalty(PenaltyType.ProviderPenalty, settings.FindPenalty(PenaltyType.ProviderPenalty).Malus);
                searchResult.ScoringResult.ApplyPenalty(penalty);
            }

            if (searchResult.ScoringResult.BaseScore >= settings.MinScore)
            {
                searchResult.TranslationProposal = searchResult.MemoryTranslationUnit;

                if (searchSegment.HasTags)
                {
                    #region "Draft"
                    tu.ConfirmationLevel = ConfirmationLevel.Draft;
                    #endregion

                    #region "FormattingPenalty"
                    var penalty = new Penalty(PenaltyType.MemoryTagsDeleted, settings.FindPenalty(PenaltyType.MemoryTagsDeleted).Malus);
                    searchResult.ScoringResult.ApplyPenalty(penalty);
                    #endregion
                }
                else
                {
                    tu.ConfirmationLevel = ConfirmationLevel.Translated;
                }
            }
            else
            {
                searchResult = null;
            }
            #endregion

            return(searchResult);
        }
        static Sdl.LanguagePlatform.Core.Segment TranslatedHtml2Segment(Sdl.LanguagePlatform.Core.Segment sourceSegment, string translatedText)
        {
            var htmlTagName       = "span"; // the only we feed for translation is span, so we expect the translation only has span tags too.
            var xmlFragment       = "<segment>" + translatedText + "</segment>";
            var xmlReader         = new System.Xml.XmlTextReader(xmlFragment, System.Xml.XmlNodeType.Element, null);
            var tagStack          = new Stack <Sdl.LanguagePlatform.Core.Tag>();
            var translatedSegment = new Sdl.LanguagePlatform.Core.Segment();

            try
            {
                while (xmlReader.Read())
                {
                    switch (xmlReader.NodeType)
                    {
                    case System.Xml.XmlNodeType.Element:
                        if (xmlReader.Name == htmlTagName)
                        {
                            var tagClass = xmlReader.GetAttribute("class");
                            var tagType  = (Sdl.LanguagePlatform.Core.TagType)
                                           Enum.Parse(typeof(Sdl.LanguagePlatform.Core.TagType), tagClass);
                            int id = Convert.ToInt32(xmlReader.GetAttribute("id"));
                            Sdl.LanguagePlatform.Core.Tag sourceTag = sourceSegment.FindTag(tagType, id);
                            if (tagType != Sdl.LanguagePlatform.Core.TagType.Standalone && !xmlReader.IsEmptyElement)
                            {
                                tagStack.Push(sourceTag);
                            }
                            translatedSegment.Add(sourceTag.Duplicate());
                            if (tagType != Sdl.LanguagePlatform.Core.TagType.Standalone && xmlReader.IsEmptyElement)
                            // the API translated <span></span> to <span/> (it does that if the tag is empty).
                            // must fetch the end tag as there is no EndElement to triger the next case block.
                            {
                                var endTag = sourceSegment.FindTag(Sdl.LanguagePlatform.Core.TagType.End, id);
                                translatedSegment.Add(endTag.Duplicate());
                            }
                        }
                        break;

                    case System.Xml.XmlNodeType.EndElement:
                    {
                        if (xmlReader.Name == htmlTagName)
                        {
                            var startTag = tagStack.Pop();
                            if (startTag.Type != Sdl.LanguagePlatform.Core.TagType.Standalone)
                            {
                                var endTag = sourceSegment.FindTag(
                                    Sdl.LanguagePlatform.Core.TagType.End, startTag.Anchor);
                                if (endTag != null)
                                {
                                    translatedSegment.Add(endTag.Duplicate());
                                }
                            }
                        }
                    }
                    break;

                    case System.Xml.XmlNodeType.Text:
                        translatedSegment.Add(xmlReader.Value);
                        break;

                    case System.Xml.XmlNodeType.Whitespace:
                        translatedSegment.Add(xmlReader.Value);
                        break;

                    default:
                        break;
                    }
                }
            }
            catch (Exception)
            {
                var    paintextSegment = new Sdl.LanguagePlatform.Core.Segment();
                string plaitext        = Regex.Replace(translatedText, "<[^>]+>", "");
                paintextSegment.Add(plaitext);
                return(paintextSegment);
            }

            return(translatedSegment);
        }