Coders Packet

Interactive Wikipedia Science Explorer using python

By Bokkasam Eswarsai

The "Interactive Wikipedia Science Explorer" is a Python project that offers an intuitive graphical interface for exploring science-related articles on Wikipedia.

The "Wikipedia Science Search GUI" is a Python project that offers an intuitive graphical interface for exploring science-related articles on Wikipedia. Utilizing the Wikipedia API, the application allows users to input search queries, offering smart suggestions through a closest-match algorithm. Upon selecting or finding a match, the app displays the article's title, a concise summary, and a clickable URL to the full page. Users can easily clear the search results for a new query. The project not only introduces beginners to GUI programming using the tkinker library but also highlights the potential of APIs for data retrieval and manipulation. It serves as a practical tool for students, researchers, and enthusiasts to swiftly access valuable scientific information from Wikipedia's vast repository.

Package Installation:

The wikipedia-api library is a Python package that provides a simple and convenient way to interact with Wikipedia's API and retrieve information from Wikipedia's vast collection of articles. It acts as a wrapper around the Wikipedia API, abstracting away many of the complexities of making API requests and handling responses. Here's an explanation of how to install and use the wikipedia-api library:

 

pip install wikipedia-api

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Libraries:

 

import wikipediaapi
from difflib import get_close_matches
import tkinter as tk
from tkinter import messagebox

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Wikipedia GUI:

class WikipediaApp:
    def __init__(self, main):
        self.main = main
        self.main.title("Wikipedia Search Using Python")
        self.main.configure(bg="light gray")
        
        self.wiki= wikipediaapi.Wikipedia('science','en')
        
        self.search_label = tk.Label(main, text="Enter query for wikipedia search:",font=('Times New Roman',16,'bold'))
        self.search_label.pack(pady=20)
        
        self.search_entry = tk.Entry(main)
        self.search_entry.pack()
        
        self.search_button = tk.Button(main, text="Search", command=self.search_wikipedia,font=("Times New Roman",14,"bold"),fg="blue")
        self.search_button.pack(pady=20)
        
        self.clear_button = tk.Button(main, text="Clear", command=self.clear_result,font=("Times New Roman",14,"bold"),fg="red")
        self.clear_button.pack(pady=10)
        
        self.result = tk.Text(main, wrap=tk.WORD, width=70, height=20,font=('Times New Roman',14))
        self.result.pack()
        
    def setup_tags(self):
        self.result.tag_configure("title", font=("Times New Roman", 16, "bold"),pady=30)
        self.result.tag_configure("summary", font=("Times New Roman", 12),pady=30)
        self.result.tag_configure("url", font=("Times New Roman", 10, "italic"), foreground="blue")
        

Search Query:

def search_wikipedia(self):
        query = self.search_entry.get()
        if query:
            closest_match = self.find_closest_match(query)
            
            if closest_match:
                page = self.wiki.page(closest_match)
                self.display_result(page)
            else:
                messagebox.showerror("No Results", "No matching page found!")
        else:
            messagebox.showerror("Empty Query", "Please enter query to search.")
            
    def find_closest_match(self, query):
        search_results = self.wiki.page(query).backlinks
        if search_results:
            closest_match = get_close_matches(query, search_results.keys())
            if closest_match:
                return closest_match[0]
        return None
    
    def display_result(self, page):
        self.result.delete(1.0, tk.END)
        self.result.insert(tk.END, f"Title: {page.title}\n\n")
        summary_sentences = page.summary.split('. ')
        if len(summary_sentences) > 0:
            self.result.insert(tk.END, f"Summary: {summary_sentences[0]}\n\n", "summary")
        self.result.insert(tk.END, f"URL: {page.fullurl}\n")
    def clear_result(self):
        self.search_entry.delete(0, tk.END)
        self.result.delete(1.0, tk.END)

Finding the closest match of that Query:

