public void Approve_TwoSegmentsUnknownWord_LearnsUnknownWord() { using (var smtModel = new ThotSmtModel(TestHelpers.ToyCorpusConfigFileName)) using (IInteractiveSmtEngine engine = smtModel.CreateInteractiveEngine()) { using (IInteractiveTranslationSession session = engine.TranslateInteractively(1, "hablé con recepción .".Split())) { TranslationResult result = session.CurrentResults[0]; Assert.That(result.TargetSegment, Is.EqualTo("hablé with reception .".Split())); result = session.SetPrefix("i talked".Split(), true)[0]; Assert.That(result.TargetSegment, Is.EqualTo("i talked with reception .".Split())); session.SetPrefix("i talked with reception .".Split(), true); session.Approve(); } using (IInteractiveTranslationSession session = engine.TranslateInteractively(1, "hablé hasta cinco en punto .".Split())) { TranslationResult result = session.CurrentResults[0]; Assert.That(result.TargetSegment, Is.EqualTo("talked until five o ' clock .".Split())); } } }
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(); }