Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Comparing Lines within a Word List

by CountZero (Bishop)
on Apr 28, 2016 at 20:32 UTC ( [id://1161806]=note: print w/replies, xml ) Need Help??


in reply to Comparing Lines within a Word List

It takes a little longer, but the result is a file that shows all words in your worlist which differ in one place only and they are sorted according to which characters are different.

For instance for "rs" differences, the result (based upon my wordlist) is as follows:

***** rs ***** abaser - abases abater - abates abider - abides abjurer - abjures ... year - yeas yodler - yodles yummier - yummies zanier - zanies zoner - zones
And here is the program:
use Modern::Perl qw/2015/; use List::MoreUtils qw/zip natatime/; use autodie; open( my $DICT, '<', './wordsEn.txt' ) or die "Could not open dictiona +ry - $!"; my @dict; print scalar <$DICT>; # show copyright message of word list # step 1: sort the dictionary into bins per length of the word while ( my $word = <$DICT> ) { chomp $word; next unless $word; push @{ $dict[ length $word ] }, $word; } close $DICT; # step 2: for each bin look for words that differ in one place only my %results; for my $length ( 2 .. @dict - 1 ) { next unless $dict[$length]; # skip if no words of this length my @bin = @{ $dict[$length] }; next if @bin < 2; # skip if only one word of this length say "*********** Testing words of length $length ***********"; while ( my ( $index, $test ) = each @bin ) { for my $check ( @bin[ $index + 1 .. @bin - 1 ] ) { my $diff = $test ^ $check; if ( 1 == $diff =~ tr/\x00//c ) { # only one character dif +ferent # find which characters are different my @first = split '', $test; my @second = split '', $check; my @test = zip @first, @second; my $it = natatime 2, @test; while ( my @vals = $it->() ) { next if $vals[0] eq $vals[1]; my $key = $vals[0] lt $vals[1] ? "$vals[0]$vals[1]" : "$vals[1]$vals[0]"; push @{ $results{$key} }, "$test - $check"; # save + in hash last; } } } } } say "Now writing results"; open( my $RESULTS, '>', './results.txt' ) or die "Cannot open output f +ile $!"; for my $key ( sort keys %results ) { say $RESULTS "***** $key *****"; for my $words ( sort @{ $results{$key} } ) { say $RESULTS "\t$words"; } } close $RESULTS;

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics

Replies are listed 'Best First'.
Re^2: Comparing Lines within a Word List
by Anonymous Monk on Apr 28, 2016 at 21:16 UTC

    "Light and agile" eh?

    #!/usr/bin/perl -l use strict; use warnings; $_ = do{local $/; <DATA>}; /^((\w*) (.) (\w*))$ # find a first word .* # skip non match lines ^(\2 (?!\3). \4)$ # second word same except one letter (??{print "$1 $5"})/xms; # print and fail __DATA__ cases carer caser cares bare mare base case bust burt bent sat rat bat matter mattes pat

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1161806]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-03-28 23:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found