public WordAlignmentMatrix GetBestAlignment(IReadOnlyList <string> sourceSegment, IReadOnlyList <string> targetSegment) { CheckDisposed(); IntPtr nativeSourceSegment = Thot.ConvertStringsToNativeUtf8(sourceSegment); IntPtr nativeTargetSegment = Thot.ConvertStringsToNativeUtf8(targetSegment); IntPtr nativeMatrix = Thot.AllocNativeMatrix(sourceSegment.Count, targetSegment.Count); uint iLen = (uint)sourceSegment.Count; uint jLen = (uint)targetSegment.Count; try { Thot.swAlignModel_getBestAlignment(Handle, nativeSourceSegment, nativeTargetSegment, nativeMatrix, ref iLen, ref jLen); return(Thot.ConvertNativeMatrixToWordAlignmentMatrix(nativeMatrix, iLen, jLen)); } finally { Thot.FreeNativeMatrix(nativeMatrix, iLen); Marshal.FreeHGlobal(nativeTargetSegment); Marshal.FreeHGlobal(nativeSourceSegment); } }
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); } }
public void AddSegmentPair(IReadOnlyList <string> sourceSegment, IReadOnlyList <string> targetSegment, WordAlignmentMatrix hintMatrix = null) { CheckDisposed(); IntPtr nativeSourceSegment = Thot.ConvertStringsToNativeUtf8(sourceSegment); IntPtr nativeTargetSegment = Thot.ConvertStringsToNativeUtf8(targetSegment); IntPtr nativeMatrix = IntPtr.Zero; uint iLen = 0, jLen = 0; if (hintMatrix != null) { nativeMatrix = Thot.ConvertWordAlignmentMatrixToNativeMatrix(hintMatrix); iLen = (uint)hintMatrix.RowCount; jLen = (uint)hintMatrix.ColumnCount; } try { Thot.swAlignModel_addSentencePair(Handle, nativeSourceSegment, nativeTargetSegment, nativeMatrix, iLen, jLen); } finally { Thot.FreeNativeMatrix(nativeMatrix, iLen); Marshal.FreeHGlobal(nativeTargetSegment); Marshal.FreeHGlobal(nativeSourceSegment); } }
public double GetSegmentProbability(IEnumerable <string> segment) { IntPtr nativeSegment = Thot.ConvertStringsToNativeUtf8(segment); try { return(Thot.langModel_getSentenceProbability(_handle, nativeSegment)); } finally { Marshal.FreeHGlobal(nativeSegment); } }
public void AddSegmentPair(IReadOnlyList <string> sourceSegment, IReadOnlyList <string> targetSegment) { CheckDisposed(); IntPtr nativeSourceSegment = Thot.ConvertStringsToNativeUtf8(sourceSegment); IntPtr nativeTargetSegment = Thot.ConvertStringsToNativeUtf8(targetSegment); try { Thot.swAlignModel_addSentencePair(Handle, nativeSourceSegment, nativeTargetSegment); } finally { Marshal.FreeHGlobal(nativeTargetSegment); Marshal.FreeHGlobal(nativeSourceSegment); } }
private static void UpdateWeights(IntPtr weightUpdaterHandle, IReadOnlyList <IReadOnlyList <string> > tuneTargetCorpus, HashSet <TranslationInfo>[] nbestLists, float[] curWeights) { IntPtr[] nativeTuneTargetCorpus = tuneTargetCorpus.Select(Thot.ConvertStringsToNativeUtf8).ToArray(); int sizeOfPtr = Marshal.SizeOf <IntPtr>(); int sizeOfDouble = Marshal.SizeOf <double>(); IntPtr nativeNBestLists = Marshal.AllocHGlobal(nbestLists.Length * sizeOfPtr); IntPtr nativeScoreComps = Marshal.AllocHGlobal(nbestLists.Length * sizeOfPtr); var nativeNBestListLens = new uint[nbestLists.Length]; for (int i = 0; i < nbestLists.Length; i++) { IntPtr nativeNBestList = Marshal.AllocHGlobal(nbestLists[i].Count * sizeOfPtr); IntPtr nativeListScoreComps = Marshal.AllocHGlobal(nbestLists[i].Count * sizeOfPtr); int j = 0; foreach (TranslationInfo ti in nbestLists[i]) { IntPtr nativeSegment = Thot.ConvertStringsToNativeUtf8(ti.Translation); Marshal.WriteIntPtr(nativeNBestList, j * sizeOfPtr, nativeSegment); IntPtr nativeTransScoreComps = Marshal.AllocHGlobal((ti.ScoreComponents.Length - 1) * sizeOfDouble); Marshal.Copy(ti.ScoreComponents, 0, nativeTransScoreComps, ti.ScoreComponents.Length - 1); Marshal.WriteIntPtr(nativeListScoreComps, j * sizeOfPtr, nativeTransScoreComps); j++; } Marshal.WriteIntPtr(nativeNBestLists, i * sizeOfPtr, nativeNBestList); Marshal.WriteIntPtr(nativeScoreComps, i * sizeOfPtr, nativeListScoreComps); nativeNBestListLens[i] = (uint)nbestLists[i].Count; } try { Thot.llWeightUpdater_updateClosedCorpus(weightUpdaterHandle, nativeTuneTargetCorpus, nativeNBestLists, nativeScoreComps, nativeNBestListLens, curWeights, (uint)nbestLists.Length, (uint)curWeights.Length - 1); } finally { foreach (IntPtr nativeSegment in nativeTuneTargetCorpus) { Marshal.FreeHGlobal(nativeSegment); } for (int i = 0; i < nbestLists.Length; i++) { IntPtr nativeNBestList = Marshal.ReadIntPtr(nativeNBestLists, i * sizeOfPtr); IntPtr nativeListScoreComps = Marshal.ReadIntPtr(nativeScoreComps, i * sizeOfPtr); for (int j = 0; j < nbestLists[i].Count; j++) { IntPtr nativeSegment = Marshal.ReadIntPtr(nativeNBestList, j * sizeOfPtr); Marshal.FreeHGlobal(nativeSegment); IntPtr nativeTransScoreComps = Marshal.ReadIntPtr(nativeListScoreComps, j * sizeOfPtr); Marshal.FreeHGlobal(nativeTransScoreComps); } Marshal.FreeHGlobal(nativeNBestList); Marshal.FreeHGlobal(nativeListScoreComps); } Marshal.FreeHGlobal(nativeNBestLists); Marshal.FreeHGlobal(nativeScoreComps); } }