1
votes

Hello fellow programmers, I am currently building a alexa replica and I am trying to setup the question command. That being said, I am trying to get the first or the most relevant thing out of the search result.

Here is what I want to achive:

In this picture, I only want the top paragraph of wikipedia starting at "The sun is the star..." and ending with "...for life on Earth"

In this picture, I only want the value 147.6 million km

Here is my code:

from os import times
from typing import Sequence
from googlesearch import search
import speech_recognition as sr
import pyttsx3
import pywhatkit
import datetime
import wikipedia

listener = sr.Recognizer()
engine = pyttsx3.init()
voices = engine.getProperty("voices")
engine.setProperty("voice",voices[0].id)

def talk(text):
    engine.say(text)
    engine.runAndWait()

def takeCommand():
    try:
        with sr.Microphone() as source:
            print("Listening...")
            voice = listener.listen(source)
            command = listener.recognize_google(voice).lower()
            if "yoda" in command:
                command = command.replace("yoda ", "")
    except:
        pass
    return command

def commandCall():
    command = takeCommand()

    if "play" in command:
        song = command.replace("play ", "")
        talk("Now playing " + song)
        pywhatkit.playonyt(song)
    elif "time" in command:
        time = datetime.datetime.now().strftime("%I:%M %p")
        talk("It is currently " + time)
    elif "what is" or "who is" in command:
        question = wikipedia.summary(command)
        #question = search(command)
        print(question)
        talk(question)
commandCall()

In my code I am using wikipedia.summary(command) which return a wikipedia summary of the question but it is not exactly what I want.

Thank you for your time!

Is your question how to get exactly what you want from a wikipedia article? What is wrong with the summary and what are you trying to get from it - please share the code relevant specifically to your problem. Is "what you want" just a certain number of words from the start and the end of the summary?Grismar