Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use strict;
- use warnings qw(all);
- use lib './lib';
- use Mojo::SlackRTM;
- use VPgrp::APIXU;
- use Data::Dumper;
- my $input = lc <STDIN>;
- chomp($input);
- my @keywords;
- push @keywords, 'weather';
- push @keywords, 'forecast';
- push @keywords, ' today';
- push @keywords, ' in \d+ day';
- push @keywords, ' tomorrow';
- push @keywords, ' in [^\d\s]+';
- push @keywords, ' week-end';
- push @keywords, ' weekend';
- push @keywords, ' week ';
- push @keywords, 'id (of )?"?[^\d\s"]+"?';
- push @keywords, 'profile (of )?"?#?100\d{5}"?';
- our %words;
- foreach my $word (@keywords)
- {
- while ($input =~ /($word)/pg)
- {
- my $key = $-[0];
- my $val = lc($1);
- $val =~ s/^\s+|\s+$//g;
- $val =~ s/[?!]//g;
- @words{$key} = lc($val);
- }
- }
- my $app = '';
- my $first = 1;
- foreach my $w (sort keys %words)
- {
- if ($words{$w} =~ /^([^\s]+).*/)
- {
- $app = $1 if $first;
- $first = 0;
- }
- }
- &weatherProcess if ($app eq 'weather' or $app eq 'forecast');
- &idProcess if $app eq 'id';
- &profileProcess if $app eq 'profile';
- &dontKnow if $app eq '';
- sub dontKnow
- {
- print 'I don\'t understand what you said...'
- }
- sub weatherProcess
- {
- my @days;
- my $city = 'SAINT-DENIS,FRANCE';
- foreach my $w (sort keys %words)
- {
- $city = uc($1) if ($words{$w} =~ /^in ([^\d\s]+)/);
- push @days, $1 if ($words{$w} =~ /^in (\d+) day/);
- push @days, 0 if ($words{$w} =~ /^today/);
- push @days, 1 if ($words{$w} =~ /^tomorrow/);
- if ($words{$w} =~ /^week(-)?end$/)
- {
- my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
- push @days, 6 - $wday;
- push @days, 6 - $wday + 1;
- }
- if ($words{$w} =~ /^week$/)
- {
- my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
- for (my $i = $wday + 1; $i <= 7; $i++)
- {
- push @days, $i - $wday;
- }
- }
- }
- if (scalar @days == 0)
- {
- push @days, 0;
- }
- foreach my $day (@days)
- {
- print 'WEATHER ('.$day.', '.$city.')',"\n";
- }
- }
- sub idProcess
- {
- my $name = '';
- foreach my $w (sort keys %words)
- {
- $name = $2 if ($words{$w} =~ /^id (of )?"?([^\d\s"]+)"?/);
- }
- print 'ID ('.$name.')',"\n";
- }
- sub profileProcess
- {
- my $id = '';
- foreach my $w (sort keys %words)
- {
- $id = $2 if ($words{$w} =~ /^profile (of )?"?#?(100\d{5})"?/);
- }
- print 'PROFILE ('.$id.')',"\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement