Beispiel #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);
            }
        }
        private string GetWord(uint index, IntPtr nativeWordStr, ref uint capacity)
        {
            uint len = GetWordNative(index, nativeWordStr, capacity);

            if (len > capacity)
            {
                capacity      = len;
                nativeWordStr = Marshal.ReAllocHGlobal(nativeWordStr, (IntPtr)capacity);
                len           = GetWordNative(index, nativeWordStr, capacity);
            }
            return(Thot.ConvertNativeUtf8ToString(nativeWordStr, len));
        }