【问题标题】:checking apostrophe in trie data structure检查特里数据结构中的撇号
【发布时间】:2017-03-11 22:43:53
【问题描述】:

我有一个字典文件(仅包含小写字母和撇号),它作为 trie 树加载。

我有一个检查函数,它检查文件中的单词是否存在于 trie 树中,不管字母大小写。

一切正常,除了撇号的单词总是被发现拼写错误。

这是我的功能

bool check(const char *word)
{
    // creat arrow to follow letters
    NODE* arrow = root;

    // for every letter in word
    for (int i = 0, length = strlen(word); i < length; i++)
    {
        int index;
        if(word[i] == '\'')
        {
           index = 26;
        } else {
              index = tolower(word[i]) - ASCII_DIFFERENCE;
        }

        // if NULL? creat a new one and move arrow to new child
        if (arrow->child[index] == NULL) 
        {
            return false;
        } else if (arrow->child[index] != NULL)  // not null?
        {
            arrow = arrow->child[index];
        } 
    }

    return arrow->isWord;
}  

我的结构:

typedef struct NODE
{
    bool isWord;
    struct NODE* child[ALPHA_SIZE];
} NODE;

定义

#define ASCII_DIFFERENCE 'a'

感谢任何帮助。

【问题讨论】:

  • 你能提供一个输入和期望输出的例子吗?和当前的输出。
  • 当然,世界“i'd”在字典中,但检查函数仍然返回 false 并将其视为拼写错误的单词@TonyTannous
  • 但是\' 没有什么特别之处,我不明白为什么它会造成问题。单词是否正确插入字典? (即,您是否正确地将其放入索引 26 中?)您可以在此 if/else 中放入一些打印语句并按照树遍历中的路径进行操作吗?也是ALPHA_SIZE = 27?
  • 谢谢大家我想通了,和往常一样,一个愚蠢的错误,在我的加载函数中我没有将撇号加载到我的树中,抱歉:))

标签: c spell-checking trie


【解决方案1】:

撇号是十六进制 27,十进制 39。我认为您使用了错误的幻数。

【讨论】:

  • 好吧,作为十进制 ti 的 39 但不管怎样,我将它放入索引 26 中,即字母大小 + 1 ,,,所以如果 a-a = 0 和 z-a=25 那么我想放撇号在 26 内
猜你喜欢
  • 1970-01-01
  • 2018-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多