Esempio n. 1
0
def wikipedia(query, gender_prefix):
    """**Endpoint** to make a **Wikipedia search** and return its **text content**.

    Args:
        query (str):            Search query.
        gender_prefix (str):    Prefix to address/call user when answering.

    Returns:
        JSON document.
    """

    global userin

    response = ""
    url = ""
    wikiresult = wikipedia_lib.search(query)
    if len(wikiresult) == 0:
        response = "Sorry, " + gender_prefix + ". But I couldn't find anything about " + query + " in Wikipedia."
    else:
        wikipage = wikipedia_lib.page(wikiresult[0])
        wikicontent = TextToAction.fix_the_encoding_in_text_for_tts(
            wikipage.content)
        wikicontent = re.sub(r'\([^)]*\)', '', wikicontent)
        response = " ".join(sentence_segmenter(wikicontent)[:3])
        url = wikipage.url
    data = {}
    data['response'] = response
    data['url'] = url
    return json.dumps(data, indent=4)
Esempio n. 2
0
def userin():
    '''Returns a :class:`dragonfire.utilities.TextToAction` instance.'''

    args = {}
    args["cli"] = True
    args["silent"] = True
    args["headless"] = True
    args["verbose"] = False
    args["gspeech"] = False
    args["server"] = False
    args["port"] = 3301
    args["version"] = False
    return TextToAction(args, testing=True)
def virtual_assistant():
    '''Returns a :class:`dragonfire.VirtualAssistant` instance.'''

    args = {}
    args["cli"] = True
    args["silent"] = True
    args["headless"] = True
    args["verbose"] = False
    args["gspeech"] = False
    args["server"] = False
    args["port"] = 3301
    args["version"] = False
    userin = TextToAction(args, testing=True)

    return VirtualAssistant(args, userin, testing=True)
Esempio n. 4
0
    def wikisearch(self, search_query):
        """Method to start Wikipedia search.

        Args:
            search_query (str):  Topic extracted from user's input.

        Returns:
            str:  Response.
        """

        wikiresult = wikipedia.search(search_query)
        if len(wikiresult) == 0:
            raise WikipediaNoResultsFoundError()
        wikipage = wikipedia.page(wikiresult[0])
        wikicontent = TextToAction.fix_the_encoding_in_text_for_tts(
            wikipage.content)
        wikicontent = re.sub(r'\([^)]*\)', '', wikicontent)
        self.userin.execute(["sensible-browser", wikipage.url], search_query)
        return self.userin.say(wikicontent,
                               cmd=["sensible-browser", wikipage.url])
Esempio n. 5
0
def initiate():
    """The top-level method to serve as the entry point of Dragonfire.

    This method is the entry point defined in `setup.py` for the `dragonfire` executable that
    placed a directory in `$PATH`.

    This method parses the command-line arguments and handles the top-level initiations accordingly.
    """

    ap = argparse.ArgumentParser()
    ap.add_argument(
        "-c",
        "--cli",
        help=
        "Command-line interface mode. Give commands to Dragonfire via command-line inputs (keyboard) instead of audio inputs (microphone).",
        action="store_true")
    ap.add_argument(
        "-s",
        "--silent",
        help=
        "Silent mode. Disable Text-to-Speech output. Dragonfire won't generate any audio output.",
        action="store_true")
    ap.add_argument(
        "-j",
        "--headless",
        help=
        "Headless mode. Do not display an avatar animation on the screen. Disable the female head model.",
        action="store_true")
    ap.add_argument("-v",
                    "--verbose",
                    help="Increase verbosity of log output.",
                    action="store_true")
    ap.add_argument(
        "-g",
        "--gspeech",
        help=
        "Instead of using the default speech recognition method(Mozilla DeepSpeech), use Google Speech Recognition service. (more accurate results)",
        action="store_true")
    ap.add_argument(
        "--server",
        help=
        "Server mode. Disable any audio functionality, serve a RESTful spaCy API and become a Twitter integrated chatbot.",
        metavar="REG_KEY")
    ap.add_argument("-p",
                    "--port",
                    help="Port number for server mode.",
                    default="3301",
                    metavar="PORT")
    ap.add_argument("--version",
                    help="Display the version number of Dragonfire.",
                    action="store_true")
    ap.add_argument(
        "--db",
        help=
        "Specificy the database engine for the knowledge base of learning feature. Values: 'mysql' for MySQL, 'sqlite' for SQLite. Default database engine is SQLite.",
        action="store",
        type=str)
    args = vars(ap.parse_args())
    if args["version"]:
        import pkg_resources
        print(pkg_resources.get_distribution("dragonfire").version)
        sys.exit(1)
    try:
        global dc
        userin = TextToAction(args)
        if not args["server"]:
            SystemTrayExitListenerSet(e)
            stray_proc = Process(target=SystemTrayInit)
            stray_proc.start()
            greet(userin)
        start(args, userin)
    except KeyboardInterrupt:
        if not args["server"]:
            stray_proc.terminate()
        sys.exit(1)
