Exemplo n.º 1
0
        public WordGraph GetWordGraph(IReadOnlyList <string> segment)
        {
            CheckDisposed();

            IntPtr nativeSentence     = Thot.ConvertStringsToNativeUtf8(segment);
            IntPtr wordGraph          = IntPtr.Zero;
            IntPtr nativeWordGraphStr = IntPtr.Zero;

            try
            {
                wordGraph = Thot.decoder_getWordGraph(_decoderHandle, nativeSentence);

                uint len = Thot.wg_getString(wordGraph, IntPtr.Zero, 0);
                nativeWordGraphStr = Marshal.AllocHGlobal((int)len);
                Thot.wg_getString(wordGraph, nativeWordGraphStr, len);
                string wordGraphStr      = Thot.ConvertNativeUtf8ToString(nativeWordGraphStr, len);
                double initialStateScore = Thot.wg_getInitialStateScore(wordGraph);
                return(CreateWordGraph(segment, wordGraphStr, initialStateScore));
            }
            finally
            {
                if (nativeWordGraphStr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(nativeWordGraphStr);
                }
                if (wordGraph != IntPtr.Zero)
                {
                    Thot.wg_destroy(wordGraph);
                }
                Marshal.FreeHGlobal(nativeSentence);
            }
        }