Ejemplo n.º 1
0
        public IEnumerable <TranslationResult> Translate(int n, IReadOnlyList <string> segment)
        {
            IEnumerable <IEnumerable <WordAnalysis> > sourceAnalyses = segment
                                                                       .Select(word => _sourceAnalyzer.AnalyzeWord(word));

            foreach (TransferResult transferResult in _transferer.Transfer(sourceAnalyses).Take(n))
            {
                IReadOnlyList <WordAnalysis> targetAnalyses = transferResult.TargetAnalyses;
                WordAlignmentMatrix          waMatrix       = transferResult.WordAlignmentMatrix;

                var    translation = new List <string>();
                var    confidences = new List <double>();
                var    sources     = new List <TranslationSources>();
                var    alignment   = new WordAlignmentMatrix(segment.Count, targetAnalyses.Count);
                double confidence  = double.MaxValue;
                for (int j = 0; j < targetAnalyses.Count; j++)
                {
                    int[] sourceIndices = Enumerable.Range(0, waMatrix.RowCount)
                                          .Where(i => waMatrix[i, j]).ToArray();
                    string targetWord = targetAnalyses[j].IsEmpty
                                                ? null
                                                : _targetGenerator.GenerateWords(targetAnalyses[j]).FirstOrDefault();
                    double             wordConfidence = 1.0;
                    TranslationSources source         = TranslationSources.Transfer;
                    if (targetWord == null)
                    {
                        if (sourceIndices.Length > 0)
                        {
                            int i = sourceIndices[0];
                            targetWord      = segment[i];
                            wordConfidence  = 0;
                            source          = TranslationSources.None;
                            alignment[i, j] = true;
                        }
                    }
                    else
                    {
                        foreach (int i in sourceIndices)
                        {
                            alignment[i, j] = true;
                        }
                    }

                    if (targetWord != null)
                    {
                        translation.Add(targetWord);
                        confidences.Add(wordConfidence);
                        sources.Add(source);
                        confidence = Math.Min(confidence, wordConfidence);
                    }
                }

                yield return(new TranslationResult(segment, translation, confidences, sources, alignment,
                                                   new[] { new Phrase(Range <int> .Create(0, segment.Count), translation.Count, confidence) }));
            }
        }
        public override void Translate(ITranslationSession translationSession)
        {
            using (var client = new TerminologyClient(_binding, _endpoint))
            {
                var translationSources = new TranslationSources()
                {
                    TranslationSource.UiStrings
                };
                foreach (var item in translationSession.Items)
                {
                    if (translationSession.IsCanceled)
                    {
                        break;
                    }

                    Contract.Assume(item != null);

                    var targetCulture = item.TargetCulture.Culture ?? translationSession.NeutralResourcesLanguage;
                    if (targetCulture.IsNeutralCulture)
                    {
                        targetCulture = CultureInfo.CreateSpecificCulture(targetCulture.Name);
                    }

                    try
                    {
                        var response = client.GetTranslations(item.Source, translationSession.SourceLanguage.Name,
                                                              targetCulture.Name, SearchStringComparison.CaseInsensitive, SearchOperator.Contains,
                                                              translationSources, false, 5, false, null);

                        if (response != null)
                        {
                            translationSession.Dispatcher.BeginInvoke(() =>
                            {
                                Contract.Requires(item != null);
                                Contract.Requires(response != null);

                                foreach (var match in response)
                                {
                                    Contract.Assume(match != null);
                                    foreach (var trans in match.Translations)
                                    {
                                        item.Results.Add(new TranslationMatch(this, trans.TranslatedText, match.ConfidenceLevel / 100.0));
                                    }
                                }
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                        translationSession.AddMessage(DisplayName + ": " + ex.Message);
                        break;
                    }
                }
            }
        }
        static void Main(string[] args)
        {
            tc = new TerminologyClient();
            ts = new TranslationSources()
            {
                TranslationSource.Terms, TranslationSource.UiStrings
            };

            SearchStringComparison ssc = (args.Length > 3) && args[3] == "usecase" ? SearchStringComparison.CaseSensitive : SearchStringComparison.CaseInsensitive;

            tc.GetTranslationsCompleted += tc_GetTranslationsCompleted;
            tc.GetLanguagesCompleted    += tc_GetLanguagesCompleted;

            if (args.Length < 3)
            {
                tc.GetLanguagesAsync();
                waitForResults = true;
                while (waitForResults)
                {
                    Console.WriteLine("Waiting for languages retrieval results...\n");
                }

                string exeName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
                Console.WriteLine("\n\n\n\nUsage: " + exeName + " \"phrase\"" + " \"source lang\"" + " \"dest lang\"" + " \"");

                //Console.WriteLine("\n\n\n\nPress any key to exit...");
                //Console.ReadKey();
                return;
            }

            Console.WriteLine("Querying Translation Service...\n");

            tc.GetTranslationsAsync(args[0],
                                    args[1],
                                    args[2],
                                    ssc,
                                    SearchOperator.Contains,
                                    ts,
                                    false,
                                    20,
                                    true,
                                    null);

            waitForResults = true;
            while (waitForResults)
            {
                Console.WriteLine("Waiting for translation results...\n");
            }

            //Console.WriteLine("\n\n\n\nPress any key to exit...\n");
            //Console.ReadKey();
        }
        public TranslationResult ToResult(IReadOnlyList <string> sourceSegment, int prefixCount = 0)
        {
            double[] confidences         = _confidences.ToArray();
            var      sources             = new TranslationSources[Words.Count];
            var      alignment           = new WordAlignmentMatrix(sourceSegment.Count, Words.Count);
            var      phrases             = new List <Phrase>();
            int      trgPhraseStartIndex = 0;

            foreach (PhraseInfo phraseInfo in _phrases)
            {
                double confidence = double.MaxValue;
                for (int j = trgPhraseStartIndex; j < phraseInfo.TargetCut; j++)
                {
                    for (int i = phraseInfo.SourceSegmentRange.Start; i < phraseInfo.SourceSegmentRange.End; i++)
                    {
                        AlignmentType alignmentType = phraseInfo.Alignment[i - phraseInfo.SourceSegmentRange.Start,
                                                                           j - trgPhraseStartIndex];
                        if (alignmentType == AlignmentType.Aligned)
                        {
                            alignment[i, j] = AlignmentType.Aligned;
                        }
                    }

                    if (j < prefixCount)
                    {
                        sources[j] = TranslationSources.Prefix;
                        if (_uncorrectedPrefixWords.Contains(j))
                        {
                            sources[j] |= TranslationSources.Smt;
                        }
                    }
                    else if (_unknownWords.Contains(j))
                    {
                        sources[j] = TranslationSources.None;
                    }
                    else
                    {
                        sources[j] = TranslationSources.Smt;
                    }

                    confidence = Math.Min(confidence, Confidences[j]);
                }

                phrases.Add(new Phrase(phraseInfo.SourceSegmentRange, phraseInfo.TargetCut, confidence));
                trgPhraseStartIndex = phraseInfo.TargetCut;
            }

            return(new TranslationResult(sourceSegment, Words, confidences, sources, alignment, phrases));
        }
Ejemplo n.º 5
0
        private static TranslationResult CreateResult(int sourceLen, int prefixLen, string target,
                                                      params double[] confidences)
        {
            string[] targetArray = target.Split();
            var      targetConfidences = new double[targetArray.Length];
            var      targetSources = new TranslationSources[targetArray.Length];
            var      alignment = new WordAlignmentMatrix(sourceLen, targetArray.Length);
            int      i = 0, j = 0;
            double   phraseConfidence = double.MaxValue;

            foreach (double confidence in confidences)
            {
                if (j < prefixLen)
                {
                    targetSources[j] = TranslationSources.Prefix;
                }

                if (confidence >= 0)
                {
                    alignment[i, j]      = true;
                    targetConfidences[j] = confidence;
                    if (confidence > 0)
                    {
                        targetSources[j] |= TranslationSources.Smt;
                    }
                    i++;
                    j++;
                }
                else if (targetArray.Length > sourceLen)
                {
                    targetConfidences[j] = confidence;
                    j++;
                }
                else if (targetArray.Length < sourceLen)
                {
                    i++;
                }
                else
                {
                    throw new ArgumentException("A confidence was incorrectly set below 0.", nameof(confidences));
                }

                phraseConfidence = Math.Min(phraseConfidence, confidence);
            }
            return(new TranslationResult(Enumerable.Range(0, sourceLen).Select(index => index.ToString()), targetArray,
                                         targetConfidences, targetSources, alignment,
                                         new[] { new Phrase(Range <int> .Create(0, sourceLen), targetArray.Length, phraseConfidence) }));
        }
        public override void Translate(ITranslationSession translationSession)
        {
            using (var client = new TerminologyClient(_binding, _endpoint))
            {
                var translationSources = new TranslationSources {
                    TranslationSource.UiStrings
                };

                foreach (var item in translationSession.Items)
                {
                    if (translationSession.IsCanceled)
                    {
                        break;
                    }

                    var targetCulture = item.TargetCulture.Culture ?? translationSession.NeutralResourcesLanguage;
                    if (targetCulture.IsNeutralCulture)
                    {
                        targetCulture = CultureInfo.CreateSpecificCulture(targetCulture.Name);
                    }

                    try
                    {
                        var response = client.GetTranslations(item.Source, translationSession.SourceLanguage.Name,
                                                              targetCulture.Name, SearchStringComparison.CaseInsensitive, SearchOperator.Contains,
                                                              translationSources, false, 5, false, null);

                        if (response != null)
                        {
                            var matches = response
                                          .SelectMany(match => match?.Translations?.Select(trans => new TranslationMatch(this, trans?.TranslatedText, match.ConfidenceLevel / 100.0)))
                                          .Where(m => m?.TranslatedText != null)
                                          .Distinct(TranslationMatch.TextComparer);

                            translationSession.Dispatcher.BeginInvoke(() =>
                            {
                                item.Results.AddRange(matches);
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                        translationSession.AddMessage(DisplayName + ": " + ex.Message);
                        break;
                    }
                }
            }
        }
Ejemplo n.º 7
0
        public override async Task Translate(ITranslationSession translationSession)
        {
            using (var client = new TerminologyClient(_binding, _endpoint))
            {
                var translationSources = new TranslationSources {
                    TranslationSource.UiStrings
                };

                foreach (var item in translationSession.Items)
                {
                    if (translationSession.IsCanceled)
                    {
                        break;
                    }

                    var targetCulture = item.TargetCulture.Culture ?? translationSession.NeutralResourcesLanguage;
                    if (targetCulture.IsNeutralCulture)
                    {
                        targetCulture = CultureInfo.CreateSpecificCulture(targetCulture.Name);
                    }

                    try
                    {
                        var response = await client.GetTranslationsAsync(
                            item.Source, translationSession.SourceLanguage.Name,
                            targetCulture.Name, SearchStringComparison.CaseInsensitive, SearchOperator.Contains,
                            translationSources, false, 5, false, null)
                                       .ConfigureAwait(false);

                        if (response != null)
                        {
                            var matches = response
                                          .SelectMany(match => match?.Translations?.Select(trans => new TranslationMatch(this, trans?.TranslatedText, match.ConfidenceLevel / 100.0)))
                                          .Where(m => m?.TranslatedText != null)
                                          .Distinct(TranslationMatch.TextComparer);

#pragma warning disable CS4014 // Because this call is not awaited ... => just push out results, no need to wait.
                            translationSession.MainThread.StartNew(() => item.Results.AddRange(matches));
                        }
                    }
                    catch (Exception ex)
                    {
                        translationSession.AddMessage(DisplayName + ": " + ex.Message);
                        break;
                    }
                }
            }
        }
Ejemplo n.º 8
0
        protected override async Task Translate(ITranslationSession translationSession)
        {
            using (var client = new TerminologyClient(_binding, _endpoint))
            {
                var translationSources = new TranslationSources {
                    TranslationSource.UiStrings
                };

                foreach (var item in translationSession.Items)
                {
                    if (translationSession.IsCanceled)
                    {
                        break;
                    }

                    var targetCulture = item.TargetCulture.Culture ?? translationSession.NeutralResourcesLanguage;
                    if (targetCulture.IsNeutralCulture)
                    {
                        targetCulture = CultureInfo.CreateSpecificCulture(targetCulture.Name);
                    }

                    var response = await client.GetTranslationsAsync(
                        item.Source, translationSession.SourceLanguage.Name,
                        targetCulture.Name, SearchStringComparison.CaseInsensitive, SearchOperator.Contains,
                        translationSources, false, 5, false, null)
                                   .ConfigureAwait(false);

                    if (response != null)
                    {
                        var matches = response
                                      .SelectMany(match => match?.Translations?.Select(trans => new TranslationMatch(this, trans?.TranslatedText, Ranking * match.ConfidenceLevel / 100.0)))
                                      .Where(m => m?.TranslatedText != null)
                                      .Distinct(TranslationMatch.TextComparer);

                        await translationSession.MainThread.StartNew(() => item.Results.AddRange(matches)).ConfigureAwait(false);
                    }
                }
            }
        }
Ejemplo n.º 9
0
        private void button1_Click(object sender, EventArgs e)
        {
            // Create a collection to define the desired sources of translations
            TranslationSources translationSources = new TranslationSources()
            {
                TranslationSource.Terms, TranslationSource.UiStrings
            };
            // Create the proxy for the Terminology Service SOAP client
            TerminologyClient service = new TerminologyService.TerminologyClient();

            try
            {
                // Call GetTranslations to get the results
                Matches results = service.GetTranslations("hello", "en-US", "es-ES", SearchStringComparison.CaseInsensitive, SearchOperator.Contains, translationSources, false, 20, true, null);
                // Use the results
                foreach (Match match in results)
                {
                    MessageBox.Show(match.OriginalText + "----" + match.Translations[0].TranslatedText);
                }
            } catch (Exception ex)
            {
                // do nothing
            }
        }
Ejemplo n.º 10
0
 internal Matches GetTranslations(string v1, string v2, string v3, SearchOperator contains, TranslationSources translationSources, bool v4, int v5, bool v6, object p)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 11
0
        public TranslationSuggestion GetSuggestion(int prefixCount, bool isLastWordComplete,
                                                   TranslationResult result)
        {
            int startingJ = prefixCount;

            if (!isLastWordComplete)
            {
                // if the prefix ends with a partial word and it has been completed,
                // then make sure it is included as a suggestion,
                // otherwise, don't return any suggestions
                if ((result.WordSources[startingJ - 1] & TranslationSources.Smt) != 0)
                {
                    startingJ--;
                }
                else
                {
                    return(new TranslationSuggestion());
                }
            }

            int    lookaheadCount = ComputeLookahead(prefixCount, result);
            int    j             = startingJ;
            bool   inPhrase      = false;
            var    indices       = new List <int>();
            double minConfidence = -1;

            while (j < result.TargetSegment.Count && (lookaheadCount > 0 || inPhrase))
            {
                string word = result.TargetSegment[j];
                // stop suggesting at punctuation
                if (word.All(char.IsPunctuation))
                {
                    break;
                }

                // criteria for suggesting a word
                // the word must either:
                // - meet the confidence threshold
                // - come from a transfer engine
                double             confidence = result.WordConfidences[j];
                TranslationSources sources    = result.WordSources[j];
                if (confidence >= ConfidenceThreshold || (sources & TranslationSources.Transfer) != 0)
                {
                    indices.Add(j);
                    if (minConfidence < 0 || confidence < minConfidence)
                    {
                        minConfidence = confidence;
                    }
                    inPhrase = true;
                    lookaheadCount--;
                }
                else
                {
                    // skip over inserted words
                    if (result.Alignment.IsColumnAligned(j) == AlignmentType.Aligned)
                    {
                        lookaheadCount--;
                        // only suggest the first word/phrase we find
                        if (inPhrase)
                        {
                            break;
                        }
                    }
                }
                j++;
            }

            return(new TranslationSuggestion(indices, minConfidence < 0 ? 0 : minConfidence));
        }
Ejemplo n.º 12
0
        public TranslationResult Merge(int prefixCount, double threshold, TranslationResult otherResult)
        {
            var mergedTargetSegment = new List <string>();
            var mergedConfidences   = new List <double>();
            var mergedSources       = new List <TranslationSources>();
            var mergedAlignment     = new HashSet <Tuple <int, int> >();

            for (int j = 0; j < TargetSegment.Count; j++)
            {
                int[] sourceIndices = Alignment.GetColumnAlignedIndices(j).ToArray();
                if (sourceIndices.Length == 0)
                {
                    // target word doesn't align with anything
                    mergedTargetSegment.Add(TargetSegment[j]);
                    mergedConfidences.Add(WordConfidences[j]);
                    mergedSources.Add(WordSources[j]);
                }
                else
                {
                    // target word aligns with some source words
                    if (j < prefixCount || WordConfidences[j] >= threshold)
                    {
                        // use target word of this result
                        mergedTargetSegment.Add(TargetSegment[j]);
                        mergedConfidences.Add(WordConfidences[j]);
                        TranslationSources sources = WordSources[j];
                        foreach (int i in sourceIndices)
                        {
                            // combine sources for any words that both this result
                            // and the other result translated the same
                            foreach (int jOther in otherResult.Alignment.GetRowAlignedIndices(i))
                            {
                                TranslationSources otherSources = otherResult.WordSources[jOther];
                                if (otherSources != TranslationSources.None &&
                                    otherResult.TargetSegment[jOther] == TargetSegment[j])
                                {
                                    sources |= otherSources;
                                }
                            }

                            mergedAlignment.Add(Tuple.Create(i, mergedTargetSegment.Count - 1));
                        }
                        mergedSources.Add(sources);
                    }
                    else
                    {
                        // use target words of other result
                        bool found = false;
                        foreach (int i in sourceIndices)
                        {
                            foreach (int jOther in otherResult.Alignment.GetRowAlignedIndices(i))
                            {
                                // look for any translated words from other result
                                TranslationSources otherSources = otherResult.WordSources[jOther];
                                if (otherSources != TranslationSources.None)
                                {
                                    mergedTargetSegment.Add(otherResult.TargetSegment[jOther]);
                                    mergedConfidences.Add(otherResult.WordConfidences[jOther]);
                                    mergedSources.Add(otherSources);
                                    mergedAlignment.Add(Tuple.Create(i, mergedTargetSegment.Count - 1));
                                    found = true;
                                }
                            }
                        }

                        if (!found)
                        {
                            // the other result had no translated words, so just use this result's target word
                            mergedTargetSegment.Add(TargetSegment[j]);
                            mergedConfidences.Add(WordConfidences[j]);
                            mergedSources.Add(WordSources[j]);
                            foreach (int i in sourceIndices)
                            {
                                mergedAlignment.Add(Tuple.Create(i, mergedTargetSegment.Count - 1));
                            }
                        }
                    }
                }
            }

            var alignment = new WordAlignmentMatrix(SourceSegment.Count, mergedTargetSegment.Count);

            foreach (Tuple <int, int> t in mergedAlignment)
            {
                alignment[t.Item1, t.Item2] = true;
            }
            return(new TranslationResult(SourceSegment, mergedTargetSegment, mergedConfidences, mergedSources,
                                         alignment, Phrases));
        }
Ejemplo n.º 13
0
        public TranslationSuggestion GetSuggestion(int prefixCount, bool isLastWordComplete,
                                                   TranslationResult result)
        {
            int startingJ = prefixCount;

            if (!isLastWordComplete)
            {
                // if the prefix ends with a partial word and it has been completed,
                // then make sure it is included as a suggestion,
                // otherwise, don't return any suggestions
                if ((result.WordSources[startingJ - 1] & TranslationSources.Smt) != 0)
                {
                    startingJ--;
                }
                else
                {
                    return(new TranslationSuggestion());
                }
            }

            int k = 0;

            while (k < result.Phrases.Count && result.Phrases[k].TargetSegmentCut <= startingJ)
            {
                k++;
            }

            double minConfidence = -1;
            var    indices       = new List <int>();

            for (; k < result.Phrases.Count; k++)
            {
                Phrase phrase = result.Phrases[k];
                if (phrase.Confidence >= ConfidenceThreshold)
                {
                    bool hitBreakingWord = false;
                    for (int j = startingJ; j < phrase.TargetSegmentCut; j++)
                    {
                        string             word    = result.TargetSegment[j];
                        TranslationSources sources = result.WordSources[j];
                        if (sources == TranslationSources.None || word.All(char.IsPunctuation))
                        {
                            hitBreakingWord = true;
                            break;
                        }
                        indices.Add(j);
                    }
                    if (minConfidence < 0 || phrase.Confidence < minConfidence)
                    {
                        minConfidence = phrase.Confidence;
                    }
                    startingJ = phrase.TargetSegmentCut;
                    if (hitBreakingWord)
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }
            }

            return(new TranslationSuggestion(indices, minConfidence < 0 ? 0 : minConfidence));
        }