How to check spelling in Hunspell with case insensitive

大憨熊 提交于 2020-01-24 09:41:53

问题


Hi I am making a desktop application (C#) that checks the spelling of the inputted word. I am using Hunspell which I added to my project using NuGet. I have 2 files the aff file and the dic file.

using (Hunspell english = new Hunspell("en_US.aff", "en_US.dic"))
{
    bool isExist = english.Spell("THesis");
}

isExist is equal to false because in my .dic file the correct spelling is "thesis". Even I use to .lower() and input proper names the isExist become false.

Can you help me in solving this?


回答1:


Given that Hunspell does not appear to support case-insensitive spelling checks, you might want to consider adapting your algorithm slightly:

Given THesis, you could try:

bool isExist = false;

using (Hunspell english = new Hunspell("en_US.aff", "en_US.dic"))
{
    TextInfo textInfo = new CultureInfo("en-US",false).TextInfo;
    isExist =      english.Spell("THesis") 
                 | english.Spell(textInfo.ToLower("THesis") 
                 | english.Spell(textInfo.ToUpper("THesis")) 
                 | english.Spell(textInfo.ToTitleCase("THesis"))
}

This will in turn logically check "THesis", "thesis", "THESIS" and "Thesis" and return true if any of those spellings is valid, courtesy of the logical OR operator.

Similarly for canada, this would work, as the ToTitleCase() method would at least guarantee a match.

This should work for most single words (including all caps acronyms).




回答2:


If you want the ToLower() call to ignore the first character, do it this way:

var textToCheck = "THesis".Substring(0, 1) + "THesis".Substring(1).ToLower();
bool isExist = english.Spell(textToCheck);

If this isn't what you're looking for, see below:


I'm not entirely sure what you want the implementation to look like, but this might help too. Using the "ToTitleCase" will take a string and capitalize the first character. Also, by calling the toLower() inside of the ToTitleCase call will ensure that only the first character is capitalized.

bool isExist = english.Spell(System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase("THesis".toLower());

You might need some sort of if statement as well to specify whether or not the call to ToTitleCase should be called on the current string or not. Is this what you're looking for?



来源:https://stackoverflow.com/questions/17882046/how-to-check-spelling-in-hunspell-with-case-insensitive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!