Esempio n. 6
0
    def command(self, com):
        """Function that serves as the entry point for each one of the user commands.

        This function goes through these steps for each one of user's commands, respectively:

        - Search across the built-in commands via a simple if-else control flow.
        - Try to get a response from :func:`dragonfire.arithmetic.arithmetic_parse` function.
        - Try to get a response from :func:`dragonfire.learn.Learner.respond` method.
        - Try to get a answer from :func:`dragonfire.odqa.ODQA.respond` method.
        - Try to get a response from :func:`dragonfire.deepconv.DeepConversation.respond` method.

        Args:
            com (str):  User's command.

        Returns:
            str:  Response.
        """

        if not self.args["server"]:
            global config_file
            global e
            if (e.is_set()):  # System Tray Icon exit must trigger this
                exit(0)
        args = self.args
        userin = self.userin
        user_full_name = self.user_full_name
        user_prefix = self.user_prefix
        if self.testing:
            config_file = self.config_file

        if isinstance(com, str) and com:
            com = com.strip()
        else:
            return False

        print("You: " + com.upper())
        doc = nlp(com)
        h = Helper(doc)
        self.h = h

        if args["verbose"]:
            userin.pretty_print_nlp_parsing_results(doc)

        # Input: DRAGONFIRE | WAKE UP | HEY
        if self.inactive and not self.check_wake_up_intent:
            return ""

        # User is answering to Dragonfire
        if user_answering['status']:
            if com.startswith("FIRST") or com.startswith(
                    "THE FIRST") or com.startswith("SECOND") or com.startswith(
                        "THE SECOND") or com.startswith(
                            "THIRD") or com.startswith("THE THIRD"):
                user_answering['status'] = False
                selection = None
                if com.startswith("FIRST") or com.startswith("THE FIRST"):
                    selection = 0
                elif com.startswith("SECOND") or com.startswith("THE SECOND"):
                    selection = 1
                elif com.startswith("THIRD") or com.startswith("THE THIRD"):
                    selection = 2

                if user_answering['for'] == 'wikipedia':
                    with nostderr():
                        search_query = user_answering['options'][selection]
                        try:
                            return self.wikisearch(search_query)
                        except requests.exceptions.ConnectionError:
                            return self.wikipedia_connection_error()
                        except WikipediaNoResultsFoundError:
                            return self.wikipedia_no_results_found_error(
                                search_query)
                        except Exception:
                            return False

        # Input: DRAGONFIRE | WAKE UP | HEY
        if self.check_wake_up_intent():
            self.inactive = False
            return userin.say(
                choice([
                    "Yes, " + user_prefix + ".", "Yes. I'm waiting.",
                    "What is your order?", "Ready for the orders!",
                    user_prefix.capitalize() + ", tell me your wish."
                ]))
        # Input: GO TO SLEEP
        if (h.check_verb_lemma("go") and h.check_noun_lemma("sleep")) or (
                h.check_verb_lemma("stop") and h.check_verb_lemma("listen")):
            self.inactive = True
            userin.execute([
                "echo"
            ], "Dragonfire deactivated. To reactivate say 'Dragonfire!' or 'Wake Up!'"
                           )
            return userin.say("I'm going to sleep")
        # Input: ENOUGH | SHUT UP
        if h.directly_equal(["enough"]) or (h.check_verb_lemma("shut")
                                            and h.check_nth_lemma(-1, "up")):
            tts_kill()
            msg = "Dragonfire quiets."
            print(msg)
            return msg
        # Input: WHAT IS YOUR NAME
        if h.check_wh_lemma("what") and h.check_deps_contains("your name"):
            return userin.execute([" "], "My name is Dragonfire.", True)
        # Input: WHAT IS YOUR GENDER
        if h.check_wh_lemma("what") and h.check_deps_contains("your gender"):
            return userin.say(
                "I have a female voice but I don't have a gender identity. I'm a computer program, "
                + user_prefix + ".")
        # Input: WHO AM I | SAY MY NAME
        if (h.check_wh_lemma("who")
                and h.check_text("I")) or (h.check_verb_lemma("say")
                                           and h.check_text("my")
                                           and h.check_lemma("name")):
            userin.execute([" "], user_full_name)
            return userin.say("Your name is " + user_full_name + ", " +
                              user_prefix + ".")

        # Input: OPEN [CAMERA, CALENDAR, CALCULATOR, STEAM, BLENDER, WRITER, MATH, IMPRESS, DRAW, TERMINAL]
        if h.check_verb_lemma("open") or h.check_adj_lemma(
                "open") or h.check_verb_lemma("run") or h.check_verb_lemma(
                    "start") or h.check_verb_lemma("show"):
            if h.check_text("blender"):
                userin.execute(["blender"], "Blender")
                return userin.say("Blender 3D computer graphics software")
            if h.check_text("draw"):
                userin.execute(["libreoffice", "--draw"], "LibreOffice Draw")
                return userin.say("Opening LibreOffice Draw")
            if h.check_text("impress"):
                userin.execute(["libreoffice", "--impress"],
                               "LibreOffice Impress")
                return userin.say("Opening LibreOffice Impress")
            if h.check_text("math"):
                userin.execute(["libreoffice", "--math"], "LibreOffice Math")
                return userin.say("Opening LibreOffice Math")
            if h.check_text("writer"):
                userin.execute(["libreoffice", "--writer"],
                               "LibreOffice Writer")
                return userin.say("Opening LibreOffice Writer")
            if h.check_noun_lemma("browser") or h.check_text(
                    "chrome") or h.check_text("firefox"):
                userin.execute(["sensible-browser"], "Web Browser")
                return userin.say("Web browser")
            if h.check_text("steam"):
                userin.execute(["steam"], "Steam")
                return userin.say("Opening Steam Game Store")
            if h.check_text("files"):
                return self.start_file_manager()
            if h.check_noun_lemma("camera"):
                userin.execute(["kamoso"], "Camera")  # KDE neon
                userin.execute(["snap-photobooth"], "Camera")  # elementary OS
                userin.execute(["cheese"], "Camera")  # Ubuntu
                return userin.say("Camera")
            if h.check_noun_lemma("calendar"):
                userin.execute(["korganizer"], "Calendar")  # KDE neon
                userin.execute(["maya-calendar"], "Calendar")  # elementary OS
                userin.execute(["orage"], "Calendar")  # Ubuntu
                return userin.say("Calendar")
            if h.check_noun_lemma("calculator"):
                userin.execute(["kcalc"], "Calculator")  # KDE neon
                userin.execute(["pantheon-calculator"],
                               "Calculator")  # elementary OS
                userin.execute(["gnome-calculator"], "Calculator")  # Ubuntu
                return userin.say("Calculator")
            if h.check_noun_lemma("console") or h.check_noun_lemma("terminal"):
                userin.execute(["konsole"], "Terminal")  # KDE neon
                userin.execute(["gnome-terminal"],
                               "Terminal")  # elementary OS & Ubuntu
                userin.execute(["guake"], "Terminal")  # Guake
                return userin.say("Terminal")
        # Input FILE MANAGER | FILE EXPLORER
        if h.check_noun_lemma("file") and (h.check_noun_lemma("manager")
                                           or h.check_noun_lemma("explorer")):
            return self.start_file_manager()
        # Input: SOFTWARE CENTER
        if h.check_noun_lemma("software") and h.check_text("center"):
            userin.execute(["plasma-discover"], "Software Center")  # KDE neon
            userin.execute(["software-center"],
                           "Software Center")  # elementary OS & Ubuntu
            return userin.say("Software Center")
        # Input: OFFICE SUITE
        if h.check_noun_lemma("office") and h.check_noun_lemma("suite"):
            userin.execute(["libreoffice"], "LibreOffice")
            return userin.say("Opening LibreOffice")
        # Input: GIMP | PHOTOSHOP | PHOTO EDITOR
        if h.check_text("gimp") or (h.check_noun_lemma("photo") and
                                    (h.check_noun_lemma("editor")
                                     or h.check_noun_lemma("shop"))):
            userin.execute(["gimp"], "GIMP")
            return userin.say("Opening the photo editor software.")
        # Input: INKSCAPE | VECTOR GRAPHICS
        if h.check_text("inkscape") or (h.check_noun_lemma("vector")
                                        and h.check_noun_lemma("graphic")) or (
                                            h.check_text("vectorial")
                                            and h.check_text("drawing")):
            userin.execute(["inkscape"], "Inkscape")
            return userin.say("Opening the vectorial drawing software.")
        # Input: Kdenlive | VIDEO EDITOR
        if h.check_text("kdenlive") or (h.check_noun_lemma("video")
                                        and h.check_noun_lemma("editor")):
            userin.execute(["kdenlive"], "Kdenlive")
            return userin.say("Opening the video editor software.")

        # Input: MY TITLE IS LADY | I'M A LADY | I'M A WOMAN | I'M A GIRL
        if h.check_lemma("be") and h.check_lemma(
                "-PRON-") and h.check_gender_lemmas('female'):
            return self.gender_update('female')
        # Input: MY TITLE IS SIR | I'M A MAN | I'M A BOY
        if h.check_lemma("be") and h.check_lemma(
                "-PRON-") and h.check_gender_lemmas('male'):
            return self.gender_update('male')
        # Input: CALL ME *
        if h.check_lemma("call") and h.check_lemma("-PRON-"):
            title = ""
            for token in doc:
                if token.pos_ == "NOUN":
                    title += ' ' + token.text
            title = title.strip()
            if not args["server"]:
                callme_config = config_file.search(
                    Query().datatype == 'callme')
                if callme_config:
                    config_file.update({'title': title},
                                       Query().datatype == 'callme')
                else:
                    config_file.insert({'datatype': 'callme', 'title': title})
            self.user_prefix = title
            user_prefix = self.user_prefix
            return userin.say("OK, " + user_prefix + ".")

        # Input: WHAT'S THE TEMPERATURE IN *
        if h.is_wh_question() and h.check_lemma("temperature"):
            city = ""
            for ent in doc.ents:
                if ent.label_ == "GPE":
                    city += ' ' + ent.text
            city = city.strip()
            if city:
                owm = pyowm.OWM("16d66c84e82424f0f8e62c3e3b27b574")
                reg = owm.city_id_registry()
                try:
                    weather = owm.weather_at_id(
                        reg.ids_for(city)[0][0]).get_weather()
                    fmt = "The temperature in {} is {} degrees celsius"
                    msg = fmt.format(
                        city,
                        weather.get_temperature('celsius')['temp'])
                    userin.execute([" "], msg)
                    return userin.say(msg)
                except IndexError:
                    msg = "Sorry, " + user_prefix + " but I couldn't find a city named " + city + " on the internet."
                    userin.execute([" "], msg)
                    return userin.say(msg)
        # Input: WHAT TIME IS IT
        if h.check_wh_lemma("what") and h.check_noun_lemma(
                "time") and h.check_verb_lemma("be") and h.check_text("it"):
            return userin.say("It's " +
                              datetime.datetime.now().strftime("%I:%M %p") +
                              choice([", " + user_prefix + ".", "."]))

        # Input: KEYBOARD *
        if (h.check_nth_lemma(0, "keyboard")
                or h.check_nth_lemma(0, "type")) and not args["server"]:
            n = len(doc[0].text) + 1
            with nostdout():
                with nostderr():
                    k = PyKeyboard()
                    if not self.testing:
                        for character in com[n:]:
                            k.tap_key(character)
                        k.tap_key(" ")
            return "keyboard"
        # Input: ENTER | NEW TAB | SWITCH TAB | CLOSE | GO BACK | GO FORWARD
        if (h.directly_equal(["enter"]) or
            (h.check_adj_lemma("new")
             and h.check_noun_lemma("line"))) and not args["server"]:
            with nostdout():
                with nostderr():
                    k = PyKeyboard()
                    if not self.testing:
                        k.tap_key(k.enter_key)
            return "enter"
        if h.check_adj_lemma("new") and h.check_noun_lemma(
                "tab") and not args["server"]:
            with nostdout():
                with nostderr():
                    k = PyKeyboard()
                    if not self.testing:
                        k.press_keys([k.control_l_key, 't'])
            return "new tab"
        if h.check_verb_lemma("switch") and h.check_noun_lemma(
                "tab") and not args["server"]:
            with nostdout():
                with nostderr():
                    k = PyKeyboard()
                    if not self.testing:
                        k.press_keys([k.control_l_key, k.tab_key])
            return "switch tab"
        if h.directly_equal(["CLOSE", "ESCAPE"]) and not args["server"]:
            with nostdout():
                with nostderr():
                    k = PyKeyboard()
                    if not self.testing:
                        k.press_keys([k.control_l_key, 'w'])
                        k.tap_key(k.escape_key)
            return "close"
        if self.check_browser_history_nav_intent("back"):
            return self.press_browser_history_nav("back")
        if self.check_browser_history_nav_intent("forward"):
            return self.press_browser_history_nav("forward")
        # Input: SCROLL LEFT | SCROLL RIGHT | SCROLL UP | SCROLL DOWN
        if (h.check_text("swipe")
                or h.check_text("scroll")) and not args["server"]:
            if h.check_text("left"):
                with nostdout():
                    with nostderr():
                        m = PyMouse()
                        if not self.testing:
                            m.scroll(0, -5)
                return "swipe left"
            if h.check_text("right"):
                with nostdout():
                    with nostderr():
                        m = PyMouse()
                        if not self.testing:
                            m.scroll(0, 5)
                return "swipe right"
            if h.check_text("up"):
                with nostdout():
                    with nostderr():
                        m = PyMouse()
                        if not self.testing:
                            m.scroll(5, 0)
                return "swipe up"
            if h.check_text("down"):
                with nostdout():
                    with nostderr():
                        m = PyMouse()
                        if not self.testing:
                            m.scroll(-5, 0)
                return "swipe down"
        # Input: PLAY | PAUSE | SPACEBAR
        if h.directly_equal(["PLAY", "PAUSE", "SPACEBAR"
                             ]) and not args["server"]:
            with nostdout():
                with nostderr():
                    k = PyKeyboard()
                    if not self.testing:
                        k.tap_key(" ")
            return "play"

        # Input: SHUT DOWN THE COMPUTER
        if ((h.check_text("shut") and h.check_text("down")) or
            (h.check_text("power") and h.check_text("off"))
            ) and h.check_text("computer") and not args["server"]:
            return userin.execute(["sudo", "poweroff"], "Shutting down", True,
                                  3)
        # Input: GOODBYE | BYE BYE | SEE YOU LATER
        if h.check_nth_lemma(0, "goodbye") or h.check_nth_lemma(
                0, "bye") or (h.check_verb_lemma("see") and h.check_text("you")
                              and h.check_adv_lemma("later")):
            response = userin.say("Goodbye, " + user_prefix)
            if not args["server"] and not self.testing:
                # raise KeyboardInterrupt
                thread.interrupt_main()
            return response

        # Input: (SEARCH|FIND) * (IN|ON|AT|USING) WIKIPEDIA
        if (h.check_lemma("search")
                or h.check_lemma("find")) and h.check_lemma("Wikipedia"):
            with nostderr():
                search_query = self.strip_the_search_query_by_intent(
                    doc, "Wikipedia")
                if search_query:
                    try:
                        return self.wikisearch(search_query)
                    except requests.exceptions.ConnectionError:
                        return self.wikipedia_connection_error()
                    except wikipedia.exceptions.DisambiguationError as disambiguation:
                        user_answering['status'] = True
                        user_answering['for'] = 'wikipedia'
                        user_answering['reason'] = 'disambiguation'
                        user_answering['options'] = disambiguation.options[:3]
                        notify = "Wikipedia disambiguation. Which one of these you meant?:\n - " + disambiguation.options[
                            0]
                        msg = user_prefix + ", there is a disambiguation. Which one of these you meant? " + disambiguation.options[
                            0]
                        for option in disambiguation.options[1:3]:
                            msg += ", or " + option
                            notify += "\n - " + option
                        notify += '\nSay, for example: "THE FIRST ONE" to choose.'
                        userin.execute([" "], notify)
                        return userin.say(msg)
                    except WikipediaNoResultsFoundError:
                        return self.wikipedia_no_results_found_error(
                            search_query)
                    except Exception:
                        pass
        # Input: (SEARCH|FIND) * (IN|ON|AT|USING) YOUTUBE
        if (h.check_lemma("search")
                or h.check_lemma("find")) and h.check_lemma("YouTube"):
            with nostdout():
                with nostderr():
                    search_query = self.strip_the_search_query_by_intent(
                        doc, "YouTube")
                    if search_query:
                        info = youtube_dl.YoutubeDL({}).extract_info(
                            'ytsearch:' + search_query,
                            download=False,
                            ie_key='YoutubeSearch')
                        if len(info['entries']) > 0:
                            youtube_title = info['entries'][0]['title']
                            youtube_url = "https://www.youtube.com/watch?v=%s" % (
                                info['entries'][0]['id'])
                            userin.execute(["sensible-browser", youtube_url],
                                           youtube_title)
                            youtube_title = TextToAction.fix_the_encoding_in_text_for_tts(
                                youtube_title)
                            response = userin.say(
                                youtube_title,
                                ["sensible-browser", youtube_url])
                        else:
                            youtube_title = "No video found, " + user_prefix + "."
                            response = userin.say(youtube_title)
                        k = PyKeyboard()
                        if not args["server"] and not self.testing:
                            time.sleep(5)
                            k.tap_key(k.tab_key)
                            k.tap_key(k.tab_key)
                            k.tap_key(k.tab_key)
                            k.tap_key(k.tab_key)
                            k.tap_key('f')
                        return response
        # Input: (SEARCH|FIND) * (IN|ON|AT|USING) (GOOGLE|WEB)
        if (h.check_lemma("search") or h.check_lemma("find")) and (
                h.check_lemma("Google") or h.check_lemma("web")
                or h.check_lemma("internet")) and not h.check_lemma("image"):
            with nostdout():
                with nostderr():
                    search_query = ""
                    for token in doc:
                        if not (token.lemma_ == "search" or token.lemma_
                                == "find" or token.lemma_ == "Google"
                                or token.lemma_ == "web" or token.lemma_
                                == "internet" or token.is_stop):
                            search_query += ' ' + token.text
                    search_query = search_query.strip()
                    if search_query:
                        tab_url = "http://google.com/?#q=" + search_query
                        return userin.execute(["sensible-browser", tab_url],
                                              search_query, True)
        # Input: (SEARCH IMAGES OF|FIND IMAGES OF|SEARCH|FIND) * (IN|ON|AT|USING) (GOOGLE|WEB|GOOGLE IMAGES|WEB IMAGES)
        if (h.check_lemma("search") or h.check_lemma("find")) and (
                h.check_lemma("Google") or h.check_lemma("web")
                or h.check_lemma("internet")) and h.check_lemma("image"):
            with nostdout():
                with nostderr():
                    search_query = ""
                    for token in doc:
                        if not (token.lemma_ == "search" or token.lemma_
                                == "find" or token.lemma_ == "Google"
                                or token.lemma_ == "web"
                                or token.lemma_ == "internet"
                                or token.lemma_ == "image" or token.is_stop):
                            search_query += ' ' + token.text
                    search_query = search_query.strip()
                    if search_query:
                        tab_url = "http://google.com/?#q=" + search_query + "&tbm=isch"
                        return userin.execute(["sensible-browser", tab_url],
                                              search_query, True)

        original_com = com
        com = coref.resolve(com)
        if args["verbose"]:
            print("After Coref Resolution: " + com)
        arithmetic_response = arithmetic_parse(com)
        if arithmetic_response:
            return userin.say(arithmetic_response)
        else:
            learner_response = learner.respond(com)
            if learner_response:
                return userin.say(learner_response)
            else:
                odqa_response = odqa.respond(com, not args["silent"], userin,
                                             user_prefix, args["server"])
                if odqa_response:
                    return userin.say(odqa_response)
                else:
                    dc_response = dc.respond(original_com, user_prefix)
                    if dc_response:
                        return userin.say(dc_response)