Creating a spellchecker

This is a discussion on Creating a spellchecker within the C Programming forums, part of the General Programming Boards category; Im trying to create a spellchecker, iv created a reference file containing a list of words. I need to be ...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    13

    Post Creating a spellchecker

    Im trying to create a spellchecker, iv created a reference file containing a list of words. I need to be able to check text files against this and if the words are incorrect i want to b able to print these to a new file including the line it appears on and any words it actually could b. This is wot iv got so far, its not much coz im getting really confused:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    char** allocate(int str_length)
            {
            char** word_store;
            word_store = (char**)(malloc(str_length*sizeof(char*)));
            return word_store;
            }
    
    void de_allocate(char** word_store, int str_length)
            {
            int i;
            for (i=0; i<=str_length; i++)
            free(word_store[i]);
            free(word_store);
            }
    
    int main(void)
            {
            char buffer[100];
            char** word_store;
            int space=0, words=0;
            FILE* in_file;
    
    
            if((in_file=fopen("textfile.txt","r"))==NULL)   /*opening the file as named*/
            printf("File open failed\n");
    
            while(1)
                    {
                    if(fgets(buffer,100,in_file)==NULL) break;      /*scanning the file using buffer as temp store,
                                                              max of 100 characters per line*/
                    while(1)
                            {
                            if(isspace(buffer))                     /*if buffer is a space, increment 'space' which*/
                            space++;                                        /*will then give a number of words in the file*/
                            printf("%d ",space);
                            if(buffer == EOF) break;      /*this is where i got really confused*/
    
                    /*think i need to call on the allocate function here, then move on to scan
                    in the seperate words and store them into word_store*/
    
                    }
                    /*{
                    sscanf(buffer, "%s", word_store[]);
                    words++;
                    }*/
    
            }
            fclose(in_file);
    
    
    
            return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,439
    In main, you have
    Code:
    while ( fgets( buffer, sizeof buffer, in_file ) != NULL ) {
      extract_words( buffer );
    }
    Now write a function called extract_words
    Code:
    void extract_words ( char *buffer ) {
      char word[100];
      while ( /* some condition */ ) {
        /* some code */
        printf( "Word=%s\n", word );
      }
    }
    When you're reliably printing words, then you can replace the
    printf( "Word=%s\n", word );
    with
    spell_check( word );

    And so on, until the program is complete.

    In the meantime, you can also do
    Code:
    int main ( ) {
        spell_check( "good" );
        spell_check( "zoig" );
        return 0;
    }
    To allow you to test and debug the spelling checker without flooding your tests with a bunch of words from the file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    13
    Iv started writing the program again from stratch but im having problems coz i dont know how to use fgets to take in a line/word from a file.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    char line[40][1];
    int r,c;
            {
            FILE *game;   
            printf("Opening file...\n");
            if((game=fopen("reference.txt","r"))!=NULL) 
                    {
                    printf ("File open\n\n");
                    for(r=0;r<40;r++)    
                            {
                            for(c=0;c<1;c++)  
                                    {
                                    fscanf(game,"%c",&line[r][c]);//need to use fgets here but cant get it to work//
                                    printf("%c",line[r][c]);       
                                    } 
                            } 
                    printf("\n");
                    }
            else printf ("Error: File could not be opened\n");
            }
    }

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,439
    > i dont know how to use fgets to take in a line/word from a file
    I just showed you how to do it!!!

    Code:
    // copies file to stdout
    while ( fgets( buffer, sizeof buffer, in_file ) != NULL ) {
      puts( buffer );
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    >> coz i dont know how to use fgets to take in a line/word from a file.

    So download some documentation and study it, for christ sake! That's what it's there for.

    Perhaps you should actually *read* Salem's posts, by the way:

    >> while ( fgets( buffer, sizeof buffer, in_file ) != NULL )

    C'mon, now!


    [edit]
    I'm way too slow.
    [/edit]

  6. #6
    Registered User
    Join Date
    Jan 2004
    Posts
    13
    Ok laziness on my part partly over, iv managed to find out how to read a single line from a file but i really have no clue how to seperate each word and iv actually bothered searchin for this one properly, sry for my lack of effort:
    Code:
    #include <stdio.h>
    int main() 
            {
            FILE *ref;
            char line[100]; 
            ref = fopen ("reference.txt", "r");
            fgets (line, sizeof(line),ref);
            puts(line);
            fclose(ref);
            }
    Last edited by warny_maelstrom; 03-04-2004 at 11:28 AM.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,439
    Try a for loop examining each char in the string
    Eg
    Code:
    for ( i = 0 ; buffer[i] != '\0' ; i++ ) {
        if ( buffer[i] == ' ' ) {
            // found a word?
        }
    }
    Given your apparent difficulty over this, where did you get allocate() and de_allocate() from?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Jan 2004
    Posts
    13
    The original code isnt mine but a friend who is trying to write the program im just trying to help. I thought it might b good practise seeing as i might b taking up programming at university. I rewrote the code seeing as it wasnt my own work so i wasnt sure exactly wot was goin on and id prefer to get it step by step. Iv done a bit of programming but mostly winio, i havnt really read from text files, iv read binary code to create a basic game but thats about it to do with fgets().

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Another example is in the FAQ
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    for etiquette and courtesy to the others on this board, please bother to write full words in your posts.

  11. #11
    Registered User
    Join Date
    Jan 2004
    Posts
    13
    Originally posted by Draco
    for etiquette and courtesy to the others on this board, please bother to write full words in your posts.
    Draco are u refering to me or Hammer, and seeing as Hammer has so many posts im assuming that my abbrevaiting certain words offends u. Its not a case of courtesy seeing as everyone can easily understand my english and it doesnt cause anyone discomfort or great offence. I cant see y, i miss out apostrophes on words and i miss letters out so words appear as "b", instead of "be" or i write "wot" instead of "what", i have lots of friends some whos first or second language isnt english and they still manage to understand me. Writing like this is how i show my personality. If my grammar and use of words is such a problem id prefer not to use this forum rather than change the way i type.
    As i just looked through the posts on my thread only i noticed that not everyones grammar was perfect and abbreviations that couldnt b found in the Oxford dictionary were used.

  12. #12
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    nevermind, you completely miss the point. It isnt even a point of offense. 'Express' yourself and your personality in your typing however you like.
    Last edited by Draco; 03-05-2004 at 12:14 AM.

  13. #13
    Registered User
    Join Date
    Mar 2004
    Posts
    3
    intreguing point....

  14. #14
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Originally posted by Helios
    intreguing point....
    And I completely agree with Draco.

    Misspelling happens.
    English as a Second Language causes interesting spelling and phrases.
    But abbreviating 4 t sake o bein lzy iz difrnt. ths isnt aol msnger

    We appreciate not having to decipher these messages.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  15. #15
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    I think Walt has already made the point, but I'll make it again, abbreviating a word that is "obvious" to a native English speaker can send a lot of people, who have the good grace to help, scurrying for a dictionary in case it is a word they should know.

    There are an awful lot of people that help out around here do not have English as their first language.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help creating multiple text files
    By Tom Bombadil in forum C Programming
    Replies: 19
    Last Post: 03-28-2009, 11:21 AM
  2. Error in creating socket in a client (UDP)
    By ferenczi in forum Networking/Device Communication
    Replies: 2
    Last Post: 11-27-2008, 10:11 AM
  3. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  4. Creating a rain effect...
    By Raigne in forum Game Programming
    Replies: 11
    Last Post: 12-04-2007, 04:30 PM
  5. Creating a Simple DLL in C
    By Ultima4701 in forum C Programming
    Replies: 2
    Last Post: 11-23-2002, 12:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21