- 论坛徽章:
- 7
|
yige lizi:
DATA: 1 hang => 1 ge doc- Hello world pop world
- good hello world
- very good
- Real World Studios
- push pop shift pop pop
- good very very good
- push pop pop insert
复制代码 test:- hello world
- HELLO
- kitty
- pop
- Very Good
复制代码 output:- [ hello world ] 1, 2, 4
- [ HELLO ] 2, 1
- [ kitty ]
- [ pop ] 5, 7, 1
- [ Very Good ] 6, 3, 2
复制代码 biru:- #!/usr/bin/perl
- use 5.010;
- # init
- my %data;
- while (<DATA>) {
- add( $., $_ );
- }
- # test
- my @test = ( 'hello world', 'HELLO', 'kitty', 'pop', 'Very Good' );
- for my $test (@test) {
- my @result = search($test);
- say "[ $test ]\t ", join ', ', @result;
- }
- # sub
- sub add {
- my ( $id, $text ) = @_;
- my @words = map lc, split /\s+/, $text;
- for my $word (@words) {
- $data{$word}{$id}++;
- }
- }
- sub search {
- my $search = shift;
- my @words = map lc, split /\s+/, $search;
- return these( \@words );
- }
- sub these {
- my @words = @{ +shift };
- my %good;
- for my $word (@words) {
- for my $ID ( keys %{ $data{$word} } ) {
- $good{$ID}{ID}++;
- $good{$ID}{COUNT} += $data{$word}{$ID};
- }
- }
- sort {
- $good{$b}{ID} <=> $good{$a}{ID}
- || $good{$b}{COUNT} <=> $good{$a}{COUNT}
- } keys %good;
- }
- __DATA__
- Hello world pop world
- good hello world
- very good
- Real World Studios
- push pop shift pop pop
- good very very good
- push pop pop insert
复制代码 |
|