Dessert: Added support for words.
We can now search for "desserts that start with apple".master
parent
98d0a07883
commit
3836912cf7
|
@ -18,23 +18,24 @@ source 'http://food.sulekha.com/dishes/course/desserts/alphabets/a.htm';
|
|||
|
||||
triggers start => 'dessert', 'desserts', 'a dessert';
|
||||
|
||||
# Returns HTML version of our dessert.
|
||||
sub to_html {
|
||||
my $dessert_name = shift;
|
||||
my $dessert_link = $dessert_name;
|
||||
$dessert_link =~ s/\s/+/g;
|
||||
|
||||
return "<a href='http://duckduckgo.com/?q=$dessert_link+recipe'>" . $dessert_name . "</a>";
|
||||
}
|
||||
|
||||
# This function picks a dessert from our lists.
|
||||
sub itemify {
|
||||
# This has our list of desserts.
|
||||
my @dessert_list = @{$_[0]};
|
||||
# This tells us if we should add an anchor tag or not.
|
||||
# We do this because we have two different outputs: HTML and plain text.
|
||||
my $is_html = $_[1];
|
||||
my ($dessert_list, $end) = @_;
|
||||
|
||||
my $i = rand scalar @dessert_list;
|
||||
my $dessert = $dessert_list[$i];
|
||||
my $i = rand scalar @{$dessert_list};
|
||||
my $dessert = $dessert_list->[$i];
|
||||
|
||||
if($is_html) {
|
||||
# Encode spaces so that we can use it as a link.
|
||||
$dessert =~ s/\s/+/g;
|
||||
return "<a href='http://duckduckgo.com/?q=$dessert+recipe'>" . $dessert_list[$i] . "</a>";
|
||||
}
|
||||
return $dessert;
|
||||
return $dessert . $end, html => to_html($dessert) . $end;
|
||||
};
|
||||
|
||||
# This is our list of desserts.
|
||||
|
@ -67,18 +68,42 @@ my %desserts = (
|
|||
z => ['Zeppole', 'Zucchini Cake', 'Zebra-Stripe Cheesecake'],
|
||||
);
|
||||
|
||||
handle remainder => sub {
|
||||
if(lc $_ =~ m/^(?:that )?(?:start|beginn?)s?(?:ing)? ?(?:with)? (?:the letter )?([a-zA-Z])$/i) {
|
||||
my $in = lc $1;
|
||||
my $items = $desserts{lc $in};
|
||||
# This function checks if the query string matches the beginning of one of the desserts.
|
||||
sub begins_with {
|
||||
my @items = ();
|
||||
my $query = shift;
|
||||
|
||||
my $end = " is a dessert that begins with the letter " . uc $in . '.';
|
||||
# We're getting the first letter, and we're going to use that as our key.
|
||||
# This should be faster since we don't go through every dessert in the hash.
|
||||
my $letter = substr($query, 0, 1);
|
||||
|
||||
my $html_output = itemify($items, 1) . $end;
|
||||
my $text_output = itemify($items, 0) . $end;
|
||||
|
||||
return $text_output, html => $html_output;
|
||||
# Check if a value exists given our key.
|
||||
if(exists $desserts{$letter}) {
|
||||
my $value = $desserts{$letter};
|
||||
|
||||
# Check if a certain dessert begins with our query string.
|
||||
for my $dessert (@{$value}) {
|
||||
if($dessert =~ /^$query/i) {
|
||||
push @items, $dessert;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return @items;
|
||||
}
|
||||
|
||||
handle remainder => sub {
|
||||
# Check the query. See if it matches this regular expression.
|
||||
if(lc $_ =~ m/^(?:that )?(?:start|beginn?)s?(?:ing)? (?:with)? (the letter )?([a-z].*)$/i) {
|
||||
# Check which desserts begin with this letter (or word).
|
||||
my @items = begins_with($2);
|
||||
|
||||
# Check if we found any results.
|
||||
if(@items > 0) {
|
||||
return itemify(\@items, " is a dessert that begins with '$2'.");
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue