The summary()
function is available in the Wikipedia package. This function retrieves the summary from a Wikipedia page on a particular topic. summary()
can easily get the important sentences from a complete Wikipedia page.
Before moving on: if you do not have wikipedia
installed, you can run the command below:
pip install wikipedia
Now, let’s look at the parameters accepted by the function.
The summary()
function can accept the following parameters:
query: This is the term used to get the summary of a given Wikipedia page.
sentences: This is an optional parameter. It allows you pass the number of sentences that you want in the summary. The value of this parameter cannot be greater than .
chars: This is also an optional parameter. It can accept an integer value denoting the number of characters you want in the result. Remember, your original summary could be longer, but you can set this parameter if you only want the first 100 characters.
auto_suggest: This is another optional parameter that accepts a boolean value. When you search for a query, it might be the case that there is no valid page. By specifying the value of this parameter as True, it allows Wikipedia find a valid page title for the query.
redirect: This is the last parameter for this function and also an optional parameter. It accepts boolean values where True denotes to allow redirection without raising RedirectError
.
The summary()
function returns the summary of the Wikipedia page in a plain text format.
Now, let’s take a look at how easy it is to use this function:
import wikipedia as wpquery = "Python programming language"print(wp.summary(query))print("Setting sentence = 1")print(wp.summary(query, sentences = 1))
Explanation
summary()
function, and in the output, we can see the summary.1
. In the output, we can see that now only one sentence is being returned.