实施拼写检查算法 [英] implementing a spellcheck algorithm

查看:145
本文介绍了实施拼写检查算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/8512261/compare-two-text-files-spellchecking-program-in-c\">Compare两个文本文件 - 在C 拼写检查程序


我正在写一个拼写检查程序将比较用户的文本文件,借助字典,看看他们是否进入了词在字典中。

本字典遍历一次,然后它被卡住的最后一句话。我怎样才能通过字典循环一遍吗?

 的#include&LT;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&文件ctype.h GT;INT主要(无效)
{
    FILE * FP1,FP2 *; / *文件句柄* /
    烧焦userword [100];
    炭dictword [100];
    焦炭FNAME [40];
    INT I,J,CA,CB;    //的printf(请输入文件名要比较的词典:);
    //与fgets(FNAME,40,标准输入);
    // FP1 = FOPEN(FNAME,R);
    FP1 = FOPEN(userdoc.txt,R); / *打开文件进行读取,使用
                                      *时间,直到被输入名称解析* /
    FP2 = FOPEN(dictionary.txt,R);    如果(FP1 == NULL)
    {
            的printf(能为输出无法打开文件\\ n);
            返回0;
    }
    如果(FP2 == NULL)
    {
        的printf(无法读取\\ n打开%s,FNAME);
        出口(1); //终止程序
    }    对于(i = 0; userword [I];我++)
    {
        的fscanf(FP1,%S,&安培; userword);
        的printf(所检查的字为%s \\ n,userword);        J = GETC(FP2);
        而(dictword [J]!= EOF)
        {
            的fscanf(FP2,%S,&安培; dictword);
            / *的printf(在字典中的第一项为%s \\ n,dictword); //检查是否词典循环* /            如果(STRCMP(dictword,userword)== 0)
                {
                的printf(你拼写\\%s \\的正确的\\ n,dictword);
                打破;
            }
            其他
            {
                / *的printf(对不起\\%s \\中没有在字典中的\\ n,userword); * /
            }
        }
    }
    FCLOSE(FP1);
    FCLOSE(FP2);
    返回0;
}


解决方案

的直接原因,这是没有做任何事情都是下面一行:

 为(i = 0; userword [I];我++)

循环条件这里是一个值,即字符在指标值 I 。请注意,因为它的立场,你的程序从来没有这个数组初始化值(因此巴西莱Starynkevitch建议你用警告编译,例如-Wall -Wextra)。

如果你得到任何输出,它只是一个侥幸。你的userword []阵列中的值可能被填充有非零值,但它们也可能是在许多情况下为零。考虑一个有趣的事情是,一些调试环境(如GDB或MSVC)将有意地填充特殊值未初始化的内存区域,以便您更轻松地看到,当这样的事情发生。请参见这个例如

Sangeeth Saravanaraj试图你指出正确的方向。在答案,发现外循环是这样的:

 而(的fscanf(FP2,%S,wordcheck)!= EOF)//读取从文本文件单词到数组//

此循环做两件事情之一 - 它拷贝从 FP2 A线到 wordcheck 同时的检查,看是否能操作返回 EOF - 基本上是一个迹象表明,我们在文件的结尾。当我们到了最后,它打破了从while循环。

简单的改正你的循环不能完全解决你的程序,虽然。仔细想一下,你是如何通过字典中的每个字循环秒。您code只能在一个单独的userdoc'字工作,因为单次通过你的字典后,你会在该文件的末尾。你必须对所谓的文件指针复位到你的字典文件的开头,如果你想这个方法来工作:

 而(的fscanf(FP1,%S,和放大器;!userword)= EOF){    的printf(所检查的字为%s \\ n,userword);
    fseek的(fp2,0,0);
    而(的fscanf(FP2,%S,和放大器;!dictword)= EOF)
    {...}

Possible Duplicate:
Compare two text files - spellchecking program in C

I'm writing a spellcheck program that will compare a user's text file with a dictionary to see if the words they entered are in the dictionary.

The dictionary loops through once and then it gets stuck on the final word. How can I loop through the dictionary again?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int main (void) 
{ 
    FILE * fp1, *fp2;               /* file handle  */ 
    char userword[100]; 
    char dictword[100];
    char fname[40];
    int i, j, ca, cb; 

    //  printf("Enter filename to compare to dictionary:");
    //  fgets(fname,40,stdin);
    //  fp1 = fopen(fname,"r");
    fp1 = fopen("userdoc.txt", "r"); /* open file for reading, use for
                                      * time being until name input resolved*/  
    fp2 =fopen("dictionary.txt", "r");

    if (fp1 == NULL) 
    { 
            printf("Could not open file for output.\n"); 
            return 0; 
    } 
    if (fp2 == NULL)
    {
        printf("Cannot open %s for reading \n", fname);
        exit(1);        // terminate program
    }

    for (i=0; userword[i]; i++)  
    {               
        fscanf(fp1, "%s", &userword); 
        printf("The word being checked is %s\n", userword); 

        j=getc(fp2);
        while (dictword[j] != EOF)
        {
            fscanf(fp2, "%s", &dictword); 
            /*printf("The first entry in the dictionary is %s\n", dictword); //check if   dictionary is looping*/

            if(strcmp(dictword, userword) == 0)
                {
                printf("you spelt \"%s\" correctly \n", dictword);
                break; 
            }
            else    
            {   
                /*printf("sorry \" %s \" is not in the dictionary\n", userword);*/
            }
        }
    }
    fclose(fp1);
    fclose(fp2); 
    return 0; 
} 

解决方案

The direct reason why this is failing to do anything is the following line:

for (i=0; userword[i]; i++)  

The loop conditional here is a value, namely the char value at index i. Notice that, as it stands, your program never initializes the values in this array (hence Basile Starynkevitch suggesting you compile with warnings, e.g. -Wall -Wextra).

If you're getting any output at all, it's just a fluke. The values in your userword[] array might be filled with non-zero values, but they might also be zero in many situations. An interesting thing to consider is that some debugging environments (such as gdb or MSVC) will purposely fill uninitialized memory areas with special values so that you more easily see when this sort of thing happens. See this for example.

Sangeeth Saravanaraj is trying to point you in the right direction. In the answer there, notice that the outer loop looks like this:

while(fscanf(fp2,"%s", wordcheck)!=EOF)//Reads word from text file into array//

This loop does two things in one - it copies a line from fp2 into wordcheck, and also checks to see if this operation returned EOF - basically an indication that we're at the end of the file. When we're at the end, it breaks from the while loop.

Simply correcting your for loop doesn't completely fix your program, though. Think for a second about how you're looping through each word in the dictionary. Your code will only work on a single 'userdoc' word, because after a single pass through your dictionary, you'll be at the end of that file. You'll have to reset the so-called file pointer to the beginning of your dictionary file if you want this method to work:

while(fscanf(fp1,"%s", &userword)!=EOF){               

    printf("The word being checked is %s\n", userword); 
    fseek(fp2,0,0);
    while (fscanf(fp2, "%s", &dictword) != EOF)
    { ... }

这篇关于实施拼写检查算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