Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator

What is wikipedia.summary() in Python?

Harsh Jain

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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.

Parameters

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 1010.

  • 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.

Return value

The summary() function returns the summary of the Wikipedia page in a plain text format.

Code

Now, let’s take a look at how easy it is to use this function:

import wikipedia as wp
query = "Python programming language"
print(wp.summary(query))
print("Setting sentence = 1")
print(wp.summary(query, sentences = 1))
Use wikipedia.summary() function in Python

Explanation

  • In line 1, we import the required package.
  • In line 3, we define the query term for which we want to get the summary from the Wikipedia page.
  • In line 4, we call the summary() function, and in the output, we can see the summary.
  • In line 7, we are setting the number of sentences to be returned as 1. In the output, we can see that now only one sentence is being returned.

RELATED TAGS

python
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring