Function Repository Resource:

AutocompleteSearchQuery

Source Notebook

Generate suggested text for autocompletion of a search query

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["AutocompleteSearchQuery"]["query"]

gives a suggested string to autocomplete the text "query".

ResourceFunction["AutocompleteSearchQuery"]["query",n]

gives a list of the top n results.

ResourceFunction["AutocompleteSearchQuery"][n]

represents an operator form of ResourceFunction["AutocompleteSearchQuery"] that can be applied to a string.

Details and Options

ResourceFunction["AutocompleteSearchQuery"] uses the Google search API to get results, so it requires an internet connection in order to function.
In ResourceFunction["AutocompleteSearchQuery"]["query",n], the value for n can be any of the following:
a positive integerthe top n results found
Allall found suggestions
UpTo[m]at most m results, as available
Automaticreturn a string equivalent to using the one-argument form

Examples

Basic Examples (4) 

Autocomplete search queries:

In[1]:=
ResourceFunction["AutocompleteSearchQuery"]["wolfram"]
Out[1]=
In[2]:=
ResourceFunction["AutocompleteSearchQuery"]["wolfram fun"]
Out[2]=
In[3]:=
ResourceFunction["AutocompleteSearchQuery"]["does this"]
Out[3]=
In[4]:=
ResourceFunction["AutocompleteSearchQuery"]["yes it d"]
Out[4]=

Specify the number of autocompletions to get:

In[5]:=
ResourceFunction["AutocompleteSearchQuery"]["is ", 5]
Out[5]=
In[6]:=
ResourceFunction["AutocompleteSearchQuery"]["autocorrect ", 3]
Out[6]=

Get all available autocompletions:

In[7]:=
ResourceFunction["AutocompleteSearchQuery"]["how do I get all", All]
Out[7]=

Use the operator form of AutocompleteSearchQuery:

In[8]:=
ResourceFunction["AutocompleteSearchQuery"][All]["wolfram"]
Out[8]=

Applications (2) 

Generate a list of questions:

In[9]:=
ResourceFunction[
 "AutocompleteSearchQuery"] /@ {"who ", "what ", "where ", "when ", "why ", "how "}
Out[9]=

Find common search queries from random words:

In[10]:=
AssociationMap[ResourceFunction["AutocompleteSearchQuery"][3], RandomWord[20]] // ResourceFunction["NiceGrid"]
Out[10]=

Properties and Relations (5) 

AutocompleteSearchQuery supports using UpTo:

In[11]:=
ResourceFunction["AutocompleteSearchQuery"]["func", UpTo[3]]
Out[11]=

AutocompleteSearchQuery does not return any completions for an empty string:

In[12]:=
ResourceFunction["AutocompleteSearchQuery"][""]
Out[12]=
In[13]:=
ResourceFunction["AutocompleteSearchQuery"]["", All]
Out[13]=

The query can contain characters that are not letters:

In[14]:=
ResourceFunction["AutocompleteSearchQuery"]["1+"]
Out[14]=

Results may be dependent on location:

In[15]:=
GeoNearest["USState", Here] -> ResourceFunction["AutocompleteSearchQuery"]["red"]
Out[15]=

Compare to evaluating from a different physical location:

In[16]:=
CloudEvaluate[
 GeoNearest["USState", Here] -> ResourceFunction["AutocompleteSearchQuery"]["red"]]
Out[16]=

AutocompleteSearchQuery can handle spelling errors in the query:

In[17]:=
ResourceFunction["AutocompleteSearchQuery"]["woolfram", All]
Out[17]=
In[18]:=
ResourceFunction["AutocompleteSearchQuery"]["wolf ram", All]
Out[18]=

Possible Issues (4) 

Requesting more completions than are available will result in a partial list along with a warning message:

In[19]:=
ResourceFunction["AutocompleteSearchQuery"]["this is too many", 20]
Out[19]=

The generated text will always be lowercase:

In[20]:=
ResourceFunction["AutocompleteSearchQuery"]["HELLO WO"]
Out[20]=
In[21]:=
ResourceFunction["AutocompleteSearchQuery"]["Wolfram Fun"]
Out[21]=

Case is only preserved if the query appears at the beginning of the output string:

In[22]:=
ResourceFunction["AutocompleteSearchQuery"]["WOOLFRAM "]
Out[22]=
In[23]:=
ResourceFunction["AutocompleteSearchQuery"]["WOLFRAM "]
Out[23]=

AutocompleteSearchQuery requires an internet connection:

In[24]:=
Block[{PacletManager`$AllowInternet = False},
 ResourceFunction["AutocompleteSearchQuery"]["no internet"]
 ]
Out[24]=
In[25]:=
ResourceFunction["AutocompleteSearchQuery"]["no internet"]
Out[25]=

Neat Examples (2) 

Use AutocompleteSearchQuery as a FieldCompletionFunction:

In[26]:=
InputField["", String, FieldCompletionFunction -> (ResourceFunction[
     "AutocompleteSearchQuery"][#, All] &)]
Out[26]=

Have a bird predict what you’re going to say next:

In[27]:=
DynamicModule[{query = ""}, Column[{InputField[Dynamic[query], String, FieldHint -> "Type anything here", ContinuousAction -> True], ResourceFunction["BirdSay"][
    Dynamic[Replace[
      ResourceFunction["AutocompleteSearchQuery"][query], "" -> "I'm waiting\[Ellipsis]"], SynchronousUpdating -> False]]}]]
Out[27]=

Version History

  • 2.0.0 – 14 November 2019
  • 1.0.0 – 05 November 2019

Related Resources

License Information