2012-03-06 20:35:45 -08:00
|
|
|
package DDG::Goodie::TitleCase;
|
2014-08-20 11:45:33 -07:00
|
|
|
# ABSTRACT: Convert a string to title case.
|
2012-03-06 20:35:45 -08:00
|
|
|
|
2015-02-22 12:09:29 -08:00
|
|
|
use strict;
|
2012-03-06 20:35:45 -08:00
|
|
|
use DDG::Goodie;
|
|
|
|
|
2014-10-07 00:49:21 -07:00
|
|
|
triggers start => 'titlecase', 'title case';
|
2012-05-23 19:05:08 -07:00
|
|
|
|
2012-03-20 21:08:12 -07:00
|
|
|
zci answer_type => "title_case";
|
2014-10-07 00:49:21 -07:00
|
|
|
zci is_cached => 1;
|
2012-03-06 20:35:45 -08:00
|
|
|
|
2013-12-31 13:52:50 -08:00
|
|
|
# http://blog.apastyle.org/apastyle/2012/03/title-case-and-sentence-case-capitalization-in-apa-style.html
|
2014-10-07 00:49:21 -07:00
|
|
|
my %exceptions = map { $_ => 1 } ("a", "an", "and", "the", "by", "but", "for", "or", "nor", "yet", "so", "as", "at", "in", "of", "on", "per", "to");
|
2013-12-31 13:52:50 -08:00
|
|
|
|
|
|
|
handle remainder => sub {
|
2014-10-07 00:49:21 -07:00
|
|
|
my $input = shift;
|
|
|
|
|
|
|
|
return unless $input;
|
|
|
|
|
|
|
|
my ($first_word, @words) = split(/\s+/, lc $input); # Normalize to lowercase and break out words.
|
|
|
|
|
|
|
|
my $title_case = join(
|
|
|
|
' ', # Single spaces between words in the result
|
|
|
|
ucfirst $first_word, # First word is always capped.
|
|
|
|
map {
|
|
|
|
($exceptions{$_})
|
|
|
|
? $_ # Exceptions left untouched.
|
|
|
|
: join('-', map { ucfirst } split(/-/, $_)) # Each part of a hyphenated word is capped.
|
|
|
|
} @words
|
|
|
|
);
|
|
|
|
|
2016-05-14 14:44:23 -07:00
|
|
|
return $title_case, structured_answer => {
|
|
|
|
data => {
|
2016-08-31 11:58:57 -07:00
|
|
|
title => $title_case,
|
|
|
|
subtitle => "Title case: $input"
|
2016-05-14 14:44:23 -07:00
|
|
|
},
|
|
|
|
templates => {
|
|
|
|
group => 'text'
|
|
|
|
}
|
|
|
|
};
|
2013-12-31 13:52:50 -08:00
|
|
|
};
|
2012-03-06 20:35:45 -08:00
|
|
|
|
2012-03-20 19:23:09 -07:00
|
|
|
1;
|