Skip to content

Commit

Permalink
Added (some) support for case insensitive expand operations
Browse files Browse the repository at this point in the history
  • Loading branch information
abjerner committed Apr 12, 2021
1 parent 27118d6 commit e5858ac
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,30 @@ public class HunspellExpandOptions {
/// If specified, suggested words with a distance highter than the value of this property will be ignored.</remarks>
public int MaxDistance { get; set; }

/// <summary>
/// Gets or sets whether the expand should be case insensitive.
/// </summary>
/// <remarks>This option may not be fully implemented throughout the expand implementation.</remarks>
public bool CaseInsentive { get; set; }

#endregion

#region Constructors

/// <summary>
/// Initializes a new instance with default options.
/// </summary>
public HunspellExpandOptions() { }
public HunspellExpandOptions() {
CaseInsentive = true;
}

/// <summary>
/// Initializes a new instance based on the specified <paramref name="text"/>.
/// </summary>
/// <param name="text">Gets or sets the text. This will typically be the raw string the user enters into the search field.</param>
public HunspellExpandOptions(string text) {
Text = text;
CaseInsentive = true;
}

#endregion
Expand Down
37 changes: 36 additions & 1 deletion src/Skybrud.TextAnalysis/Hunspell/HunspellTextAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,44 @@ public virtual HunspellExpandResult Expand(HunspellExpandOptions options) {

}

// Get the suggestions for "piece"
string[] suggestions = Suggest(piece);

// If case insensitivity is enabled, we're looking for a suggestion that is spelled the same way,
// but with different casing. If a match is found, all other suggestions are ignored as they may
// lead to a higher amount of "unrelated" search results.
//
// This will typically be the case if the user enters a name in lowercase (eg. "ole" instead of "Ole"),
// which then technically is a spelling error. The same may be the case for abbreviations.
if (options.CaseInsentive) {

string insensitiveMatch = suggestions.FirstOrDefault(x => string.Equals(x, piece, StringComparison.InvariantCultureIgnoreCase));

if (insensitiveMatch != null) {

// Append the match with the correct casing
or.Append(insensitiveMatch);

// Iterate over the stem(s) of "suggestion"
foreach (HunspellStemResult stem in Stem(insensitiveMatch)) {

// Append each variant/morph to the list
foreach (string variant in Morph(stem)) {
or.Append(variant);
}

}

continue;

}

}

// Append the word as entered by the user
or.Append(piece);

foreach (string suggestion in Suggest(piece)) {
foreach (string suggestion in suggestions) {

// Calculate the Levenshtein distance
int distance = Levenshtein(piece, suggestion);
Expand Down

0 comments on commit e5858ac

Please sign in to comment.