Help - Search - Member List - Calendar
Full Version: How do I open 2 files at once 1 to be checked against the other
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
mark McWilliams
Please help

I need to make a spell check program that will open a
dictionary that the user enters the name of and then a
file that will be checked against the dictionary file.

so at prompt
% spellcheck.pl Dictionary_file Check_this_file

the program will
open the Dictionary_File and then open the
Check_this_file

after they are open I have to check one file against
the other but I can'r get two files to open at once.

I have to separate the file names to be opened later
in subs
($file_D , $file_C) = (split " ". <>);

I am working on a few subs that will open the files
after I get them like

sub read_file1
{
print " $file1 = $file1 n"; # what is
value of $file1 ./DICT
my $file1 = shift; # get 1st
argument to the sub ?
my @dictionary; # going to be
checked against
open(DICTIONARY,' $file_D') or return undef ; #
Also NOT working
# open(DICTIONARY,$file_D) or return undef ; # NOT
WORKING
# write dictionary to be compared against
# added path because $file_D = './DICT' didn't work
(set for test)
# open(DICTIONARY,'./DICT') or return undef;
#works w/ hard path
while(<DICTIONARY>)
{
chomp;
push @dictionary,$_;
# i know it gets here if I use a path in the open
(handle , ' path ')
print "while $dictionary[$i] t $in"; #
check 2b removed
$i++;
# check 2b removed
}
close DICTIONARY;
return @dictionary;
}


# a similar start fot the file to be checked but as
of yet I can not get a file open without a hard coded
path . It can not be a hard coded path because I do
not know the dictionary or the file to be checked in
advance.

Should I change my way of thinking of the problem?
Is there some bit of code that will separate files
like a n (newline) except ? (newfile) that I can
split the <> input on?

As always I thank you very much for your time and
effort in advance.

THank you Thank you Thank you very much

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

thundergnat
mark McWilliams wrote:
QUOTE
Please help

I need to make a spell check program that will open a
dictionary that the user enters the name of and then a
file that will be checked against the dictionary file.

so at prompt
% spellcheck.pl  Dictionary_file  Check_this_file

the program will
open the Dictionary_File and then open the
Check_this_file

Parameters passed at the command line are in the @ARGV
array. Just assign the names from there. You can shift
them off quite easily. If you want to want to be able to
check multiple files at once, shift off the dictionary
file then put the remainder of @ARGV into an array
the you can cycle through.

Here's a "spell check" script I had written if you are
interested. It assumes the dictionary will have one word
pair per line with some sort of delimiter between words,
(set to ', ' currently.) Feed the script the dictionary
file and a list of files to check and it will cycle
through them, checking each.

It is fairly efficient, especially as the dictionary
grows large; and is pretty good about error trapping.
It is Unicode aware.

Use or modify as you like.



#!/usr/bin/perl

use strict;
use warnings;
use File::Temp qw/ tempfile /;
++$|;

my $usage = "Usage: $0 (Dictionary file) (file list)n";

die $usage if (@ARGV < 2);

my $dictionary_file = shift;
# Get the dictionary file name

my @checkfiles = @ARGV;
# and the names of the files to check.

my %dictionary;
# Set up a hash to hold the dictionary.

my $dictionary_delimiter = ', ';
# Set up the delimiter separating word pairs.
# Dependent on how your file is set up,
# adjust accordingly.


load_dictionary();

for (@checkfiles){
spellcheck($_)
}

############################################################
sub load_dictionary{

print "Loading Dictionary $dictionary_file...nn";

open my $dict, '<', $dictionary_file or die
"Could not open dictionary file; $!.n";

while (<$dict>){
s/^s+//;
s/s+$//;
if (m/([p{Alnum}']+)$dictionary_delimiter([p{Alnum}']+)/){
$dictionary{$1} = $2;
}
}
}
############################################################
sub spellcheck{
my $filename = shift;
my $file;

print "Checking file $filename... ";

unless (open $file, '<', $filename){
warn "Could not open file $filename; $!.n";
return;
}

my ($temphandle, $tempfile) = tempfile();

while (<$file>){

my $saveline = $_;

# Need to preserve words with ' in them so do
# a little dancing to get rid of all punctuation
# except ' inside a word. x{A0} is a non breaking
# space. For spell checking purposes, they are
# unimportant. Get rid of any, convert ' inside a
# word to nbs, remove all remaining punctuation
# then convert back. A pain, but necessary.

s/x{A0}/ /g;
s/(?<=w)'(?=w)/x{A0}/g;
s/p{Punct}/ /g;
s/x{A0}/'/g;

# Now split the words into an array
my @words = split(/s+/, $_);

# and substitute as necessary.
for (@words){
$saveline =~ s/(?<=P{Alnum})$_(?=P{Alnum})/$dictionary{$_}/g
if defined $dictionary{$_};
}

print $temphandle $saveline;
}
close $file;
close $temphandle;

unlink $filename.'.bak' if (-e $filename.'.bak') or
die "Couldn't overwrite backup file $filename.bak.n";

rename $filename, $filename.'.bak' or
die "Couldn't save backup file $filename.bak.n";

rename $tempfile, $filename or
die "Couldn't save file $filename.n";

print "Done.n"
}
############################################################

