예제 #1
0
        private static IEnumerable <IReadOnlyList <string> > GenerateTranslations(ThotSmtParameters parameters,
                                                                                  IReadOnlyList <IReadOnlyList <string> > sourceCorpus)
        {
            var results = new IReadOnlyList <string> [sourceCorpus.Count];

            Parallel.ForEach(Partitioner.Create(0, sourceCorpus.Count), range =>
            {
                IntPtr smtModelHandle = IntPtr.Zero, decoderHandle = IntPtr.Zero;
                try
                {
                    smtModelHandle = Thot.LoadSmtModel(parameters);
                    decoderHandle  = Thot.LoadDecoder(smtModelHandle, parameters);
                    for (int i = range.Item1; i < range.Item2; i++)
                    {
                        IReadOnlyList <string> segment = sourceCorpus[i];
                        results[i] = Thot.DoTranslate(decoderHandle, Thot.decoder_translate, segment, false,
                                                      segment, (s, t, d) => t);
                    }
                }
                finally
                {
                    if (decoderHandle != IntPtr.Zero)
                    {
                        Thot.decoder_close(decoderHandle);
                    }
                    if (smtModelHandle != IntPtr.Zero)
                    {
                        Thot.smtModel_close(smtModelHandle);
                    }
                }
            });
            return(results);
        }
예제 #2
0
        public ThotSmtModel(ThotSmtParameters parameters)
        {
            Parameters = parameters;
            Parameters.Freeze();

            Handle = Thot.LoadSmtModel(Parameters);

            _directWordAlignmentModel = new ThotWordAlignmentModel(
                Thot.smtModel_getSingleWordAlignmentModel(Handle));
            _inverseWordAlignmentModel = new ThotWordAlignmentModel(
                Thot.smtModel_getInverseSingleWordAlignmentModel(Handle));
        }
예제 #3
0
            public override void Save()
            {
                foreach (ThotSmtEngine engine in _smtModel._engines)
                {
                    engine.CloseHandle();
                }
                Thot.smtModel_close(_smtModel.Handle);

                base.Save();

                _smtModel.Parameters = Parameters;
                _smtModel.Handle     = Thot.LoadSmtModel(_smtModel.Parameters);
                _smtModel._directWordAlignmentModel.Handle  = Thot.smtModel_getSingleWordAlignmentModel(_smtModel.Handle);
                _smtModel._inverseWordAlignmentModel.Handle =
                    Thot.smtModel_getInverseSingleWordAlignmentModel(_smtModel.Handle);
                foreach (ThotSmtEngine engine in _smtModel._engines)
                {
                    engine.LoadHandle();
                }
            }
예제 #4
0
        private IEnumerable <IList <TranslationInfo> > GetNBestLists(ThotSmtParameters parameters,
                                                                     IReadOnlyList <IReadOnlyList <string> > sourceCorpus)
        {
            IntPtr smtModelHandle = IntPtr.Zero;

            try
            {
                smtModelHandle = Thot.LoadSmtModel(parameters);
                var results = new IList <TranslationInfo> [sourceCorpus.Count];
                Parallel.ForEach(Partitioner.Create(0, sourceCorpus.Count), range =>
                {
                    IntPtr decoderHandle = IntPtr.Zero;
                    try
                    {
                        decoderHandle = Thot.LoadDecoder(smtModelHandle, parameters);
                        for (int i = range.Item1; i < range.Item2; i++)
                        {
                            IReadOnlyList <string> sourceSegment = sourceCorpus[i];
                            results[i] = Thot.DoTranslateNBest(decoderHandle, Thot.decoder_translateNBest, K,
                                                               sourceSegment, false, sourceSegment, CreateTranslationInfo).ToArray();
                        }
                    }
                    finally
                    {
                        if (decoderHandle != IntPtr.Zero)
                        {
                            Thot.decoder_close(decoderHandle);
                        }
                    }
                });
                return(results);
            }
            finally
            {
                if (smtModelHandle != IntPtr.Zero)
                {
                    Thot.smtModel_close(smtModelHandle);
                }
            }
        }