private void UpdateSuggestion()
        {
            TranslationSuggestion suggestion = _suggester.GetSuggestion(Prefix.Length, IsLastWordComplete, _curResult);

            Suggestion           = suggestion.TargetWordIndices.Select(j => _curResult.TargetSegment[j]).ToArray();
            SuggestionConfidence = suggestion.Confidence;
        }
Exemplo n.º 2
0
        private void WriteSuggestions(StreamWriter traceWriter, IInteractiveTranslationSession session,
                                      IReadOnlyList <TranslationSuggestion> suggestions)
        {
            if (traceWriter == null)
            {
                return;
            }

            for (int k = 0; k < suggestions.Count; k++)
            {
                TranslationSuggestion suggestion = suggestions[k];
                bool inSuggestion = false;
                traceWriter.Write($"SUGGESTION {k + 1}  ");
                for (int j = 0; j < session.CurrentResults[k].TargetSegment.Count; j++)
                {
                    if (suggestion.TargetWordIndices.Contains(j))
                    {
                        if (j > 0)
                        {
                            traceWriter.Write(" ");
                        }
                        if (!inSuggestion)
                        {
                            traceWriter.Write("[");
                        }
                        inSuggestion = true;
                    }
                    else if (inSuggestion)
                    {
                        traceWriter.Write("] ");
                        inSuggestion = false;
                    }
                    else if (j > 0)
                    {
                        traceWriter.Write(" ");
                    }

                    traceWriter.Write(session.CurrentResults[k].TargetSegment[j]);
                }
                if (inSuggestion)
                {
                    traceWriter.Write("]");
                }
                traceWriter.WriteLine();
            }
        }
Exemplo n.º 3
0
        private void TestSegment(IInteractiveSmtEngine engine, ITranslationSuggester suggester, int n,
                                 ParallelTextSegment segment, StreamWriter traceWriter)
        {
            traceWriter?.WriteLine($"Segment:      {segment.SegmentRef}");
            IReadOnlyList <string> sourceSegment = segment.SourceSegment.Preprocess(Preprocessors.Lowercase);

            traceWriter?.WriteLine($"Source:       {string.Join(" ", sourceSegment)}");
            IReadOnlyList <string> targetSegment = segment.TargetSegment.Preprocess(Preprocessors.Lowercase);

            traceWriter?.WriteLine($"Target:       {string.Join(" ", targetSegment)}");
            traceWriter?.WriteLine(new string('=', 120));
            string[][] prevSuggestionWords  = null;
            bool       isLastWordSuggestion = false;
            string     suggestionResult     = null;

            using (IInteractiveTranslationSession session = engine.TranslateInteractively(n, sourceSegment))
            {
                while (session.Prefix.Count < targetSegment.Count || !session.IsLastWordComplete)
                {
                    int targetIndex = session.Prefix.Count;
                    if (!session.IsLastWordComplete)
                    {
                        targetIndex--;
                    }

                    bool match = false;
                    TranslationSuggestion[] suggestions = suggester.GetSuggestions(session).ToArray();
                    string[][] suggestionWords          = suggestions.Select((s, k) =>
                                                                             s.TargetWordIndices.Select(j =>
                                                                                                        session.CurrentResults[k].TargetSegment[j]).ToArray()).ToArray();
                    if (prevSuggestionWords == null || !SuggestionsAreEqual(prevSuggestionWords, suggestionWords))
                    {
                        WritePrefix(traceWriter, suggestionResult, session.Prefix);
                        WriteSuggestions(traceWriter, session, suggestions);
                        suggestionResult = null;
                        if (suggestions.Any(s => s.TargetWordIndices.Count > 0))
                        {
                            _totalSuggestionCount++;
                        }
                    }
                    for (int k = 0; k < suggestions.Length; k++)
                    {
                        TranslationSuggestion suggestion = suggestions[k];
                        var accepted = new List <int>();
                        for (int i = 0, j = targetIndex; i < suggestionWords[k].Length && j < targetSegment.Count; i++)
                        {
                            if (suggestionWords[k][i] == targetSegment[j])
                            {
                                accepted.Add(suggestion.TargetWordIndices[i]);
                                j++;
                            }
                            else if (accepted.Count == 0)
                            {
                                j = targetIndex;
                            }
                            else
                            {
                                break;
                            }
                        }

                        if (accepted.Count > 0)
                        {
                            session.AppendSuggestionToPrefix(k, accepted);
                            isLastWordSuggestion = true;
                            _actionCount++;
                            _totalAcceptedSuggestionCount++;
                            if (accepted.Count == suggestion.TargetWordIndices.Count)
                            {
                                suggestionResult = "ACCEPT_FULL";
                                _fullSuggestionCount++;
                            }
                            else if (accepted[0] == suggestion.TargetWordIndices[0])
                            {
                                suggestionResult = "ACCEPT_INIT";
                                _initSuggestionCount++;
                            }
                            else if (accepted[accepted.Count - 1]
                                     == suggestion.TargetWordIndices[suggestion.TargetWordIndices.Count - 1])
                            {
                                suggestionResult = "ACCEPT_FIN";
                                _finalSuggestionCount++;
                            }
                            else
                            {
                                suggestionResult = "ACCEPT_MID";
                                _middleSuggestionCount++;
                            }
                            _acceptedSuggestionCounts[k]++;
                            match = true;
                            break;
                        }
                    }

                    if (!match)
                    {
                        if (isLastWordSuggestion)
                        {
                            _actionCount++;
                            isLastWordSuggestion = false;
                            WritePrefix(traceWriter, suggestionResult, session.Prefix);
                            suggestionResult = null;
                        }

                        int    len        = session.IsLastWordComplete ? 0 : session.Prefix[session.Prefix.Count - 1].Length;
                        string targetWord = targetSegment[targetIndex];
                        if (len == targetWord.Length)
                        {
                            session.AppendToPrefix("", true);
                        }
                        else
                        {
                            string c = targetWord.Substring(len, 1);
                            session.AppendToPrefix(c, false);
                        }

                        suggestionResult = suggestions.Any(s => s.TargetWordIndices.Count > 0) ? "REJECT" : "NONE";
                        _actionCount++;
                    }

                    prevSuggestionWords = suggestionWords;
                }

                WritePrefix(traceWriter, suggestionResult, session.Prefix);

                session.Approve(_approveAlignedOption.HasValue());
            }

            _charCount += targetSegment.Sum(w => w.Length + 1);
            traceWriter?.WriteLine();
        }