Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.56 KB | None | 0 0
  1. use strict;
  2. use warnings qw(all);
  3. use lib './lib';
  4. use Mojo::SlackRTM;
  5. use VPgrp::APIXU;
  6. use Data::Dumper;
  7.  
  8. my $input = lc <STDIN>;
  9. chomp($input);
  10.  
  11. my @keywords;
  12. push @keywords, 'weather';
  13. push @keywords, 'forecast';
  14. push @keywords, ' today';
  15. push @keywords, ' in \d+ day';
  16. push @keywords, ' tomorrow';
  17. push @keywords, ' in [^\d\s]+';
  18. push @keywords, ' week-end';
  19. push @keywords, ' weekend';
  20. push @keywords, ' week ';
  21. push @keywords, 'id (of )?"?[^\d\s"]+"?';
  22. push @keywords, 'profile (of )?"?#?100\d{5}"?';
  23.  
  24. our %words;
  25.  
  26. foreach my $word (@keywords)
  27. {
  28.     while ($input =~ /($word)/pg)
  29.     {
  30.         my $key = $-[0];
  31.         my $val = lc($1);
  32.         $val =~ s/^\s+|\s+$//g;
  33.         $val =~ s/[?!]//g;
  34.         @words{$key} = lc($val);
  35.     }
  36. }
  37.  
  38. my $app = '';
  39. my $first = 1;
  40. foreach my $w (sort keys %words)
  41. {
  42.     if ($words{$w} =~ /^([^\s]+).*/)
  43.     {
  44.         $app = $1 if $first;
  45.         $first = 0;
  46.     }
  47. }
  48.  
  49. &weatherProcess if ($app eq 'weather' or $app eq 'forecast');
  50. &idProcess if $app eq 'id';
  51. &profileProcess if $app eq 'profile';
  52. &dontKnow if $app eq '';
  53.  
  54. sub dontKnow
  55. {
  56.     print 'I don\'t understand what you said...'
  57. }
  58.  
  59. sub weatherProcess
  60. {
  61.     my @days;
  62.     my $city = 'SAINT-DENIS,FRANCE';
  63.     foreach my $w (sort keys %words)
  64.     {
  65.         $city = uc($1) if ($words{$w} =~ /^in ([^\d\s]+)/);
  66.         push @days, $1 if ($words{$w} =~ /^in (\d+) day/);
  67.         push @days, 0 if ($words{$w} =~ /^today/);
  68.         push @days, 1 if ($words{$w} =~ /^tomorrow/);
  69.         if ($words{$w} =~ /^week(-)?end$/)
  70.         {
  71.             my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
  72.             push @days, 6 - $wday;
  73.             push @days, 6 - $wday + 1;
  74.         }
  75.         if ($words{$w} =~ /^week$/)
  76.         {
  77.             my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
  78.             for (my $i = $wday + 1; $i <= 7; $i++)
  79.             {
  80.                 push @days, $i - $wday;
  81.             }
  82.         }
  83.     }
  84.     if (scalar @days == 0)
  85.     {
  86.         push @days, 0;
  87.     }
  88.     foreach my $day (@days)
  89.     {
  90.         print 'WEATHER ('.$day.', '.$city.')',"\n";
  91.     }
  92. }
  93.  
  94. sub idProcess
  95. {
  96.     my $name = '';
  97.     foreach my $w (sort keys %words)
  98.     {
  99.         $name = $2 if ($words{$w} =~ /^id (of )?"?([^\d\s"]+)"?/);
  100.     }
  101.     print 'ID ('.$name.')',"\n";
  102. }
  103.  
  104. sub profileProcess
  105. {
  106.     my $id = '';
  107.     foreach my $w (sort keys %words)
  108.     {
  109.         $id = $2 if ($words{$w} =~ /^profile (of )?"?#?(100\d{5})"?/);
  110.     }
  111.     print 'PROFILE ('.$id.')',"\n";
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement