#!/usr/bin/perl

use utf8;
use Encode;
binmode STDIN,":utf8";
binmode STDOUT,":utf8";

$regexp=$ARGV[0];
$regexp=decode("utf8",$regexp);

$page=$ARGV[1];
$number_of_printed = 0;
$number_of_printed_max = 10;
#if (open(PAGE, "lynx -dump '$page'|")) {
if (open(PAGE, "$page")) {
    binmode PAGE,":utf8";
    local $/;
    $text=<PAGE>;
    $text =~ s@http://[a-zA-Z&_.:/0-9%?=,\#+()\[\]~-]*@@g;
    $text =~ s@\n@@g;
    $text =~ s@(Mr|Mrs|viz)\.@\1POINT@g;
    $text =~ s@e\.g\.@ePOINTgPOINT@g;
    $text =~ s@i\.e\.@iPOINTePOINT@g;
    @sentences=split /[.!?]/, $text;
    for (@sentences) {
        s@iPOINTePOINT@i\.e\.@g;
        s@ePOINTgPOINT@e\.g\.@g;
        s@(Mr|Mrs|viz)POINT@\1.@g;
        s/^\s*//;
        s/\s*$//;
        s/\[[0-9]+\]//g;
        s/\s+/ /g;
        if (/\b$regexp\b/ and $number_of_printed < $number_of_printed_max ) {
            print "$_.\n\n";
            $number_of_printed++;
        }
    }
}
