PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/DDG/Spice/RandPOS.pm

http://github.com/duckduckgo/zeroclickinfo-spice
Perl | 39 lines | 24 code | 6 blank | 9 comment | 3 complexity | 352c81211b55d18ad5795788d9cf295d MD5 | raw file
Possible License(s): Apache-2.0
  1. package DDG::Spice::RandPOS;
  2. # ABSTRACT: Returns a random word or list of words from the specified part of speech using Wordnik.
  3. use DDG::Spice;
  4. use Text::Trim;
  5. spice from => '([^/]+)/?(?:([^/]+)/?(?:([^/]+)|)|)';
  6. spice to => 'http://api.wordnik.com/v4/words.json/randomWords?includePartOfSpeech=$1&minCorpusCount=10000&limit=$2&api_key={{ENV{DDG_SPICE_WORDNIK_APIKEY}}}&callback={{callback}}';
  7. spice proxy_cache_valid => '418 1d';
  8. spice content_type_javascript => 1;
  9. # List of all trigger words
  10. my @triggerWords = map { ($_, $_."s") } qw(noun verb adjective pronoun preposition conjunction adverb);
  11. # List of keywords used with triggers
  12. my @keywords = ("random", "example");
  13. # Join keywords for use in regexp
  14. my $keywords = join "|", @keywords;
  15. # Map keywords and triggers into a list suitable for triggers
  16. my @triggers = map { $keywords[0]." ".$_, $keywords[1]." ".$_, $_." ".$keywords[1] } @triggerWords;
  17. # Use the triggers list
  18. triggers start => @triggers;
  19. handle query_lc => sub {
  20. # Remove keywords, trim: random, example
  21. s/$keywords//g;
  22. $_ = trim($_);
  23. # Guard, returns if additional space is found, indicating a new word
  24. return if m/([\s]+)/;
  25. # Last triggerWord ends in "s" return 5 else return 1
  26. if (/s$/) {
  27. return substr($_, 0, -1), 5;
  28. } else {
  29. return $_, 1;
  30. }
  31. return;
  32. };
  33. 1;