免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3468 | 回复: 0
打印 上一主题 下一主题

perl 倒排 [复制链接]

论坛徽章:
7
戌狗
日期:2013-12-15 20:43:38技术图书徽章
日期:2014-03-05 01:33:12技术图书徽章
日期:2014-03-15 20:31:17未羊
日期:2014-03-25 23:48:20丑牛
日期:2014-04-07 22:37:44巳蛇
日期:2014-04-11 21:58:0915-16赛季CBA联赛之青岛
日期:2016-03-17 20:36:13
1 [报告]
发表于 2016-02-20 03:33 |显示全部楼层
yige lizi:

DATA: 1 hang => 1 ge doc
  1. Hello world pop world
  2. good hello world
  3. very good
  4. Real World Studios
  5. push pop shift pop pop
  6. good very very good
  7. push pop pop insert
复制代码
test:
  1. hello world
  2. HELLO
  3. kitty
  4. pop
  5. Very Good
复制代码
output:
  1. [ hello world ]         1, 2, 4
  2. [ HELLO ]         2, 1
  3. [ kitty ]         
  4. [ pop ]         5, 7, 1
  5. [ Very Good ]         6, 3, 2
复制代码
biru:
  1. #!/usr/bin/perl

  2. use 5.010;

  3. # init

  4. my %data;

  5. while (<DATA>) {
  6.     add( $., $_ );
  7. }

  8. # test

  9. my @test = ( 'hello world', 'HELLO', 'kitty', 'pop', 'Very Good' );

  10. for my $test (@test) {
  11.     my @result = search($test);
  12.     say "[ $test ]\t ", join ', ', @result;
  13. }

  14. # sub

  15. sub add {
  16.     my ( $id, $text ) = @_;
  17.     my @words = map lc, split /\s+/, $text;
  18.     for my $word (@words) {
  19.         $data{$word}{$id}++;
  20.     }
  21. }

  22. sub search {
  23.     my $search = shift;
  24.     my @words = map lc, split /\s+/, $search;
  25.     return these( \@words );
  26. }

  27. sub these {
  28.     my @words = @{ +shift };
  29.     my %good;
  30.     for my $word (@words) {
  31.         for my $ID ( keys %{ $data{$word} } ) {
  32.             $good{$ID}{ID}++;
  33.             $good{$ID}{COUNT} += $data{$word}{$ID};
  34.         }
  35.     }

  36.     sort {
  37.              $good{$b}{ID} <=> $good{$a}{ID}
  38.           || $good{$b}{COUNT} <=> $good{$a}{COUNT}
  39.     } keys %good;
  40. }

  41. __DATA__
  42. Hello world pop world
  43. good hello world
  44. very good
  45. Real World Studios
  46. push pop shift pop pop
  47. good very very good
  48. push pop pop insert
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP