Build A Simple Research Assistant With Python

python research assistant img

PYTHON RESEARCH ASSISTANT 101

Among other great stuffs we can do with python, research and information sourcing can be done better and faster with python. In this tutorial we are going to build a simple research assistant with a few lines of code.

Meanwhile, we’ll be using a few python modules like Wikipedia and Pywhatkit to make this brief project fun and effective.

REQUIRED KNOWLEDGE.

The difficulty rate for this particular Python Research Assistant is beginner/medium, and what’s required is not that much, once you already know the basics of python like lists, importing of modules, python scope, indentation, for and while loops, try and except functions and a few other similar concepts, then this should be fun..

IN THIS TUTORIAL YOU’LL LEARN:

  • How to import python modules
  • How to use the wikipedia module to get basic information about anything quick
  • How to use the Pywhatkit module to get basic information about anything quick, play a youtube video and even more
  • How to create and run functions in python
  • How to use for and while loops alongside with try and except

Let the coding begin!

First off, let’s start with Pywhatkit: this is a Python library with various helpful features. It is an easy to use library which does not require you to do some additional setup.

Pywhatkit doesn’t come preinstalled with python, but can install it with pip or pip3 by simply running: pip install pywhakit on your command line.

Then import the module whenever you want to use it.

import pywhatkit as kit

On the code above we imported pywhatkit as kit, you can import yours as whatever makes coding easy, it could be pk or anything similar. But remember that your code should always follow conventions, someone else will probably work on codes in future.

This module could also be used to send images and messages via whatsapp, play youtube videos, search the web, convert text to handwritten images and more. But for this brief tutorial we are basically going to use it to make a simple research assistant.

To get the full documentation on pywhatkit module click here

Now let’s see how this works:

#import pywhatkit
import pywhatkit as kit

#Get information about python in 5 lines/paragraphs
kit.info('python', 5)

The code above will give the following output:

Python is an interpreted high-level general-purpose programming language. Its design philosophy emphasizes code readability with its use of significant indentation. Its language constructs as well as its object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.Python is dynamically-typed and garbage-collected. It supports multiple programming paradigms, including structured (particularly, procedural), object-oriented and functional programming. It is often described as a "batteries included" language due to its comprehensive standard library.Guido van Rossum began working on Python in the late 1980s, as a successor to the ABC programming language, and first released it in 1991 as Python 0.9.0.

Normally you’d want to save the output to a variable or to a file. However, before you do that, try and check the return type first. Let’s use a different approach so we can see the return type.

import pywhatkit as kit

sourced = kit.info('python', 5)
print('\n'*2)
print(type(sourced))

See output below:

Python is an interpreted high-level general-purpose programming language. Its design philosophy emphasizes code readability with its use of significant indentation. Its language constructs as well as its object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.Python is dynamically-typed and garbage-collected. It supports multiple programming paradigms, including structured (particularly, procedural), object-oriented and functional programming. It is often described as a "batteries included" language due to its comprehensive standard library.Guido van Rossum began working on Python in the late 1980s, as a successor to the ABC programming language, and first released it in 1991 as Python 0.9.0.



<class 'NoneType'>

The output below means that the return type is ‘None’

In essence, if you’d like to do something useful with this sourced information then you should rather use wikipedia.

It’s actually similar to pywhatkit. You just install with pip: pip install wikipedia via command line, then you import the module.

import wikipedia as wk

Wikipedia module has some good features just like pywhatkit that’d be useful for any python research assistant program like .summary, .search, .page. W e can even change/set the language to the language of our choice with the .set_lang.

Now let’s see them in action:

import wikipedia as wk

sourced = wk.summary('python', sentences = 5)
print(sourced)

Output:

Python is an interpreted high-level general-purpose programming language. Its design philosophy emphasizes code readability with its use of significant indentation. Its language constructs as well as its object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.Python is dynamically-typed and garbage-collected. It supports multiple programming paradigms, including structured (particularly, procedural), object-oriented and functional programming. It is often described as a "batteries included" language due to its comprehensive standard library.Guido van Rossum began working on Python in the late 1980s, as a successor to the ABC programming language, and first released it in 1991 as Python 0.9.0.

Apparently both pywhatkit.info and wikipedia.summary are using the same knowledge base which is the official wikipedia, so you should not put any phrase or words that you wouldn’t normally search on wikipedia. It’s not google search engine. You may use .search instead of .summary to get search results from google(list format).

Now let’s make the code reusable and more fun.


import wikipedia as wk

def find():
    while True:
        try:
            word, sentence = input('Enter a word, space, followed by the number of lines you want(in figures):\n ').split()
            print(f'\nSearching wikipedia for {word}...\n')
            sourced = wk.summary(word, int(sentence))
            
            print(sourced)

        except:
            print('Please make sure you entered a wikipedia compatible valid information with the following format: Programming 5. Separating the inputs with a space only.')

        else:
            print('\n......................................................\nResearch done!')
            break
    
find()

Output:


Enter a word, space, followed by the number of lines you want(in figures):
 programming 4

Searching wikipedia for programming...

Computer programming is the process of designing and building an executable computer program to accomplish a specific computing result or to perform a specific task. Programming involves tasks such as: analysis, generating algorithms, profiling algorithms' accuracy and resource consumption, and the implementation of algorithms in a chosen programming language (commonly referred to as coding). The source code of a program is written in one or more languages that are intelligible to programmers, rather than machine code, which is directly executed by the central processing unit. The purpose of programming is to find a sequence of instructions that will automate the performance of a task (which can be as complex as an operating system) on a computer, often for solving a given problem.

......................................................
Research done!

In the code above, anytime the function is ran, it will take a desired language, word and sentences to get some information about the word entered. However, we use try and except to avoid error and also to continue asking for the right inputs.

The try and except code will work for all the associated error, including when a non wikipedia compatible word is entered or when there’s a typo error.

Feel free to change the methods and tweak things with .search, .set_lang or any other method. In the last code for this tutorial, we’ll make the function to ask for a language and also set it. We’ll use just two extra language which is french(fr) and spanish(es). Feel free to add multiple languages to yours.

However, any correct language code added will still work with the function below, to see other language codes visit this page.

#import wikipedia
import wikipedia as wk

# #create a function to ask for language, word and sentence lines. Use the inputs to generate basic information
# for the word entered
def find():
    
    while True:
        try:
            lang = input('Please choose a language: "fr" for france, "es" for spanish and "en" for english:\n')
            wk.set_lang(lang)
            
            word, sentence = input('Enter a word, space, followed by the number of lines you want(in figures):\n ').split()
            print(f'\nSearching wikipedia for {word}...\n')
            sourced = wk.summary(word, int(sentence))
            
            print(sourced)

        except:
            print('Please make sure you entered a wikipedia compatible valid information with the following format: Programming 5. Separating the inputs with a space only. Also make sure you selected a valid LANGUAGE!')

        else:
            print('\n......................................................\nResearch done!')
            break
    
#run the function
find()

CONCLUSIONS:

This is just a simple research assistant program with python. Expect something more advanced on this same topic in the future. All you’ve got to do is signup for our newsletter to say in the loop.

However, If you happen to run into any puzzle or challenge on this tutorial, please don’t hesitate to write me, or leave a comment at comment section.

To automatically get the first hand information when the next post and subsequent ones gets published, subscribe to techswitch newsletter or join us on telegram.

Get an affordable and seamless python one on one training today from anywhere in the world, location is never a barrier, we have friendly learning tools to make your python programming training a worthwhile experience.

🤞 Don’t miss the tips!

We don’t spam! Read more in our privacy policy

Geoff

Geoff is a software engineer, web content specialist, tech private trainer and an IT virtual assistant.

Leave a Reply