gpt4 book ai didi

C++:检查单词是否拼写正确

转载 作者:搜寻专家 更新时间:2023-10-31 00:04:14 25 4
gpt4 key购买 nike

我正在寻找一种简单的方法来检查某个字符串是否是拼写正确的英文单词。例如,“looked”会返回 True,而“hurrr”会返回 False。我不需要拼写建议或任何拼写纠正功能。只是一个接受字符串并返回 bool 值的简单函数。

我可以使用 PyEnchant 使用 Python 轻松完成此操作,但如果您想将它与 MS Visual C++ 一起使用,您似乎必须自己编译该库。

最佳答案

PyEnchant 基于 Enchant ,这是一个提供 C 和 C++ 接口(interface)的 C 库。所以你可以只将它用于 C++。最小的例子是这样的:

#include <memory>
#include <cstdio>

#include "enchant.h"
#include "enchant++.h"

int main ()
{
try
{
enchant::Broker *broker = enchant::Broker::instance ();
std::auto_ptr<enchant::Dict> dict (broker->request_dict ("en_US"));

const char *check_checks[] = { "hello", "helllo" };
for (int i = 0; i < (sizeof (check_checks) / sizeof (check_checks[0])); ++i)
{
printf ("enchant_dict_check (%s): %d\n", check_checks[i],
dict->check (check_checks[i]) == false);
}

} catch (const enchant::Exception &) {
return 1;
}
}

有关更多示例/测试,请参阅他们的 SVN repository .

关于C++:检查单词是否拼写正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4521456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com