DEV Community

Cover image for BUILD A SIMPLE RESEARCH ASSISTANT WITH PYTHON
Geof
Geof

Posted on

BUILD A SIMPLE RESEARCH ASSISTANT WITH PYTHON

research gify img
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.

Firstly Let's Make Use Of Pywhatkit

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

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

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
import pywhatkit as kit

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

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.
Enter fullscreen mode Exit fullscreen mode

That sounds like fun right? However, the 5 is for number of lines you need, you change it to whatever number you need.
But there is a little limitation here: Normally you’d want to save the output to a variable or to a file and probably do something with it. However, before you do that, try and check the return type first. If you save the search result to a variable and print the type, it'll be NonetType

Something like this:

import pywhatkit as kit

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

Fortunately there is another better way, in case you'd like to save your search results to a variable, file or even send it as an email then you should use Wikipedia instead

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

Working With Wikipedia

Now let's code up something real fun:

#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()
Enter fullscreen mode Exit fullscreen mode

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

If haven't used the wikipedia library before, try and read the documentation here, it's actually precise and easy to understand, I strongly believe reading things up when necessary should be a major part of every programmer's skills.

OK guys.. This is just a simple research assistant program with python. Expect something more advanced on this same topic in the future.

If you'd like a more explained version of this post please visit the post page on my blog here.

Finally am kinda new here and this happens to be my first post on Dev, I'll be publishing more pythonic posts and your following up would be appreciated and would also make you see more of my posts.

Discussion (0)