renamed goodie, upgraded the code and made a test file

master
Konrad Scorciapino 2013-05-07 01:26:26 -03:00
parent 75e4bae8f0
commit a2bd031f44
3 changed files with 48 additions and 5 deletions

View File

@ -1,4 +1,4 @@
package DDG::Goodie::UnicodeReverse;
package DDG::Goodie::UnicodeFuzzySearch;
# ABSTRACT: returns unicode symbols matching the input
use DDG::Goodie;
@ -6,7 +6,6 @@ use DDG::Goodie;
triggers startend => "unicode";
zci is_cached => 1;
# zci answer_type => "convert_to_ascii";
attribution
github => "konr",
@ -23,10 +22,22 @@ topics 'programming';
handle remainder => sub {
return unless $_;
my $pattern = uc join('.*', $_);
# UnicodeData.txt is a semicolon-separated file.
# Uploaded file version: 6.3.0, obtained from
# ftp://ftp.unicode.org/Public/6.3.0/ucd/
my @lines = split /\n/, share("UnicodeData.txt")->slurp;
my @matches = grep { /^[^;]*;?[^;]*$pattern/ } @lines;
# 1st column = number ; 2nd column = name. See
# http://www.unicode.org/draft/ucd/UnicodeData.html
my @matches;
# AS FUZZY AS POSSIBLE BUT NOT MORE - It's either (a) number (no ';'
# before) or (b) part of the name or, when there are way too many
# matches for the result to be helpful, (c) isolated words.
@matches = grep { /^[^;]*;?[^;]*$pattern/ } @lines;
@matches = grep { /\b$pattern\b/ } @lines if (scalar @matches >= 50);
return unless (scalar @matches > 0 && scalar @matches < 50);
@ -37,7 +48,6 @@ handle remainder => sub {
name => $name};
} @matches;
my @results = map {sprintf('%s: %s (U+%s)', @{$_}{qw/name symbol code/})} @matches;
return join("\n", @results), html => join("<br>", @results);

33
t/UnicodeFuzzySearch.t Normal file
View File

@ -0,0 +1,33 @@
#!/usr/bin/env perl
#use encoding "utf8";
use strict;
use warnings;
use Test::More;
use DDG::Test::Goodie;
zci is_cached => 1;
zci answer_type => "unicodefuzzysearch";
ddg_goodie_test(
[qw(
DDG::Goodie::UnicodeFuzzySearch
)],
# ------
"unicode white heart" => test_zci("WHITE HEART SUIT: \x{2661} (U+2661)", html => "WHITE HEART SUIT: \x{2661} (U+2661)"),
"0f00 unicode" => test_zci("TIBETAN SYLLABLE OM: \x{0f00} (U+0F00)", html => ("TIBETAN SYLLABLE OM: \x{0f00} (U+0F00)")),
# ------
"unicode snowman" =>
test_zci("SNOWMAN: \x{2603} (U+2603)
SNOWMAN WITHOUT SNOW: \x{26c4} (U+26C4)
BLACK SNOWMAN: \x{26c7} (U+26C7)",
html => "SNOWMAN: \x{2603} (U+2603)<br>SNOWMAN WITHOUT SNOW: \x{26c4} (U+26C4)<br>BLACK SNOWMAN: \x{26c7} (U+26C7)"),
# ------
"sharp s unicode" =>
test_zci("LATIN SMALL LETTER SHARP S: \x{00df} (U+00DF)
LATIN CAPITAL LETTER SHARP S: \x{1e9e} (U+1E9E)
MUSIC SHARP SIGN: \x{266f} (U+266F)",
html => "LATIN SMALL LETTER SHARP S: \x{00df} (U+00DF)<br>LATIN CAPITAL LETTER SHARP S: \x{1e9e} (U+1E9E)<br>MUSIC SHARP SIGN: \x{266f} (U+266F)"),
);
done_testing;