def find_closest_match(self, query):
        search_results = self.wiki.page(query).backlinks
        if search_results:
            closest_match = get_close_matches(query, search_results.keys())
            if closest_match:
                return closest_match[0]
        return None
    
    def display_result(self, page):
        self.result.delete(1.0, tk.END)
        self.result.insert(tk.END, f"Title: {page.title}\n\n")
        summary_sentences = page.summary.split('. ')
        if len(summary_sentences) > 0:
            self.result.insert(tk.END, f"Summary: {summary_sentences[0]}\n\n", "summary")
        self.result.insert(tk.END, f"URL: {page.fullurl}\n")
    def clear_result(self):
        self.search_entry.delete(0, tk.END)
        self.result.delete(1.0, tk.END)
        
main = tk.Tk()
main.state('zoomed')
app = WikipediaApp(main)
main.mainloop()

Detail Explanation of Code:

Certainly, I'll provide a detailed explanation of the code for the "Wikipedia Science Search GUI" project:

1. **Imports and Library Usage**:
- The code begins by importing necessary libraries: `wikipediaapi` for interacting with Wikipedia, `get_close_matches` from `difflib` for finding close matches to user queries, and `tkinter` for creating the GUI.

2. **Class Definition - WikipediaApp**:
- The `WikipediaApp` class is defined to encapsulate the entire application.

3. **Initialization (`__init__`)**:
- The constructor initializes the main application window, sets its title, and configures its background color.
- It creates an instance of the Wikipedia API (`wikipediaapi.Wikipedia`) focused on the "science" category in English.

4. **Creating GUI Elements**:
- The constructor creates various GUI elements using `tk.Label`, `tk.Entry`, `tk.Button`, and `tk.Text` widgets.
- The interface consists of a label prompting the user to enter a search query, an entry field for the query, a "Search" button to initiate the search, a "Clear" button to reset results, and a text widget to display the search results.

5. **Setting Up Tags (`setup_tags`)**:
- A method to configure tags for different styles of text within the `tk.Text` widget. In this case, it sets up tags for title, summary, and URL text.

6. **Search Wikipedia (`search_wikipedia`)**:
- This method is triggered when the "Search" button is clicked.
- It retrieves the user's query from the entry field.
- If a query is provided, it attempts to find the closest matching article using `find_closest_match` method.
- If a match is found, it retrieves the Wikipedia page for the closest match and displays the result using the `display_result` method.
- If no match is found, it displays an error message.

7. **Finding Closest Match (`find_closest_match`)**:
- This method uses the Wikipedia API to retrieve backlinks (related articles) for the user's query.
- It employs the `get_close_matches` function to find the closest match among the retrieved backlinks.
- If a match is found, it returns the closest match; otherwise, it returns `None`.

8. **Displaying Result (`display_result`)**:
- This method clears the text widget and populates it with the title, summary, and URL of the selected Wikipedia page.
- The title and URL are displayed normally, while the summary text is displayed with a custom tag for consistent styling.

9. **Clearing Results (`clear_result`)**:
- This method resets the search query entry field and clears the text widget.

10. **Main Application Setup and Execution**:
- The script initializes the main application window using `tk.Tk()`.
- The window is maximized using the `.state('zoomed')` method call.
- An instance of the `WikipediaApp` class is created, passing the main application window as an argument.
- The `mainloop()` call starts the event loop, allowing the GUI to interact with user actions.

The "Wikipedia Science Search GUI" project demonstrates the integration of GUI elements with data retrieval from Wikipedia's API. It enables users to search for scientific articles, suggests close matches, displays essential details, and provides a user-friendly experience for exploring Wikipedia's science-related content. The project combines foundational concepts in GUI programming, API usage, and text manipulation, making it an educational and practical example of how programming can enhance information access and interaction.

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Output:

The picture below demonstrates how to access a detailed explanation of our search query by copying and pasting the URL into a browser.

 

 

Download Complete Code

Comments

No comments yet

Download Packet

Reviews Report

Submitted by Bokkasam Eswarsai (Eswarsai26)

Download packets of source code on Coders Packet