#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Arrow Suggestions for a spellchecker


    I've created a little spellchecker.
    This script works in this way:

    After reading each line of the text, realizes some corrections
    thanks to a comparison between a dictionary and the text itself.

    When it finds a word that doesn't exist in the dictionary, it corrects the words
    (giving one or more suggestions) and pushes it into an array.

    Here there's my problem:

    I would like to give to the user the possibility to choose the correct word
    among the words suggested. Something like this:

    We found the word "wlak" in your text which isn't correct.
    The suggested possibilities are:
    1. walk
    2. work

    type the number associated to the word or 0 if you can't find the correct word.

    Then I would like to replace the correct word on the original text (creating a new .txt).

    How can I do this?


    Code:
    use diagnostics; use warnings; my ($file_dictionary, $word, $line, $line1, $alph, $elt, $w, $transposition, $letter1, $letter2); my (@word, @altered_word, @filedictionary, @filetext, @dictionary, @addition, @replacement, @transposition, @removal); $file_dictionary = "lexique.txt"; $file_text = "texte.txt"; #I create an array for the dictionary open (L, "<", $file_dictionary); while (defined( $line1 = <L>)) { chomp($line1); @filedictionary = split (/\s/, $line1); push (@dictionary, @filedictionary); } #I create an array for the text open (T, "<", $file_text); while (defined( $line = <T>)) { chomp($line); @filetext = split (/(\s|\pP)/, $line); for ($i = 0; $i < @filetext; $i++) { if (!grep(/^$filetext[$i]$/, @dictionary)) { push (@word, $filetext[$i]); } } } #then I create an array for each word foreach $w(@word) { @altered_word = split (//, $w); #I create an array for the dictionary open (L, "<", $file_dictionary); while (defined( $line1 = <L>)) { chomp($line1); @filedictionary = split (/\s/, $line1); push (@dictionary, @filedictionary); } #first operation --> "palrer" will be "parler" for (my $i=0; $i < $#altered_word ; $i++) { @transposition = @altered_word; $letter1 = $transposition[$i]; $letter2 = $transposition[$i+1]; $transposition[$i] = $letter2; $transposition[$i+1] = $letter1; $transposition = join "", @transposition; if (grep(/^$transposition$/, @dictionary)) { print "post transposition : $transposition\n"; } } foreach $elt (0 .. $#altered_word) { #second operation --> parller will be parler @removal = @altered_word; splice(@removal, $elt, 1); $removal = join "", @removal; if (grep(/^$removal$/, @dictionary)) { print "post enlevement : $removal\n"; } #third operation --> parer will be parler foreach $alph('a' .. 'z') { @addition = @altered_word; splice(@addition, $elt, 0, $alph); $addition = join "", @addition; if (grep(/^$addition$/, @dictionary)) { print "post addition : $addition\n"; } #last operation : mancer will be manger @replacement = @altered_word; splice(@replacement, $elt, 1, $alph); $replacement = join "", @replacement; if (grep(/^$replacement$/, @dictionary)) { print "post replacement : $replacement\n"; } } } }


    Text French Dictionary
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    828
    Rep Power
    496
    Open a new file, read the original file lines, correct whatever needs to be corrected in each line, and then print the lines (corrected or not) into the new file. Then, you can do house cleaning (removing or renaming original file, renaming new file with the name of the old file, etc.).

    For the benefit of other readers, I have quickly reformatted you code, to make it more readable:

    Perl Code:
    use diagnostics;
    use warnings;
    my ($file_dictionary, $word, $line, $line1, $alph, $elt, $w, $transposition, $letter1, $letter2); 
    my (@word, @altered_word, @filedictionary, @filetext, @dictionary, @addition, @replacement, @transposition, @removal); 
     
    $file_dictionary = "lexique.txt"; 
    $file_text = "texte.txt"; #I create an array for the dictionary 
    open (L, "<", $file_dictionary); 
    while (defined( $line1 = <L>)) { 
    	chomp($line1); 
    	@filedictionary = split (/\s/, $line1); 
    	push (@dictionary, @filedictionary); 
    } #I create an array for the text 
     
    open (T, "<", $file_text); 
    while (defined( $line = <T>)) { 
    	chomp($line); 
    	@filetext = split (/(\s|\pP)/, $line); 
    	for ($i = 0; $i < @filetext; $i++) {
    		if (!grep(/^$filetext[$i]$/, @dictionary)) {
    			push (@word, $filetext[$i]); 
    		} 
    	} 
    }
    #then I create an array for each word 
     
    foreach $w(@word) { 
    	@altered_word = split (//, $w); #I create an array for the dictionary 
    	open (L, "<", $file_dictionary); 
    	while (defined( $line1 = <L>)) { 
    		chomp($line1); 
    		@filedictionary = split (/\s/, $line1); 
    		push (@dictionary, @filedictionary); 
    	} 
    	#first operation --> "palrer" will be "parler" 
     
    	for (my $i=0; $i < $#altered_word ; $i++) { 
    		@transposition = @altered_word; 
    		$letter1 = $transposition[$i]; 
    		$letter2 = $transposition[$i+1]; 
    		$transposition[$i] = $letter2; 
    		$transposition[$i+1] = $letter1; 
    		$transposition = join "", @transposition; 
    		if (grep(/^$transposition$/, @dictionary)) { 
    			print "post transposition : $transposition\n"; 
    		}
    	} 
    	foreach $elt (0 .. $#altered_word) { 
    		#second operation --> parller will be parler 
    		@removal = @altered_word; 
    		splice(@removal, $elt, 1); 
    		$removal = join "", @removal; 
    		if (grep(/^$removal$/, @dictionary)) { 
    			print "post enlevement : $removal\n";
    		} 
    		#third operation --> parer will be parler 
    		foreach $alph('a' .. 'z') { 
    			@addition = @altered_word; 
    			splice(@addition, $elt, 0, $alph); 
    			$addition = join "", @addition; 
    			if (grep(/^$addition$/, @dictionary)) { 
    				print "post addition : $addition\n";
    			}
    			#last operation : mancer will be manger 
    			@replacement = @altered_word; 
    			splice(@replacement, $elt, 1, $alph); 
    			$replacement = join "", @replacement; 
    			if (grep(/^$replacement$/, @dictionary)) { 
    				print "post replacement : $replacement\n"; 
    			} 
    		} 
    	} 
    }

    Comments on this post

    • Winters agrees
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by Laurent_R
    Open a new file, read the original file lines, correct whatever needs to be corrected in each line, and then print the lines (corrected or not) into the new file. Then, you can do house cleaning (removing or renaming original file, renaming new file with the name of the old file, etc.).
    thank you for the code!
    However, I don't understand how the user can choose the right word among the suggested ones?

    What I would like to do is

    1. Give the opportunity to choose
    2. Replace the word chosen by the user.

    But I don't know how and I think it's not so difficult but I can't get it
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    828
    Rep Power
    496
    Suppose your text contains the word "ralk" and your list of words finds the following similar words: "balk", "walk", "talk" "chalk", "rank" and "rail".

    You may just create a short menu showing part of the sentence where the original work appears and the list of words found as candidates for replacement:

    Code:
    Found sentence: "I have seen enough of this, let's ralk away!"
    Please which word do you want to use to replace the underlined word:
    0. Don't change it.
    1. balk
    2. chalk
    3. rail
    4. rank
    5. talk
    6. walk
    Please enter the number of the word you want to use:
    Then on the basis of the user's choice, just replace "ralk" by the work the user chose in the line being processed.

    For example, if the user chose option (6), assuming that the falty word is stored in $faulty_word and that the proposals are stored in the @proposal array, you could have something like this:
    Perl Code:
    $line =~ s/$faulty_word/$proposals[6]/;


    It does not seem too complicated, or did I miss something in your problem?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    It's ok ! Yesterday after I answered to you, I did this and it was perfect! Now I have to apply it to all the lines of my text. I'll try today

    I lost myself in the simplest part of the script.. I feel so stupid !

    Thank you very much !

IMN logo majestic logo threadwatch logo seochat tools logo