Weinberg Alpha This page contains open-sourced code that I use to integrate WolframAlpha (a knowledge engine) into DuckDuckGo (a search engine).
This integration consists of the following Perl regular expressions to identify query types that WolframAlpha answers well. These queries then get proxied via the WWW::WolframAlpha Perl module, which I also wrote and have open-sourced.
Why WeinbergAlpha? My name is Gabriel Weinberg. It's an homage.
Chemical Formulas
Queries consisting fully of one or more chemical element blocks, e.g. Si4 or O8.
$query_with_no_spaces =~ /^(?:(?:\()*(?=[ABCDEFGHIKLMNOPRSTUVWXYZ])(?:S[gnbmreic]?|N[eoabdip]?|G[dae]|B[ariehk]?|U(?:u[ptsohq])?|P[troamdbu]?|M[dgton]|A[ltrmgcus]|I[nr]?|R[bengaufh]|C[rnldumofsae]?|L[ruia]|E[rus]|T[hlacemib]|D[ys]|H[eogsaf]?|F[emr]?|Kr?|Yb?|Z[rn]|Os?|[VW]|Xe)(?:\d|\))*)+$/o
Examples
Conversions
Queries with a number, unit, conversion word and conversion target.
$query =~ /^(?:convert\s|)[\-\+]?[\d\,\.]+\s*.+\s(?:in|to|in ?to)\s.+$/io

$query =~ /^.+\s?[\-\+]?[\d\,\.]+\s(?:in|to|in ?to)\s.+$/io
General Answers
Queries starting/ending with keywords WA answers well.
$query =~ /^(?:mass|radius|density|distance|elevation|population|age|(?:birth|death)\s?(?:year|date)|weight|sun(?:rise|set)|calories)\s+.*$/io

$query =~ /^.*\s+(?:mass|radius|density|distance|elevation|population|age|(?:birth|death)\s?(?:year|date)|weight|sun(?:rise|set)|calories)$/io
Holidays
Queries matching holiday names (using a lower case and punctuation free version of $query).
my %holidays = (
    'mlk day' => undef,
    'martin luther king day' => undef,
    'martin luther kings birthday' => undef,
    'presidents day' => undef,
    'washingtons birthday' => undef,
    'memorial day' => undef,
    'independence day' => undef,
    'labor day' => undef,
    'columbus day' => undef,
    'veterans day' => undef,
    'thanksgiving' => undef,
    'fathers day' => undef,
    'mothers day' => undef,
    'inauguration day' => undef,
    'ash wednesday' => undef,
    'shrove tuesday' => undef,
    'groundhog day' => undef,
    'saint patricks day' => undef,
    'easter' => undef,
    'earth day' => undef,
    'arbor day' => undef,
    'cinco de mayo' => undef,
    'st patricks day' => undef,
    'flag day' => undef,
    'rosh hashanah' => undef,
    'yom kippur' => undef,
    'halloween' => undef,
    'election day' => undef,
    'hanukkah' => undef,
    );

exists $holidays{$query}

($query =~ /^(?:date\s|day\s|\d{4}\s)+(?:of\s|)(.*)$/o 
|| $query =~ /^(.*)\s(?:date|day|\d{4})$/o) 
&& exists $holidays{$1}
Math
Queries with complicated math (I do simple calcs myself).
$query =~ /^\d+\!$/o # factorial

$query =~ /(?:\d|\b(?:pi|[xX]|e)\b)/o && $query =~ /\b(?:sqrt|sin|cos|tan|sec|csc|cot|arcsin|arccis|arctan|arccsc|sinh|mod|cosh|tanh|csch|arsinh|arccsch|ln|log|lg|exp|int|choose|integral|integrate|sum|derivative|\% of|=)\b/io
Examples
Time
Queries about time.
$query =~ /^(?:current\s|what\s|)(?:time|clock)\s/io

$query =~ /\s(?:current\s|what\s|)(?:time|clock)$/io
Notes
  • You can test regexp using DuckDuckGo. Also note the perlre man page.
  • The background on this page is © Wolfram Alpha LLC.
  • Here's the license (MIT) for the above software:
 Copyright (c) 2010 Duck Duck Go, Inc.

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
Here are some areas where I could use some help:
If you think of anything, please let me know.