fork download
  1. #!/usr/bin/perl
  2.  
  3. use warnings;
  4. use strict;
  5.  
  6. $\ = $/;
  7.  
  8. $_ = <>, chomp;
  9. my %words = map { lc $_, 1 } m/[a-z]+/gi;
  10. my @words = keys %words;
  11.  
  12. my @sentences = map { s/^\s+|\s+$//gr }
  13. map { split m/[.]\K\s+(?=[A-Z0-9])/ }
  14. split m/[!?]\K/;
  15.  
  16. my %Hash;
  17. for my $word (@words){
  18. my $i = 0;
  19. for my $sentence (@sentences){
  20. $sentence =~ m/\b$word\b/i and push @{ $Hash{ $word } }, $i;
  21. $i ++;
  22. }
  23. }
  24.  
  25. <>;
  26.  
  27. while(<>){
  28. m/\w/ or next;
  29. print "Search results for \"$_\":";
  30. my %w = map { lc $_, 1 } m/\w+/g;
  31. my @w = keys %w;
  32. next if grep { !exists $Hash{ $_ } } @w;
  33. @w = grep { exists $Hash{ $_ } } @w;
  34. my %idx;
  35. map { $idx{ $_ } ++ } map { @{ $Hash{ $_ } } } @w;
  36.  
  37. my @idx = grep { @w == $idx{ $_ } } keys %idx;
  38. print "- \"$_\"" for map $sentences[$_], @idx;
  39. }
  40.  
  41.  
Success #stdin #stdout 0s 6188KB
stdin
  abc . If op ? . . Nud . pl ! um isp .  
8
abc
if
nud
isp
op   nud
op if
if o
stdout
Search results for "abc":
- "abc ."
Search results for "if":
- "If op ?"
Search results for "nud":
- "Nud . pl !"
Search results for "isp":
- "um isp ."
Search results for "op   nud":
Search results for "op if":
- "If op ?"
Search results for "if o":