From fd1c66408daffdf7d52c488a5141eb1209db5ba6 Mon Sep 17 00:00:00 2001 From: Matt Miller Date: Mon, 6 Oct 2014 15:13:35 +0200 Subject: [PATCH] HelpLine: add structured answer. Also, restructure data sources to make it easier for non-technical people to contribute. --- lib/DDG/Goodie/HelpLine.pm | 90 +++++++--------------------- share/goodie/help_line/helplines.yml | 51 ++++++++++++++++ share/goodie/help_line/triggers.yml | 40 +++++++++++++ t/HelpLine.t | 2 +- 4 files changed, 115 insertions(+), 68 deletions(-) create mode 100644 share/goodie/help_line/helplines.yml create mode 100644 share/goodie/help_line/triggers.yml diff --git a/lib/DDG/Goodie/HelpLine.pm b/lib/DDG/Goodie/HelpLine.pm index b74342681..295c434ad 100644 --- a/lib/DDG/Goodie/HelpLine.pm +++ b/lib/DDG/Goodie/HelpLine.pm @@ -3,92 +3,48 @@ package DDG::Goodie::HelpLine; use DDG::Goodie; -my @triggers = ( - 'suicide', - 'suicide hotline', - 'kill myself', - 'suicidal thoughts', - 'end my life', - 'suicidal thoughts', - 'suicidal', - 'suicidal ideation', - 'i want to kill myself', - 'commit suicide', - 'suicide pills', - 'suicide pill', - 'suicide prevention', - 'kill myself', - 'suicide phone number', - 'suicide hot line', - 'suicide lifeline', - 'suicide life line', - 'crisis intervention', - 'i want to die', - 'committing suicide', - 'killing myself', - 'hang myself', - 'shoot myself', - 'fastest way to kill myself', - 'i\'m suicidal', - 'how to make suicide pill', - 'how to make suicide pills', - 'make suicide pills', - 'make suicide pill', - 'best way to kill myself', - 'easiest suicide method', - 'i want to end my life', - 'suicide help', - 'suicide intervention', - 'suicide method', - 'suicide methods', - 'how to kill myself', - 'ways to kill myself', -); +use YAML::XS qw( Load ); -triggers any => @triggers; +my $triggers = Load(scalar share('triggers.yml')->slurp); + +triggers any => @$triggers; zci answer_type => 'helpline'; zci is_cached => 0; primary_example_queries 'suicide hotline'; description 'Checks if a query with the word "suicide" was made and returns a 24 hr suicide hotline.'; -attribution github => ['https://github.com/conorfl', 'conorfl'], -twitter => '@areuhappylucia'; +attribution github => ['https://github.com/conorfl', 'conorfl'], + twitter => '@areuhappylucia'; code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/Helpline.pm'; topics 'everyday'; category 'special'; source 'https://en.wikipedia.org/wiki/List_of_suicide_crisis_lines'; -my %helpline = ( - # http://www.lifeline.org.au/Get-Help/I-Need-Help-Now and http://www.kidshelp.com.au/grownups/about-this-site.php - AU => ['13 11 14 or 1800 55 1800 (kids)', 'Australia'], - # http://www.suicidepreventionlifeline.org/ and http://org.kidshelpphone.ca/en - CA => ['1-800-273-8255 or 1-800-668-6868 (kids)', 'Canada'], - # http://samaritans.org.hk/24-hour-telephone-hotline (supports English, Cantonese and Mandarin) - HK => [ '2896 0000', 'Hong Kong'], - # http://www.samaritans.org/how-we-can-help-you/contact-us - IE => ['1850 60 90 90', 'Ireland'], - # http://www.lifeline.org.nz/corp_Home_378_2001.aspx - NZ => ['0800 543 354', 'New Zealand'], - # http://www.papyrus-uk.org/, http://www.samaritans.org/, and http://www.thecalmzone.net/help/helpline/helpline-nationwide/ - GB => ['0800 068 41 41, 08457 90 90 90, or 0800 58 58 58', 'the UK'], - # http://www.samaritans.org.sg/contact/confide.htm#call_us - SG => ['1800-221 4444', 'Singapore'], - # http://www.suicidepreventionlifeline.org/ - US => ['1-800-273-TALK (8255)', 'the U.S.'], -); +my $helplines = Load(scalar share('helplines.yml')->slurp); +my %suicide_phrases = map { $_ => 1 } @$triggers; handle query_lc => sub { my $query = shift; - # Check if the query matches exatly the trigger word. - my %suicide_phrases = map { $_ => 1 } @triggers; return unless exists $suicide_phrases{$query}; # Display the result. - my $code = $loc->country_code; - return "24 Hour Suicide Hotline in " . $helpline{$code}[1] . ": " . $helpline{$code}[0] - if exists $helpline{$code}[0]; + my $helpline = $helplines->{$loc->country_code}; + return unless $helpline; + + my @contacts = @{$helpline->{contacts}}; + my $numbers_string = join(', ', map { ($_->{for_kids}) ? $_->{phone} . ' (kids)' : $_->{phone}; } @contacts); + my $operation = '24 Hour Suicide Hotline'; + $operation .= 's' if (scalar @contacts > 1); + $operation .= ' in ' . $helpline->{display_country}; + + return $operation . ": " . $numbers_string, + structured_answer => { + input => [], + operation => $operation, + result => $numbers_string, + }; }; 1; diff --git a/share/goodie/help_line/helplines.yml b/share/goodie/help_line/helplines.yml new file mode 100644 index 000000000..debf43384 --- /dev/null +++ b/share/goodie/help_line/helplines.yml @@ -0,0 +1,51 @@ +--- +AU: + contacts: + - phone: 13 11 14 + web: http://www.lifeline.org.au/Get-Help/I-Need-Help-Now + - for_kids: 1 + phone: 1800 55 1800 + web: http://www.kidshelp.com.au/grownups/about-this-site.php + display_country: Australia +CA: + contacts: + - phone: 1-800-273-8255 + web: http://www.suicidepreventionlifeline.org/ + - for_kids: 1 + phone: 1-800-668-6868 + web: http://org.kidshelpphone.ca/en + display_country: Canada +GB: + contacts: + - phone: 0800 068 41 41 + web: http://www.papyrus-uk.org/ + - phone: 08457 90 90 90 + web: http://www.samaritans.org/ + - phone: 0800 58 58 58 + web: http://www.thecalmzone.net/help/helpline/helpline-nationwide/ + display_country: the UK +HK: + contacts: + - phone: 2896 0000 + web: http://samaritans.org.hk/24-hour-telephone-hotline + display_country: Hong Kong +IE: + contacts: + - phone: 1850 60 90 90 + web: http://www.samaritans.org/how-we-can-help-you/contact-us + display_country: Ireland +NZ: + contacts: + - phone: 0800 543 354 + web: http://www.lifeline.org.nz/corp_Home_378_2001.aspx + display_country: New Zealand +SG: + contacts: + - phone: 1800-221 4444 + web: http://www.samaritans.org.sg/contact/confide.htm#call_us + display_country: Singapore +US: + contacts: + - phone: 1-800-273-TALK (8255) + web: http://www.suicidepreventionlifeline.org/ + display_country: the U.S. diff --git a/share/goodie/help_line/triggers.yml b/share/goodie/help_line/triggers.yml new file mode 100644 index 000000000..ba9660949 --- /dev/null +++ b/share/goodie/help_line/triggers.yml @@ -0,0 +1,40 @@ +--- +- suicide +- suicide hotline +- kill myself +- suicidal thoughts +- end my life +- suicidal thoughts +- suicidal +- suicidal ideation +- i want to kill myself +- commit suicide +- suicide pills +- suicide pill +- suicide prevention +- kill myself +- suicide phone number +- suicide hot line +- suicide lifeline +- suicide life line +- crisis intervention +- i want to die +- committing suicide +- killing myself +- hang myself +- shoot myself +- fastest way to kill myself +- i'm suicidal +- how to make suicide pill +- how to make suicide pills +- make suicide pills +- make suicide pill +- best way to kill myself +- easiest suicide method +- i want to end my life +- suicide help +- suicide intervention +- suicide method +- suicide methods +- how to kill myself +- ways to kill myself diff --git a/t/HelpLine.t b/t/HelpLine.t index 6cf6a86d5..d0ff672db 100644 --- a/t/HelpLine.t +++ b/t/HelpLine.t @@ -48,7 +48,7 @@ ddg_goodie_test( query_raw => "$query", location => test_location("$locations[$_]") ), - test_zci(qr/24 Hour Suicide Hotline/), + test_zci(qr/24 Hour Suicide Hotline/, structured_answer => { input => [], operation => qr/24 Hour Suicide Hotline/, result => qr/[0-9]{2}/}), } 0 .. scalar @locations - 1 } 0 .. scalar @queries - 1), (map {