Exemplo n.º 1
0
        public HybridInteractiveTranslationResult TranslateInteractively(IReadOnlyList <string> segment)
        {
            WordGraph         smtWordGraph = SmtEngine.GetWordGraph(segment);
            TranslationResult ruleResult   = RuleEngine?.Translate(segment);

            return(new HybridInteractiveTranslationResult(smtWordGraph, ruleResult));
        }
        private WordGraphConfidences ComputeWordGraphConfidences(WordGraph wordGraph)
        {
            double normalizationFactor = LogSpace.Zero;
            var    backwardProbs       = new double[wordGraph.Arcs.Count];

            for (int i = wordGraph.Arcs.Count - 1; i >= 0; i--)
            {
                WordGraphArc arc = wordGraph.Arcs[i];
                double       sum = LogSpace.One;
                if (!wordGraph.FinalStates.Contains(arc.NextState))
                {
                    sum = LogSpace.Zero;
                    foreach (int nextArcIndex in wordGraph.GetNextArcIndices(arc.NextState))
                    {
                        WordGraphArc nextArc = wordGraph.Arcs[nextArcIndex];
                        sum = LogSpace.Add(sum, LogSpace.Multiple(nextArc.Score, backwardProbs[nextArcIndex]));
                    }
                }
                backwardProbs[i] = sum;
                if (arc.PrevState == WordGraph.InitialState)
                {
                    normalizationFactor = LogSpace.Add(normalizationFactor,
                                                       LogSpace.Multiple(arc.Score, backwardProbs[i]));
                }
            }

            var rawWpps      = new Dictionary <string, Dictionary <int, double> >();
            var forwardProbs = new (double Prob, int Index)[wordGraph.Arcs.Count];
        public void Estimate(IReadOnlyList <string> sourceSegment, TranslationResultBuilder builder)
        {
            WordGraph            wordGraph            = _smtEngine.GetWordGraph(sourceSegment);
            WordGraphConfidences wordGraphConfidences = ComputeWordGraphConfidences(wordGraph);

            for (int j = 0; j < builder.Words.Count; j++)
            {
                builder.SetConfidence(j, wordGraphConfidences.GetConfidence(builder.Words[j]));
            }
        }
        public void Estimate(IReadOnlyList <string> sourceSegment, WordGraph wordGraph)
        {
            WordGraphConfidences wordGraphConfidences = ComputeWordGraphConfidences(wordGraph);

            foreach (WordGraphArc arc in wordGraph.Arcs)
            {
                for (int k = 0; k < arc.Words.Count; k++)
                {
                    arc.WordConfidences[k] = wordGraphConfidences.GetConfidence(arc.Words[k]);
                }
            }
        }
        public void Estimate(IReadOnlyList <string> sourceSegment, WordGraph wordGraph)
        {
            var range = Range <int> .Create(0, sourceSegment.Count);

            foreach (WordGraphArc arc in wordGraph.Arcs)
            {
                if (PhraseOnly)
                {
                    range = arc.SourceSegmentRange;
                }

                for (int k = 0; k < arc.Words.Count; k++)
                {
                    arc.WordConfidences[k] = GetConfidence(sourceSegment, range, arc.Words[k]);
                }
            }
        }
Exemplo n.º 6
0
        public ErrorCorrectionWordGraphProcessor(ErrorCorrectionModel ecm, IReadOnlyList <string> sourceSegment,
                                                 WordGraph wordGraph, double ecmWeight = 1, double wordGraphWeight = 1)
        {
            _ecm            = ecm;
            _sourceSegment  = sourceSegment;
            _wordGraph      = wordGraph;
            EcmWeight       = ecmWeight;
            WordGraphWeight = wordGraphWeight;

            _restScores           = _wordGraph.ComputeRestScores().ToArray();
            _stateEcmScoreInfos   = new List <EcmScoreInfo>();
            _arcEcmScoreInfos     = new List <List <EcmScoreInfo> >();
            _stateBestScores      = new List <List <double> >();
            _stateWordGraphScores = new List <double>();
            _stateBestPrevArcs    = new List <List <int> >();
            _statesInvolvedInArcs = new HashSet <int>();
            _prevPrefix           = new string[0];

            InitStates();
            InitArcs();
        }
        private static void TranslateInteractively_Success(Assert assert)
        {
            var httpClient = new MockHttpClient();
            var resultDto  = new InteractiveTranslationResultDto
            {
                WordGraph = new WordGraphDto
                {
                    InitialStateScore = -111.111f,
                    FinalStates       = new[] { 4 },
                    Arcs = new[]
                    {
                        new WordGraphArcDto
                        {
                            PrevState          = 0,
                            NextState          = 1,
                            Score              = -11.11f,
                            Words              = new[] { "This", "is" },
                            Confidences        = new[] { 0.4f, 0.5f },
                            SourceSegmentRange = new RangeDto {
                                Start = 0, End = 2
                            },
                            IsUnknown = false,
                            Alignment = new[]
                            {
                                new AlignedWordPairDto {
                                    SourceIndex = 0, TargetIndex = 0
                                },
                                new AlignedWordPairDto {
                                    SourceIndex = 1, TargetIndex = 1
                                }
                            }
                        },
                        new WordGraphArcDto
                        {
                            PrevState          = 1,
                            NextState          = 2,
                            Score              = -22.22f,
                            Words              = new[] { "a" },
                            Confidences        = new[] { 0.6f },
                            SourceSegmentRange = new RangeDto {
                                Start = 2, End = 3
                            },
                            IsUnknown = false,
                            Alignment = new[]
                            {
                                new AlignedWordPairDto {
                                    SourceIndex = 0, TargetIndex = 0
                                }
                            }
                        },
                        new WordGraphArcDto
                        {
                            PrevState          = 2,
                            NextState          = 3,
                            Score              = 33.33f,
                            Words              = new[] { "prueba" },
                            Confidences        = new[] { 0.0f },
                            SourceSegmentRange = new RangeDto {
                                Start = 3, End = 4
                            },
                            IsUnknown = true,
                            Alignment = new[]
                            {
                                new AlignedWordPairDto {
                                    SourceIndex = 0, TargetIndex = 0
                                }
                            }
                        },
                        new WordGraphArcDto
                        {
                            PrevState          = 3,
                            NextState          = 4,
                            Score              = -44.44f,
                            Words              = new[] { "." },
                            Confidences        = new[] { 0.7f },
                            SourceSegmentRange = new RangeDto {
                                Start = 4, End = 5
                            },
                            IsUnknown = false,
                            Alignment = new[]
                            {
                                new AlignedWordPairDto {
                                    SourceIndex = 0, TargetIndex = 0
                                }
                            }
                        }
                    }
                },
                RuleResult = new TranslationResultDto
                {
                    Target      = new[] { "Esto", "es", "una", "test", "." },
                    Confidences = new[] { 0.0f, 0.0f, 0.0f, 1.0f, 0.0f },
                    Sources     = new[]
                    {
                        TranslationSources.None,
                        TranslationSources.None,
                        TranslationSources.None,
                        TranslationSources.Transfer,
                        TranslationSources.None
                    },
                    Alignment = new[]
                    {
                        new AlignedWordPairDto {
                            SourceIndex = 0, TargetIndex = 0
                        },
                        new AlignedWordPairDto {
                            SourceIndex = 1, TargetIndex = 1
                        },
                        new AlignedWordPairDto {
                            SourceIndex = 2, TargetIndex = 2
                        },
                        new AlignedWordPairDto {
                            SourceIndex = 3, TargetIndex = 3
                        },
                        new AlignedWordPairDto {
                            SourceIndex = 4, TargetIndex = 4
                        }
                    }
                }
            };

            httpClient.Requests.Add(new MockRequest
            {
                Method       = HttpRequestMethod.Post,
                ResponseText = JsonConvert.SerializeObject(resultDto, RestClientBase.SerializerSettings)
            });

            var    engine = new TranslationEngine("http://localhost/", "project1", httpClient);
            Action done   = assert.Async();

            engine.TranslateInteractively("Esto es una prueba.", 0.2, session =>
            {
                assert.NotEqual(session, null);

                WordGraph wordGraph = session.SmtWordGraph;
                assert.Equal(wordGraph.InitialStateScore, -111.111);
                assert.DeepEqual(wordGraph.FinalStates.ToArray(), new[] { 4 });
                assert.Equal(wordGraph.Arcs.Count, 4);
                WordGraphArc arc = wordGraph.Arcs[0];
                assert.Equal(arc.PrevState, 0);
                assert.Equal(arc.NextState, 1);
                assert.Equal(arc.Score, -11.11);
                assert.DeepEqual(arc.Words.ToArray(), new[] { "This", "is" });
                assert.DeepEqual(arc.WordConfidences.ToArray(), new[] { 0.4, 0.5 });
                assert.Equal(arc.SourceSegmentRange.Start, 0);
                assert.Equal(arc.SourceSegmentRange.End, 2);
                assert.Equal(arc.IsUnknown, false);
                assert.Equal(arc.Alignment[0, 0], AlignmentType.Aligned);
                assert.Equal(arc.Alignment[1, 1], AlignmentType.Aligned);
                arc = wordGraph.Arcs[2];
                assert.Equal(arc.IsUnknown, true);

                TranslationResult ruleResult = session.RuleResult;
                assert.DeepEqual(ruleResult.TargetSegment.ToArray(), new[] { "Esto", "es", "una", "test", "." });
                assert.DeepEqual(ruleResult.WordConfidences.ToArray(), new[] { 0.0, 0.0, 0.0, 1.0, 0.0 });
                assert.DeepEqual(ruleResult.WordSources.ToArray(),
                                 new[]
                {
                    TranslationSources.None,
                    TranslationSources.None,
                    TranslationSources.None,
                    TranslationSources.Transfer,
                    TranslationSources.None
                });
                assert.Equal(ruleResult.Alignment[0, 0], AlignmentType.Aligned);
                assert.Equal(ruleResult.Alignment[1, 1], AlignmentType.Aligned);
                assert.Equal(ruleResult.Alignment[2, 2], AlignmentType.Aligned);
                assert.Equal(ruleResult.Alignment[3, 3], AlignmentType.Aligned);
                assert.Equal(ruleResult.Alignment[4, 4], AlignmentType.Aligned);
                done();
            });
        }