zeroclickinfo-goodies/lib/DDG/Goodie/Chars.pm

69 lines
1.8 KiB
Perl

package DDG::Goodie::Chars;
# ABSTRACT: Give the number of characters (length) of the query.
use strict;
use DDG::Goodie;
use Text::Trim;
triggers startend =>
'chars',
'number of characters',
'number of chars',
'num chars',
'num characters',
'char count',
'character count',
'characters count',
'length of string',
'length in characters',
'length in chars';
zci answer_type => "chars";
zci is_cached => 1;
name 'Character Counter';
description 'Count the number of charaters in a query';
primary_example_queries 'chars in "my string"';
code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/Chars.pm';
category 'computing_tools';
topics 'programming';
handle remainder => sub {
my ($str) = @_;
return if !$str;
# remove leading word 'in',
# e.g. 'chars in mississippi' would just count the string 'mississippi'.
$str =~ s/^\s*in\b//;
# trim spaces at beg and end of string
$str = trim $str;
# if nothing left in the string, return without triggering the IA.
# this means the remainder contained only the word 'in' and/or spaces.
return if !$str;
# if surrounded by quotation marks (either ' or ")
# remove so we don't include them in the count
$str =~ s/^["'](.*)["']$/$1/;
# get the length of the string in characters
my $len = length($str);
# pluralize the word 'character' unless length is 1.
# note that this works for length=0, i.e. we'll correctly get '0 characters'.
my $characters_pluralized = ($len == 1 ? 'character' : 'characters');
# build the output string
my $text_out = qq("$str" is $len $characters_pluralized long.);
return $text_out,
structured_answer => {
input => [html_enc($str)],
operation => 'Character count',
result => $len
};
};
1;