Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate from NHunspell to WeCantSpell.Hunspell #309

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<PackageOutputPath>$(MSBuildThisFileDirectory)/artifacts</PackageOutputPath>
<SignAssembly>true</SignAssembly>
<WarningsAsErrors>NU1605;CS8002</WarningsAsErrors>
<WarningsAsErrors>NU1605</WarningsAsErrors>
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)/liblcm.snk</AssemblyOriginatorKeyFile>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
Expand Down
1 change: 1 addition & 0 deletions src/SIL.LCModel.Core/SIL.LCModel.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ SIL.LCModel.Core provides a base library with core functionality.</Description>
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
<PackageReference Include="System.Runtime.InteropServices.RuntimeInformation" Version="4.3.0" />
<PackageReference Include="vswhere" Version="2.8.4" PrivateAssets="all" />
<PackageReference Include="WeCantSpell.Hunspell" Version="5.0.0" />
<ProjectReference Include="..\CSTools\Tools\Tools.csproj" />
<ProjectReference Include="..\SIL.LCModel.Build.Tasks\SIL.LCModel.Build.Tasks.csproj" />
<ProjectReference Include="..\SIL.LCModel.Utils\SIL.LCModel.Utils.csproj" />
Expand Down
25 changes: 19 additions & 6 deletions src/SIL.LCModel.Core/SpellChecking/SpellEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ internal static SpellEngine Create(string affixPath, string dictPath, string exc
SpellEngine spellEngine = null;
try
{
if (Platform.IsWindows)
spellEngine = CreateSpellEngineWindows(affixPath, dictPath, exceptionPath);
else
spellEngine = CreateSpellEngineLinux(affixPath, dictPath, exceptionPath);
if (SpellingHelper.UseWeCantSpell)
{
spellEngine = new SpellEngineWeCantSpell(affixPath, dictPath, exceptionPath);
} else {
spellEngine = Platform.IsWindows ? CreateSpellEngineWindows(affixPath, dictPath, exceptionPath) : CreateSpellEngineLinux(affixPath, dictPath, exceptionPath);
}

spellEngine.Initialize();
}
Expand Down Expand Up @@ -87,9 +89,20 @@ private void Initialize()
/// <inheritdoc />
public abstract bool Check(string word);

private bool _isVernacular;
private bool _gotIsVernacular;
public bool IsVernacular
{
get
{
if (_gotIsVernacular)
return _isVernacular;

/// <inheritdoc />
public abstract bool IsVernacular { get; }
_isVernacular = Check(SpellingHelper.PrototypeWord);
_gotIsVernacular = true;
return _isVernacular;
}
}

/// <inheritdoc />
public abstract ICollection<string> Suggest(string badWord);
Expand Down
16 changes: 0 additions & 16 deletions src/SIL.LCModel.Core/SpellChecking/SpellEngineLinux.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ namespace SIL.LCModel.Core.SpellChecking
internal sealed class SpellEngineLinux: SpellEngine
{
private IntPtr _hunspellHandle;
private bool _isVernacular;
private bool _gotIsVernacular;

internal SpellEngineLinux(string affixPath, string dictPath, string exceptionPath)
: base(exceptionPath)
Expand Down Expand Up @@ -218,20 +216,6 @@ public override ICollection<string> Suggest(string badWord)
Hunspell_free_list(_hunspellHandle, ref pointerToAddressStringArray, resultCount);
return results;
}

/// <inheritdoc />
public override bool IsVernacular
{
get
{
if (_gotIsVernacular)
return _isVernacular;

_isVernacular = Check(SpellingHelper.PrototypeWord);
_gotIsVernacular = true;
return _isVernacular;
}
}
}

/// <summary>
Expand Down
54 changes: 54 additions & 0 deletions src/SIL.LCModel.Core/SpellChecking/SpellEngineWeCantSpell.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using WeCantSpell.Hunspell;

namespace SIL.LCModel.Core.SpellChecking
{
internal class SpellEngineWeCantSpell: SpellEngine
{
private readonly WordList _wordList;
private readonly WordList.Builder _customWordsBuilder;
private WordList _customWordList;
private readonly HashSet<string> _badWords = new HashSet<string>();

public SpellEngineWeCantSpell(string affixPath, string dictPath, string exceptionPath) : base(exceptionPath)
{
_wordList = WordList.CreateFromFiles(dictPath, affixPath);
_customWordsBuilder = new WordList.Builder(_wordList.Affix);
_customWordList = _customWordsBuilder.ToImmutable();
}

public override bool Check(string word)
{
if (_badWords.Contains(word)) return false;
if (_customWordList.Check(word)) return true;
return _wordList.Check(word);
}

public override ICollection<string> Suggest(string badWord)
{
var suggestions = _wordList.Suggest(badWord).Union(_customWordList.Suggest(badWord));
return suggestions.Where(suggestion => !_badWords.Contains(suggestion)).ToArray();
}

protected override void SetStatusInternal(string word1, bool isCorrect)
{
// WeCantSpell does not support modifying the word list, so we have to use 2 and merge them.
if (isCorrect)
{
var detail = IsVernacular
? new WordEntryDetail(FlagSet.Empty,
MorphSet.Create(new []{SpellingHelper.PrototypeWord}),
WordEntryOptions.None)
: WordEntryDetail.Default;
_customWordsBuilder.Add(word1, detail);
_customWordList = _customWordsBuilder.ToImmutable();
}
else
{
_badWords.Add(word1);
}
}
}
}
16 changes: 0 additions & 16 deletions src/SIL.LCModel.Core/SpellChecking/SpellEngineWindows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ internal class NoLinuxRepack : System.Attribute
internal sealed class SpellEngineWindows: SpellEngine
{
private readonly Hunspell _hunspellHandle;
private bool _isVernacular;
private bool _gotIsVernacular;

internal SpellEngineWindows(string affixPath, string dictPath, string exceptionPath)
: base(exceptionPath)
Expand Down Expand Up @@ -87,20 +85,6 @@ public override ICollection<string> Suggest(string badWord)
return _hunspellHandle.Suggest(MarshallAsUtf8Bytes(badWord));
}

/// <inheritdoc />
public override bool IsVernacular
{
get
{
if (_gotIsVernacular)
return _isVernacular;

_isVernacular = Check(MarshallAsUtf8Bytes(SpellingHelper.PrototypeWord));
_gotIsVernacular = true;
return _isVernacular;
}
}

/// <summary>
/// We can't declare these arguments (char * in C++) as [MarshalAs(UnmanagedType.LPStr)] string, because that
/// unconditionally coverts the string to bytes using the current system code page, which is never what we want.
Expand Down
11 changes: 11 additions & 0 deletions src/SIL.LCModel.Core/SpellChecking/SpellingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ namespace SIL.LCModel.Core.SpellChecking
/// </summary>
public static class SpellingHelper
{
/// <summary>
/// FieldWorks uses NHunspell for spell checking, but NHunspell is not available on Linux.
/// Use this flag to switch between NHunspell and WeCantSpell as needed. On dotnet framework we use NHunspell by default.
/// On dotnet core we use WeCantSpell by default.
/// </summary>
public static bool UseWeCantSpell { get; set; } =
#if NETFRAMEWORK
false;
#else
true;
#endif
// A helper object used to ensure that the spelling engines are properly disposed of
private sealed class SingletonToDispose : IDisposable
{
Expand Down
Loading