<?xml version="1.0" encoding="windows-1252"?>
<node id="1004395" title="problem spellchecker" created="2012-11-18 06:40:59" updated="2012-11-18 06:40:59">
<type id="115">
perlquestion</type>
<author id="1004392">
Jurafsky</author>
<data>
<field name="doctext">
&lt;p&gt; I've created a little spellchecker. 
This script works in this way: &lt;br&gt;

After reading each line of the text, realizes some corrections 
thanks to a comparison between a dictionary and the text itself. &lt;br&gt;

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. &lt;br&gt; &lt;/p&gt;

&lt;p&gt; Here there's my problem: &lt;/p&gt;
&lt;br&gt;
I would like to give to the user the possibility to choose the correct word 
among the words suggested. Something like this: &lt;br&gt;

We found the word "wlak" in your text which isn't correct. &lt;br&gt;
The suggested possibilities are: &lt;br&gt; 
&lt;li&gt;1. walk &lt;/li&gt;
&lt;li&gt;2. work &lt;/li&gt;

&lt;br&gt; type the number associated to the word or 0 if you can't find the correct word. 

&lt;br&gt; Then I would like to replace the correct word on the original text (creating a new .txt). &lt;br&gt;

&lt;p&gt;How can I do this? &lt;/p&gt;

&lt;code&gt;	
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, "&lt;", $file_dictionary); 
while (defined( $line1 = &lt;L&gt;)) { 
	chomp($line1); 
	@filedictionary = split (/\s/, $line1); 
	push (@dictionary, @filedictionary); 
	} 
	 
#I create an array for the text	 
open (T, "&lt;", $file_text); 
while (defined( $line = &lt;T&gt;)) { 
	chomp($line); 
	@filetext = split (/(\s|\pP)/, $line); 
	for ($i = 0; $i &lt; @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, "&lt;", $file_dictionary); 
while (defined( $line1 = &lt;L&gt;)) { 
	chomp($line1); 
	@filedictionary = split (/\s/, $line1); 
	push (@dictionary, @filedictionary); 
	} 
 
#first operation --&gt; "palrer" will be "parler" 
for (my $i=0; $i &lt; $#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 --&gt; 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 --&gt; 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"; 
			} 
		} 
	} 
}&lt;/code&gt;

https://www.dropbox.com/s/t9fc2dk5mqbsb20/texte.txt &lt;br&gt; this is the text &lt;br&gt;

https://www.dropbox.com/s/717rczou0mkrp0s/lexique.txt" &lt;br&gt; this is the French Dictionary</field>
</data>
</node>
