/// <summary> /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// </summary> /// <param name="callFromDispose"> /// The call From Dispose. /// </param> public void Dispose(bool callFromDispose) { if (this.IsDisposed) { return; } IsDisposed = true; if (this.unmanagedHandle != IntPtr.Zero) { MarshalHunspellDll.HunspellFree(this.unmanagedHandle); this.unmanagedHandle = IntPtr.Zero; } if (this.nativeDllIsReferenced) { MarshalHunspellDll.UnReferenceNativeHunspellDll(); this.nativeDllIsReferenced = false; } if (callFromDispose) { GC.SuppressFinalize(this); } }
/// <summary> /// The dispose. /// </summary> public void Dispose() { if (!this.IsDisposed) { MarshalHunspellDll.HyphenFree(this.unmanagedHandle); this.unmanagedHandle = IntPtr.Zero; } }
/// <summary> /// Spell check the word. /// </summary> /// <param name="word"> /// The word. /// </param> /// <returns> /// <c>true</c> if word is correct, <c>false</c> otherwise /// </returns> public bool Spell(string word) { if (this.unmanagedHandle == IntPtr.Zero) { throw new InvalidOperationException("Dictionary is not loaded"); } return(MarshalHunspellDll.HunspellSpell(this.unmanagedHandle, word)); }
/// <summary> /// Hyphenates the specified word. /// </summary> /// <param name="word"> /// The word. /// </param> /// <returns> /// A <see cref="HyphenResult"/> object with data for simple and complex hyphenation /// </returns> public HyphenResult Hyphenate(string word) { if (this.unmanagedHandle == IntPtr.Zero) { throw new InvalidOperationException("Dictionary is not loaded"); } if (string.IsNullOrEmpty(word)) { return(null); } IntPtr buffer = MarshalHunspellDll.HyphenHyphenate(this.unmanagedHandle, word); IntPtr hyphenatedWord = Marshal.ReadIntPtr(buffer); int bufferOffset = IntPtr.Size; IntPtr hyphenationPoints = Marshal.ReadIntPtr(buffer, bufferOffset); bufferOffset += IntPtr.Size; IntPtr hyphenationRep = Marshal.ReadIntPtr(buffer, bufferOffset); bufferOffset += IntPtr.Size; IntPtr hyphenationPos = Marshal.ReadIntPtr(buffer, bufferOffset); bufferOffset += IntPtr.Size; IntPtr hyphenationCut = Marshal.ReadIntPtr(buffer, bufferOffset); bufferOffset += IntPtr.Size; var hyphenationPointsArray = new byte[Math.Max(word.Length - 1, 1)]; var hyphenationRepArray = new string[Math.Max(word.Length - 1, 1)]; var hyphenationPosArray = new int[Math.Max(word.Length - 1, 1)]; var hyphenationCutArray = new int[Math.Max(word.Length - 1, 1)]; for (int i = 0; i < word.Length - 1; ++i) { hyphenationPointsArray[i] = Marshal.ReadByte(hyphenationPoints, i); if (hyphenationRep != IntPtr.Zero) { IntPtr repString = Marshal.ReadIntPtr(hyphenationRep, i * IntPtr.Size); if (repString != IntPtr.Zero) { hyphenationRepArray[i] = Marshal.PtrToStringUni(repString); } hyphenationPosArray[i] = Marshal.ReadInt32(hyphenationPos, i * sizeof(int)); hyphenationCutArray[i] = Marshal.ReadInt32(hyphenationCut, i * sizeof(int)); } } var result = new HyphenResult(Marshal.PtrToStringUni(hyphenatedWord), hyphenationPointsArray, hyphenationRepArray, hyphenationPosArray, hyphenationCutArray); return(result); }
/// <summary> /// Adds the specified word to the internal dictionary. Determines the affixes from the provided sample. /// </summary> /// <param name="word"> /// The word in stem form /// </param> /// <param name="example"> /// The example in stem form /// </param> /// <returns> /// <c>true</c> if the word was successfully added, otherwise <c>false</c> /// </returns> /// <remarks> /// <para> /// The affixiation is determined by the example. The added word should have the stem form /// </para> /// <para> /// The word is NOT added to the dictionary file or data or stored in some way. /// It is only added to the internal data of the current /// <see cref="Hunspell"/> /// class. /// You must store your user dictionary elsewhere and Add() all words every time you create a new /// <see cref="Hunspell"/> /// object. /// </para> /// </remarks> /// <example> /// bool spellBefore = hunspell.Spell("phantasos"); spellBefore = hunspell.Spell("phantasoses"); add = hunspell.AddWithAffix("phantasos","fish"); // this fantasy word is affixed like the word fish ( plural es ...) spellAfter = hunspell.Spell("phantasos"); spellAfter = hunspell.Spell("phantasoses"); // the plural (like fish) is also correct /// </example> public bool AddWithAffix(string word, string example) { if (this.unmanagedHandle == IntPtr.Zero) { throw new InvalidOperationException("Dictionary is not loaded"); } MarshalHunspellDll.HunspellAddWithAffix(this.unmanagedHandle, word, example); return(this.Spell(word)); }
/// <summary> /// Initializes a new instance of the <see cref="Hunspell"/> class. /// </summary> /// <param name="affixData"> /// The affix data. (aff file data) /// </param> /// <param name="dictionaryData"> /// The dictionary data. (dic file Data) /// </param> /// <param name="key"> /// The key for encrypted dictionaries. /// </param> private void HunspellInit(byte[] affixData, byte[] dictionaryData, string key) { if (this.unmanagedHandle != IntPtr.Zero) { throw new InvalidOperationException("Dictionary is already loaded"); } MarshalHunspellDll.ReferenceNativeHunspellDll(); this.nativeDllIsReferenced = true; this.unmanagedHandle = MarshalHunspellDll.HunspellInit(affixData, new IntPtr(affixData.Length), dictionaryData, new IntPtr(dictionaryData.Length), key); }
/// <summary> /// The dispose. /// </summary> /// <param name="callFromDispose"> /// The call From Dispose. /// </param> protected virtual void Dispose(bool callFromDispose) { if (this.IsDisposed) { return; } IsDisposed = true; if (this.unmanagedHandle != IntPtr.Zero) { MarshalHunspellDll.HyphenFree(this.unmanagedHandle); this.unmanagedHandle = IntPtr.Zero; } if (this.nativeDllIsReferenced) { MarshalHunspellDll.UnReferenceNativeHunspellDll(); this.nativeDllIsReferenced = false; } }
/// <summary> /// Gets the word stems for the specified word. /// </summary> /// <param name="word"> /// The word. /// </param> /// <returns> /// List of word stems /// </returns> public List <string> Stem(string word) { if (this.unmanagedHandle == IntPtr.Zero) { throw new InvalidOperationException("Dictionary is not loaded"); } var result = new List <string>(); IntPtr strings = MarshalHunspellDll.HunspellStem(this.unmanagedHandle, word); int stringCount = 0; IntPtr currentString = Marshal.ReadIntPtr(strings, stringCount * IntPtr.Size); while (currentString != IntPtr.Zero) { ++stringCount; result.Add(Marshal.PtrToStringUni(currentString)); currentString = Marshal.ReadIntPtr(strings, stringCount * IntPtr.Size); } return(result); }
/// <summary> /// The init. /// </summary> /// <param name="dictionaryData"> /// The dictionary data. /// </param> private void Init(byte[] dictionaryData) { MarshalHunspellDll.LoadNativeHunspellDll(); this.unmanagedHandle = MarshalHunspellDll.HyphenInit(dictionaryData, new IntPtr(dictionaryData.Length)); }
/// <summary> /// Initializes a new instance of the <see cref="Hunspell"/> class. /// </summary> /// <param name="affixData"> /// The affix data. (aff file data) /// </param> /// <param name="dictionaryData"> /// The dictionary data. (dic file Data) /// </param> /// <param name="key"> /// The key for encrypted dictionaries. /// </param> private void HunspellInit(byte[] affixData, byte[] dictionaryData, string key) { MarshalHunspellDll.LoadNativeHunspellDll(); this.unmanagedHandle = MarshalHunspellDll.HunspellInit( affixData, new IntPtr(affixData.Length), dictionaryData, new IntPtr(dictionaryData.Length), key); }