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

Re^4: Words in Words

by Lotus1 (Chaplain)
on Sep 30, 2011 at 22:43 UTC ( #928950=note: print w/ replies, xml ) Need Help??


in reply to Re^3: Words in Words
in thread Words in Words

Sorry, but only comparing words with those that come after it alphabetically doesn't work.

True, but in the solution I posted I sort by length after the alphabetical sort. The alphabetical sort is probably overkill but needed for this particular solution.

chomp (my @words = sort { length($a) <=> length($b) } sort <DATA>);


Comment on Re^4: Words in Words
Download Code
Re^5: Words in Words
by sarchasm (Acolyte) on Sep 30, 2011 at 23:18 UTC

    It looks like both solutions will work!

    One thing I just realized from your post about sorting is that you only need to look at words that are longer than the current word (which you are sortof doing). This means that as the program runs, it actually becomes faster at finding the results.

    I ran each program for 1 minute and BrowserUk's code produced 320 records. Lotus1's code produced 150. Even though your code appears to run slower I imagine performance will improve the longer the process runs because it will have fewer records to look through each time.

    I will let the programs run over the weekend to see what I get.

    Thank you all for your help. I learned a lot from your examples and suggestions!

      Another tweak should improve performance again:

      #! perl -slw use strict; my @words = do{ local @ARGV = 'words.txt'; <> }; chomp @words; my $all = join ' ', @words; my $start = time; for my $i ( @words ) { while( $all =~ m[ ([^ ]*$i[^ ]*) ]g ) { my $j = $1; next if $j eq $i or $j eq "${i}s" or $j eq "${i}'s"; print "$j contains $i"; last; ## Added } } printf STDERR "Took %d seconds for %d words\n", time() - $start, scalar @words;

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      Update: Evidently this is a step too far as it produces the wrong results. It could (probably) be fixed, but it will never beat choroba's solution below.

      My final offering. Combining Lotus1's sort by length with my big-string approach and this really flies, beating my previous best by an order of magnitude:

      Ignore!

      #! perl -slw use strict; my @words = sort{ length($a) <=> length($b) } do{ local @ARGV = 'words.txt'; <> }; chomp @words; my $start = time; my $all = join ' ', @words; study $all; my @offsets; for my $l ( 1 .. 20 ) { push @offsets, $all =~ m[ ([^ ]{$l}) ] ? $-[0] : $offsets[-1]; } for my $i ( @words ) { while( substr( $all, $offsets[ length( $i ) +1 ] ) =~ m[ ([^ ]*$i[^ ]*) ]g ) { my $j = $1; next if $j eq $i or $j eq "${i}s" or $j eq "${i}'s"; print $i; last; } } printf STDERR "Took %d seconds for %d words\n", time() - $start, scalar @words;

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        I slightly modified my script:
        #!/usr/bin/perl use feature 'say'; use warnings; use strict; my $file = 'words.txt'; open my $IN, '<', $file or die "$!"; my %words; while (my $word = <$IN>) { chomp $word; undef $words{$word}; } my %reported; for my $word (keys %words) { my $length = length $word; for my $pos (0 .. $length - 1) { my $skip_itself = ! $pos; for my $len (1 .. $length - $pos - $skip_itself) { my $subword = substr($word, $pos, $len); next if exists $reported{$subword}; next if $word eq $subword . q{s} or $word eq $subword . q{'s}; if (exists $words{$subword}) { say "$subword"; undef $reported{$subword}; } } } }
        I used english.0 from this archive as words.txt: http://downloads.sourceforge.net/wordlist/ispell-enwl-3.1.20.zip. Your script took 58s, whilest mine only 6s (on Pentium 4, 2.8 GHz). The results were different, though: your output contains the word indistinguishableness that mine does not; my list contained 911 more words than yours (e.g. you, wraps or tribe's).

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (17)
As of 2014-07-22 14:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (116 votes), past polls