thundergnat
mark McWilliams wrote:
QUOTE
Please help

I need to make a spell check program that will open a
dictionary that the user enters the name of and then a
file that will be checked against the dictionary file.

so at prompt
% spellcheck.pl  Dictionary_file  Check_this_file

the program will
open the Dictionary_File and then open the
Check_this_file


(Arrgh, That last post I made had horrible formatting.
Let me try this again.)

Parameters passed at the command line are in the @ARGV
array. Just assign the names from there. You can shift
them off quite easily. If you want to want to be able to
check multiple files at once, shift off the dictionary
file then put the remainder of @ARGV into an array the
you can cycle through.

Here's a "spell check" script I had written if you are
interested. It assumes the dictionary will have one word
pair per line with some sort of delimiter between words,
(set to ', ' currently.) Feed the script the dictionary
file and a list of files to check and it will cycle
through them, checking each.

It is fairly efficient, especially as the dictionary
grows large; and is pretty good about error trapping.
It is Unicode aware.

Use or modify as you like.




#!/usr/bin/perl

use strict;
use warnings;
use File::Temp qw/ tempfile /;
++$|;

my $usage = "Usage: $0 (Dictionary file) (file list)n";

die $usage if (@ARGV < 2);

my $dictionary_file = shift;
# Get the dictionary file name

my @checkfiles = @ARGV;
# and the names of the files to check.

my %dictionary;
# Set up a hash to hold the dictionary.

my $dictionary_delimiter = ', ';
# Set up the delimiter separating word pairs.
# Dependent on how your file is set up,
# adjust accordingly.


load_dictionary();

for (@checkfiles){
spellcheck($_)
}

############################################################
sub load_dictionary{

print "Loading Dictionary $dictionary_file...nn";

open my $dict, '<', $dictionary_file or die
"Could not open dictionary file; $!.n";

while (<$dict>){
s/^s+//;
s/s+$//;
if (m/([p{Alnum}']+)$dictionary_delimiter([p{Alnum}']+)/){
$dictionary{$1} = $2;
}
}
}
############################################################
sub spellcheck{
my $filename = shift;
my $file;

print "Checking file $filename... ";

unless (open $file, '<', $filename){
warn "Could not open file $filename; $!.n";
return;
}

my ($temphandle, $tempfile) = tempfile();

while (<$file>){

my $saveline = $_;

# Need to preserve words with ' in them so do
# a little dancing to get rid of all punctuation
# except ' inside a word. x{A0} is a non breaking
# space. For spell checking purposes, they are
# unimportant. Get rid of any, convert ' inside a
# word to nbs, remove all remaining punctuation
# then convert back. A pain, but necessary.

s/x{A0}/ /g;
s/(?<=w)'(?=w)/x{A0}/g;
s/p{Punct}/ /g;
s/x{A0}/'/g;

# Now split the words into an array
my @words = split(/s+/, $_);

# and substitute as necessary.
for (@words){
$saveline =~ s/(?<=P{Alnum})$_(?=P{Alnum})/$dictionary{$_}/g
if defined $dictionary{$_};
}

print $temphandle $saveline;
}
close $file;
close $temphandle;

unlink $filename.'.bak' if (-e $filename.'.bak') or
die "Couldn't overwrite backup file $filename.bak.n";

rename $filename, $filename.'.bak' or
die "Couldn't save backup file $filename.bak.n";

rename $tempfile, $filename or
die "Couldn't save file $filename.n";

print "Done.n"
}
############################################################


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2005 Invision Power Services, Inc.