Exemple #1
0
        internal WordAlignmentMatrix GetHintMatrix(IReadOnlyList <string> sourceSegment,
                                                   IReadOnlyList <string> targetSegment, TranslationResult ruleResult)
        {
            TranslationResult smtResult    = SmtEngine.GetBestPhraseAlignment(sourceSegment, targetSegment);
            TranslationResult hybridResult = ruleResult == null ? smtResult : smtResult.Merge(targetSegment.Count,
                                                                                              RuleEngineThreshold, ruleResult);

            var matrix   = new WordAlignmentMatrix(sourceSegment.Count, targetSegment.Count, AlignmentType.Unknown);
            var iAligned = new HashSet <int>();

            for (int j = 0; j < targetSegment.Count; j++)
            {
                bool jAligned = false;
                if (j < hybridResult.WordSources.Count &&
                    (hybridResult.WordSources[j] & TranslationSources.Transfer) != 0)
                {
                    foreach (int i in hybridResult.Alignment.GetColumnAlignedIndices(j))
                    {
                        matrix[i, j] = AlignmentType.Aligned;
                        iAligned.Add(i);
                        jAligned = true;
                    }
                }

                if (jAligned)
                {
                    for (int i = 0; i < sourceSegment.Count; i++)
                    {
                        if (matrix[i, j] == AlignmentType.Unknown)
                        {
                            matrix[i, j] = AlignmentType.NotAligned;
                        }
                    }
                }
            }

            foreach (int i in iAligned)
            {
                for (int j = 0; j < targetSegment.Count; j++)
                {
                    if (matrix[i, j] == AlignmentType.Unknown)
                    {
                        matrix[i, j] = AlignmentType.NotAligned;
                    }
                }
            }

            return(matrix);
        }
Exemple #2
0
        public TranslationResult Translate(IReadOnlyList <string> segment)
        {
            CheckDisposed();

            TranslationResult smtResult = SmtEngine.Translate(segment);

            if (RuleEngine == null)
            {
                return(smtResult);
            }

            TranslationResult ruleResult = RuleEngine.Translate(segment);

            return(smtResult.Merge(0, RuleEngineThreshold, ruleResult));
        }
        public string[] UpdatePrefix(string prefix)
        {
            if (!IsInitialized)
            {
                throw new InvalidOperationException("The session has not been initialized.");
            }

            Range <int>[] tokenRanges = _engine.TargetWordTokenizer.Tokenize(prefix).ToArray();
            Prefix             = tokenRanges.Select(s => prefix.Substring(s.Start, s.Length)).ToArray();
            IsLastWordComplete = tokenRanges.Length == 0 || tokenRanges[tokenRanges.Length - 1].End != prefix.Length;

            TranslationResult smtResult = _wordGraphProcessor.Correct(Prefix, IsLastWordComplete, 1).FirstOrDefault();

            if (smtResult == null)
            {
                var builder = new TranslationResultBuilder();
                smtResult = builder.ToResult(SourceSegment, Prefix.Length);
            }

            if (RuleResult == null)
            {
                _curResult = smtResult;
            }
            else
            {
                int prefixCount = Prefix.Length;
                if (!IsLastWordComplete)
                {
                    prefixCount--;
                }

                _curResult = smtResult.Merge(prefixCount, RuleEngineThreshold, RuleResult);
            }

            UpdateSuggestion();

            return(Suggestion);
        }