Cleanup Chinese Zodiac IA

- Remove meta-data from code
- Simplify attribute passing
- Use interpolation instead of concatenation
master
Mariano Simone 2016-05-21 16:19:59 -07:00 committed by marianosimone
parent 4406404927
commit b2d4251ab6
2 changed files with 45 additions and 52 deletions

View File

@ -11,31 +11,22 @@ use utf8;
triggers any => 'chinese zodiac', 'shēngxiào', 'shengxiao', 'shēng xiào', 'sheng xiao';
zci is_cached => 0;
name 'Chinese Zodiac';
description 'Return the Chinese zodiac animal for a given year';
primary_example_queries 'chinese zodiac for 1969';
secondary_example_queries '2004 chinese zodiac animal', 'what was the chinese zodiac animal in 1992', 'what will the chinese zodiac animal be for 2056', 'last year\'s chinese zodiac';
category 'dates';
topics 'special_interest';
code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/ChineseZodiac.pm';
attribution github => ['http://github.com/wilkox', 'wilkox'],
github => ['https://github.com/Sloff', 'Sloff'];
my %animal_to_language_and_path_and_class = (
'hare' => { en => 'Rabbit', zh => '兔' , path => "share/goodie/chinese_zodiac/rabbit.png", class => "bg-clr--wood"},
'dragon' => { en => 'Dragon', zh => '龙' , path => "share/goodie/chinese_zodiac/dragon.png", class => "bg-clr--green"},
'snake' => { en => 'Snake', zh => '蛇' , path => "share/goodie/chinese_zodiac/snake.png", class => "bg-clr--red"},
'horse' => { en => 'Horse', zh => '马' , path => "share/goodie/chinese_zodiac/horse.png", class => "bg-clr--red"},
'sheep' => { en => 'Goat', zh => '羊' , path => "share/goodie/chinese_zodiac/goat.png", class => "bg-clr--green"},
'monkey' => { en => 'Monkey', zh => '猴' , path => "share/goodie/chinese_zodiac/monkey.png", class => "bg-clr--grey"},
'fowl' => { en => 'Rooster', zh => '鸡' , path => "share/goodie/chinese_zodiac/rooster.png", class => "bg-clr--grey"},
'dog' => { en => 'Dog', zh => '狗' , path => "share/goodie/chinese_zodiac/dog.png", class => "bg-clr--green"},
'pig' => { en => 'Pig', zh => '猪' , path => "share/goodie/chinese_zodiac/pig.png", class => "bg-clr--blue-light"},
'rat' => { en => 'Rat', zh => '鼠' , path => "share/goodie/chinese_zodiac/rat.png", class => "bg-clr--blue-light"},
'ox' => { en => 'Ox', zh => '牛' , path => "share/goodie/chinese_zodiac/ox.png", class => "bg-clr--green"},
'tiger' => { en => 'Tiger', zh => '虎' , path => "share/goodie/chinese_zodiac/tiger.png", class => "bg-clr--wood"}
my %animal_attributes = (
'hare' => { en => 'Rabbit', zh => '兔' , icon => "rabbit.png", class => "bg-clr--wood"},
'dragon' => { en => 'Dragon', zh => '龙' , icon => "dragon.png", class => "bg-clr--green"},
'snake' => { en => 'Snake', zh => '蛇' , icon => "snake.png", class => "bg-clr--red"},
'horse' => { en => 'Horse', zh => '马' , icon => "horse.png", class => "bg-clr--red"},
'sheep' => { en => 'Goat', zh => '羊' , icon => "goat.png", class => "bg-clr--green"},
'monkey' => { en => 'Monkey', zh => '猴' , icon => "monkey.png", class => "bg-clr--grey"},
'fowl' => { en => 'Rooster', zh => '鸡' , icon => "rooster.png", class => "bg-clr--grey"},
'dog' => { en => 'Dog', zh => '狗' , icon => "dog.png", class => "bg-clr--green"},
'pig' => { en => 'Pig', zh => '猪' , icon => "pig.png", class => "bg-clr--blue-light"},
'rat' => { en => 'Rat', zh => '鼠' , icon => "rat.png", class => "bg-clr--blue-light"},
'ox' => { en => 'Ox', zh => '牛' , icon => "ox.png", class => "bg-clr--green"},
'tiger' => { en => 'Tiger', zh => '虎' , icon => "tiger.png", class => "bg-clr--wood"}
);
my $goodie_version = $DDG::GoodieBundle::OpenSourceDuckDuckGo::VERSION // 999;
my $chinese_zodiac_tz = 'Asia/Shanghai';
my $descriptive_datestring_regex = descriptive_datestring_regex();
my $formatted_datestring_regex = formatted_datestring_regex();
@ -79,26 +70,26 @@ handle remainder => sub {
my $year_start = chinese_new_year_before($date_gregorian->add(days => 1))->set_time_zone($chinese_zodiac_tz);
my $year_end = chinese_new_year_after($date_gregorian)->subtract(days => 1)->set_time_zone($chinese_zodiac_tz);
my $animal = $year_chinese->zodiac_animal;
my $english = $animal_to_language_and_path_and_class{$animal}{'en'};
my $character = $animal_to_language_and_path_and_class{$animal}{'zh'};
my $path = $animal_to_language_and_path_and_class{$animal}{'path'};
my $class = $animal_to_language_and_path_and_class{$animal}{'class'};
my $statement = $year_start->strftime("%b %d, %Y") . " " . $year_end->strftime("%b %d, %Y");
return format_answer($character, $english, $statement, $path, $class);
return format_answer($statement, $year_chinese->zodiac_animal);
};
sub format_answer {
my ($character, $english, $statement, $path, $class) = @_;
my ($statement, $animal) = @_;
my $attributes = $animal_attributes{$animal};
my $english = $attributes->{'en'};
my $character = $attributes->{'zh'};
my $path = "/share/goodie/chinese_zodiac/$goodie_version/$attributes->{'icon'}";
my $class = $attributes->{'class'};
return "$character ($english)", structured_answer => {
data => {
title => "$character ($english)",
subtitle => $statement,
image => $path,
url => 'https://en.wikipedia.org/wiki/'.$english.'_(zodiac)'
url => "https://en.wikipedia.org/wiki/$english\_(zodiac)"
},
templates => {
group => "icon",
@ -109,7 +100,7 @@ sub format_answer {
iconImage => 'large'
},
elClass => {
iconImage => $class.' circle'
iconImage => "$class circle"
}
},
meta => {

View File

@ -10,35 +10,37 @@ use utf8;
zci answer_type => 'chinesezodiac';
zci is_cached => 0;
my %animal_to_language_and_path_and_class = (
'hare' => { en => 'Rabbit', zh => '兔' , path => "share/goodie/chinese_zodiac/rabbit.png", class => "bg-clr--wood"},
'dragon' => { en => 'Dragon', zh => '龙' , path => "share/goodie/chinese_zodiac/dragon.png", class => "bg-clr--green"},
'snake' => { en => 'Snake', zh => '蛇' , path => "share/goodie/chinese_zodiac/snake.png", class => "bg-clr--red"},
'horse' => { en => 'Horse', zh => '马' , path => "share/goodie/chinese_zodiac/horse.png", class => "bg-clr--red"},
'sheep' => { en => 'Goat', zh => '羊' , path => "share/goodie/chinese_zodiac/goat.png", class => "bg-clr--green"},
'monkey' => { en => 'Monkey', zh => '猴' , path => "share/goodie/chinese_zodiac/monkey.png", class => "bg-clr--grey"},
'fowl' => { en => 'Rooster', zh => '鸡' , path => "share/goodie/chinese_zodiac/rooster.png", class => "bg-clr--grey"},
'dog' => { en => 'Dog', zh => '狗' , path => "share/goodie/chinese_zodiac/dog.png", class => "bg-clr--green"},
'pig' => { en => 'Pig', zh => '猪' , path => "share/goodie/chinese_zodiac/pig.png", class => "bg-clr--blue-light"},
'rat' => { en => 'Rat', zh => '鼠' , path => "share/goodie/chinese_zodiac/rat.png", class => "bg-clr--blue-light"},
'ox' => { en => 'Ox', zh => '牛' , path => "share/goodie/chinese_zodiac/ox.png", class => "bg-clr--green"},
'tiger' => { en => 'Tiger', zh => '虎' , path => "share/goodie/chinese_zodiac/tiger.png", class => "bg-clr--wood"}
my $goodie_version = $DDG::GoodieBundle::OpenSourceDuckDuckGo::VERSION // 999;
my %animal_attributes = (
'hare' => { en => 'Rabbit', zh => '兔' , icon => "rabbit.png", class => "bg-clr--wood"},
'dragon' => { en => 'Dragon', zh => '龙' , icon => "dragon.png", class => "bg-clr--green"},
'snake' => { en => 'Snake', zh => '蛇' , icon => "snake.png", class => "bg-clr--red"},
'horse' => { en => 'Horse', zh => '马' , icon => "horse.png", class => "bg-clr--red"},
'sheep' => { en => 'Goat', zh => '羊' , icon => "goat.png", class => "bg-clr--green"},
'monkey' => { en => 'Monkey', zh => '猴' , icon => "monkey.png", class => "bg-clr--grey"},
'fowl' => { en => 'Rooster', zh => '鸡' , icon => "rooster.png", class => "bg-clr--grey"},
'dog' => { en => 'Dog', zh => '狗' , icon => "dog.png", class => "bg-clr--green"},
'pig' => { en => 'Pig', zh => '猪' , icon => "pig.png", class => "bg-clr--blue-light"},
'rat' => { en => 'Rat', zh => '鼠' , icon => "rat.png", class => "bg-clr--blue-light"},
'ox' => { en => 'Ox', zh => '牛' , icon => "ox.png", class => "bg-clr--green"},
'tiger' => { en => 'Tiger', zh => '虎' , icon => "tiger.png", class => "bg-clr--wood"}
);
sub build_answer {
my ($animal, $statement) = @_;
my $character = $animal_to_language_and_path_and_class{$animal}{'zh'};
my $english = $animal_to_language_and_path_and_class{$animal}{'en'};
my $path = $animal_to_language_and_path_and_class{$animal}{'path'};
my $class = $animal_to_language_and_path_and_class{$animal}{'class'};
my $character = $animal_attributes{$animal}{'zh'};
my $english = $animal_attributes{$animal}{'en'};
my $path = "/share/goodie/chinese_zodiac/$goodie_version/$animal_attributes{$animal}->{'icon'}";
my $class = $animal_attributes{$animal}{'class'};
return test_zci("$character ($english)", structured_answer => {
data => {
title => "$character ($english)",
subtitle => $statement,
image => $path,
url => 'https://en.wikipedia.org/wiki/'.$english.'_(zodiac)'
url => "https://en.wikipedia.org/wiki/$english\_(zodiac)"
},
templates => {
group => "icon",
@ -49,7 +51,7 @@ sub build_answer {
iconImage => 'large'
},
elClass => {
iconImage => $class.' circle'
iconImage => "$class circle"
}
},
meta => {