def __init__(self, **kwargs):
        # Initializing variables
        self.program_data = {"name": "", "path": ""}
        self.flags = []
        self.data_dir = "generated_data"
        self.data = self.read_program_file()

        self.stopwords = StopWordsManager()
        self.tagger = POSTagger()
        self.conversation = []
    def __init__(self, **kwargs):
        # Initializing variables
        self.program_data = { "name" : "", "path" : "" }
        self.flags = []
        self.data_dir = "generated_data"
        self.data = self.read_program_file()

        self.stopwords = StopWordsManager()
        self.tagger = POSTagger()
        self.conversation = []
Exemple #3
0
class ExtractImportantInformation():
    """
    Extracts the important information from the incoming request from the
    user. This includes:
    - Name of program to run (or do other stuff for)
    - Path to program
    """

    def __init__(self, **kwargs):
        # Initializing variables
        self.program_data = { "name" : "", "path" : "" }

        self.stopwords = StopWordsManager()
        self.tagger = POSTagger()

    def extract_program_information(self, user_input):
        """
        Assuming the user inputed statement is a request for the
        developer assistant, parse the request and return the
        important information.
        """
        # Resetting program_data
        self.program_data["path"] = ""
        self.program_data["name"] = ""

        # Extracting important information from user_input
        self.program_data["path"] = self.extract_path(user_input)
        self.program_data["name"] = self.extract_name(user_input)

        # Return the important information
        return self.program_data["path"], self.program_data["name"]

    def extract_name(self, user_input):
        """
        Return the program's name if it is included somewhere in the
        conversation.
        """
        name = ""

        # The following assumes that the user_input is simply: "run program_x"
        # @TODO: Change this to a more advanced parsing of the user_input. It
        #   requires additional functions within the chatterbot.utils module
        #   and some more thought on how to implement a better system
        # @TODO: Implement more ways a user can communicate the name for
        #   a program
        has_asked_run = False
        for token in self.tagger.tokenize(user_input):
            if has_asked_run:
                if "/" in token:
                    name = token.split("/")[len(token.split("/")) - 1]
                else:
                    name = token
                break

            if "run" in token:
                has_asked_run = True

        return name

    def extract_path(self, user_input):
        """
        Return the program's path if it is included somewhere in the
        conversation.
        """
        path = ""

        # Identifies the path if one is in user_input
        # @TODO: Rewrite to remove false positives (which can be created
        #   easily with the current implementation)
        # @TODO: Implement more ways a user can communicate the path for
        #   a program
        for word in self.tagger.tokenize(user_input):
            if "/" in word:
                if word.endswith("/"):
                    path = word
                else:
                    split = word.split("/")
                    path = "/".join(split[:len(split) - 1]) + "/"
                break

        return path
Exemple #4
0
    def __init__(self, **kwargs):
        # Initializing variables
        self.program_data = { "name" : "", "path" : "" }

        self.stopwords = StopWordsManager()
        self.tagger = POSTagger()
class FileRunner(Module):
    """
    Runs a program file. Current list of supported languages:
    - python
    """

    def __init__(self, **kwargs):
        # Initializing variables
        self.program_data = { "name" : "", "path" : "" }
        self.flags = []
        self.data_dir = "generated_data"
        self.data = self.read_program_file()

        self.stopwords = StopWordsManager()
        self.tagger = POSTagger()
        self.conversation = []

    def process(self, statement):
        """
        Assuming the user inputed statement is a
        request for the developer assistant, parse
        the request and determine the appropriate
        action to be used.
        """
        confidence = 0

        # Getting the conversation
        try:
            self.conversation = self.context.conversation
        except:
            pass

        # Getting the stage of interaction with the user (assuming a command has not been executed)
        if ' '.join(self.flags) is not "name path":
            self.data = self.read_program_file()
            confidence = self.determine_stage_of_interaction(statement)

        if ' '.join(self.flags) is "name" or "ignore_previously_used" in ' '.join(self.flags):
            return "What is the absolute path to " + self.program_data["name"] + "?", confidence
        elif "previously_used" in ' '.join(self.flags):
            return "Would you like to use the path " + self.program_data["suggested_path"] + "?", confidence
        elif "name path" in ' '.join(self.flags):
            # Run program
            self.run_program()

            # Generating data
            return_statement = "Running " + self.program_data["name"] + "..."
            self.update_data()

            # Resetting global variables
            self.program_data = { "name" : "", "path" : "" }
            self.flags = []

            # Return a response
            return return_statement, confidence

        return "", 0

    def should_process(self, user_input):
        """
        Returns a 0 or 1 depending on whether the module should respond to
        the user's input (0 means the module should not respond, 1 means it
        should).

        This method looks at multiple different factors to determine whether
        it should respond to what the user asks. These factors include:
        - Previous conversation : By looking at previous conversation,
            the method can identify important data that is required to run
            the module. If that data is not present, then it is clear that
            the module should not respond to the incoming statement
        - Flags: By looking at the flags previously created, the method can
            determine whether or not a request is currently in process
        """
        # If no conversation is in progress, then we will need to parse
        #   the user's input to determine whether the module needs to
        #   respond
        if self.flags is []:
            return 1

        # Currently there are no false positives that should stop this module
        #   from running
        return 1

    def run_program(self):
        """
        Run the program at the location of self.program_data["path"] with the
        name self.program_data["name"].
        """
        # Determine what language the program is
        program_type = self.determine_program_type(self.program_data["path"], self.program_data["name"])

        # If the file is python, run it the specific way
        # @TODO: Make it work without shell=True
        if program_type == "python":
            subprocess.Popen([("python " + self.program_data["path"] + self.program_data["name"])], shell=True)

    def read_program_file(self):
        """
        Read in the programs that have been run previously.
        """
        path = os.path.join(self.data_dir, "programs_run.json")
        if os.path.exists(path):
            with open(path, 'r') as data_file:
                try:
                    return json.load(data_file)
                except:
                    pass

        empty_data = {
            "programs_run": {
            }
        }

        return empty_data

    def write_program_file(self):
        """
        Write the programs that have been previously run.
        """
        path = os.path.join(self.data_dir, "programs_run.json")
        if not os.path.exists(self.data_dir):
            os.mkdir(self.data_dir)

        with open(path, 'w') as data_file:
            json.dump(self.data, data_file, sort_keys = True, indent = 4, ensure_ascii=False)

    def update_data(self):
        """
        Update the data for the programs run.
        """
        most_recent_data = { self.program_data["name"] : self.program_data["path"] }
        self.data["programs_run"].update(most_recent_data)
        self.write_program_file()

    def determine_stage_of_interaction(self, input_statement):
        """
        Determines at which point in the interaction with
        the user chatterbot is.
        """
        confidence = 0

        length = len(self.conversation)
        if length == 0:
            length = 1
        else:
            length += 1

        # Going through flags and setting appropriate temporary variables
        ignore_previously_used = False
        for flag in self.flags:
            if "ignore_previously_used" in flag:
                ignore_previously_used = True

        # Parsing through the conversation with chatterbot looking for information
        user_input = ""
        for conversation_index in range(0, length):
            if conversation_index == len(self.conversation):
                user_input = input_statement
            else:
                user_input = self.conversation[conversation_index][0]

            # Determining whether suggested path was asked
            if "previously_used" in ' '.join(self.flags):
                # @TODO: Replace the hardcoded "yes" with a call to a utility
                #   function that determines if any word similar to (in this
                #   case) "yes" is the text
                if input_statement.lower() == "yes":
                    self.flags = ["name", "path"]
                    self.program_data["path"] = self.program_data["suggested_path"]

                    return 1
                elif input_statement.lower() == "no":
                    self.flags = ["name", "ignore_previously_used"]

                    return 1

            # Getting name of program (if available)
            extracted_name = self.extract_name(user_input)
            if self.program_data["name"] is "":
                if extracted_name is not "":
                    self.program_data["name"] = extracted_name
                    self.flags = ["name"]
            elif self.program_data["name"] is not extracted_name and extracted_name is not "":
                self.program_data["name"] = extracted_name
                self.flags = ["name"]

            # Getting path of program (if available)
            extracted_path = self.extract_path(user_input)
            if self.program_data["path"] is "":
                if extracted_path is not "":
                    self.program_data["path"] = extracted_path
                    self.flags.append("path")
            elif self.program_data["path"] is not extracted_path and extracted_path is not "":
                self.program_data["path"] = extracted_path
                self.flags.append("path")

        if ' '.join(self.flags) != "":
            confidence = 1

        if ' '.join(self.flags) is not "name path":
            # Read through the programs
            for program in self.data["programs_run"]:
                if self.program_data["name"] == program and ignore_previously_used == False:
                    # Use a suggested path if the program has been used before
                    self.flags.append("previously_used")
                    self.program_data["suggested_path"] = self.data["programs_run"][program]

        return confidence

    def extract_name(self, user_input):
        """
        Return the program's name if it is included somewhere in the
        conversation.
        """
        name = ""

        # The following assumes that the user_input is simply: "run program_x"
        # @TODO: Change this to a more advanced parsing of the user_input. It
        #   requires additional functions within the chatterbot.utils module
        #   and some more thought on how to implement a better system
        # @TODO: Implement more ways a user can communicate the name for
        #   a program
        has_asked_run = False
        for token in self.tagger.tokenize(user_input):
            if has_asked_run:
                if "/" in token:
                    name = token.split("/")[len(token.split("/")) - 1]
                else:
                    name = token
                break

            if "run" in token:
                has_asked_run = True

        return name

    def extract_path(self, user_input):
        """
        Return the program's path if it is included somewhere in the
        conversation.
        """
        path = ""

        # Identifies the path if one is in user_input
        # @TODO: Rewrite to remove false positives (which can be created
        #   easily with the current implementation)
        # @TODO: Implement more ways a user can communicate the path for
        #   a program
        for word in self.tagger.tokenize(user_input):
            if "/" in word:
                if word.endswith("/"):
                    path = word
                else:
                    split = word.split("/")
                    path = "/".join(split[:len(split) - 1]) + "/"
                break

        return path

    def determine_program_type(self, path, name):
        """
        Determines the type of file that is being asked to run.
        This in turn allows DevAssist to run the file properly.
        """
        # The program is a python program if it ends in ".py"
        if name.endswith(".py"):
            return "python"

        return "unknown"
class FileRunner(Module):
    """
    Runs a program file. Current list of supported languages:
    - python
    """
    def __init__(self, **kwargs):
        # Initializing variables
        self.program_data = {"name": "", "path": ""}
        self.flags = []
        self.data_dir = "generated_data"
        self.data = self.read_program_file()

        self.stopwords = StopWordsManager()
        self.tagger = POSTagger()
        self.conversation = []

    def process(self, statement):
        """
        Assuming the user inputed statement is a
        request for the developer assistant, parse
        the request and determine the appropriate
        action to be used.
        """
        confidence = 0

        # Getting the conversation
        try:
            self.conversation = self.context.conversation
        except:
            pass

        # Getting the stage of interaction with the user (assuming a command has not been executed)
        if ' '.join(self.flags) is not "name path":
            self.data = self.read_program_file()
            confidence = self.determine_stage_of_interaction(statement)

        if ' '.join(
                self.flags) is "name" or "ignore_previously_used" in ' '.join(
                    self.flags):
            return "What is the absolute path to " + self.program_data[
                "name"] + "?", confidence
        elif "previously_used" in ' '.join(self.flags):
            return "Would you like to use the path " + self.program_data[
                "suggested_path"] + "?", confidence
        elif "name path" in ' '.join(self.flags):
            # Run program
            self.run_program()

            # Generating data
            return_statement = "Running " + self.program_data["name"] + "..."
            self.update_data()

            # Resetting global variables
            self.program_data = {"name": "", "path": ""}
            self.flags = []

            # Return a response
            return return_statement, confidence

        return "", 0

    def should_process(self, user_input):
        """
        Returns a 0 or 1 depending on whether the module should respond to
        the user's input (0 means the module should not respond, 1 means it
        should).

        This method looks at multiple different factors to determine whether
        it should respond to what the user asks. These factors include:
        - Previous conversation : By looking at previous conversation,
            the method can identify important data that is required to run
            the module. If that data is not present, then it is clear that
            the module should not respond to the incoming statement
        - Flags: By looking at the flags previously created, the method can
            determine whether or not a request is currently in process
        """
        # If no conversation is in progress, then we will need to parse
        #   the user's input to determine whether the module needs to
        #   respond
        if self.flags is []:
            return 1

        # Currently there are no false positives that should stop this module
        #   from running
        return 1

    def run_program(self):
        """
        Run the program at the location of self.program_data["path"] with the
        name self.program_data["name"].
        """
        # Determine what language the program is
        program_type = self.determine_program_type(self.program_data["path"],
                                                   self.program_data["name"])

        # If the file is python, run it the specific way
        # @TODO: Make it work without shell=True
        if program_type == "python":
            subprocess.Popen([("python " + self.program_data["path"] +
                               self.program_data["name"])],
                             shell=True)

    def read_program_file(self):
        """
        Read in the programs that have been run previously.
        """
        path = os.path.join(self.data_dir, "programs_run.json")
        if os.path.exists(path):
            with open(path, 'r') as data_file:
                try:
                    return json.load(data_file)
                except:
                    pass

        empty_data = {"programs_run": {}}

        return empty_data

    def write_program_file(self):
        """
        Write the programs that have been previously run.
        """
        path = os.path.join(self.data_dir, "programs_run.json")
        if not os.path.exists(self.data_dir):
            os.mkdir(self.data_dir)

        with open(path, 'w') as data_file:
            json.dump(self.data,
                      data_file,
                      sort_keys=True,
                      indent=4,
                      ensure_ascii=False)

    def update_data(self):
        """
        Update the data for the programs run.
        """
        most_recent_data = {
            self.program_data["name"]: self.program_data["path"]
        }
        self.data["programs_run"].update(most_recent_data)
        self.write_program_file()

    def determine_stage_of_interaction(self, input_statement):
        """
        Determines at which point in the interaction with
        the user chatterbot is.
        """
        confidence = 0

        length = len(self.conversation)
        if length == 0:
            length = 1
        else:
            length += 1

        # Going through flags and setting appropriate temporary variables
        ignore_previously_used = False
        for flag in self.flags:
            if "ignore_previously_used" in flag:
                ignore_previously_used = True

        # Parsing through the conversation with chatterbot looking for information
        user_input = ""
        for conversation_index in range(0, length):
            if conversation_index == len(self.conversation):
                user_input = input_statement
            else:
                user_input = self.conversation[conversation_index][0]

            # Determining whether suggested path was asked
            if "previously_used" in ' '.join(self.flags):
                # @TODO: Replace the hardcoded "yes" with a call to a utility
                #   function that determines if any word similar to (in this
                #   case) "yes" is the text
                if input_statement.lower() == "yes":
                    self.flags = ["name", "path"]
                    self.program_data["path"] = self.program_data[
                        "suggested_path"]

                    return 1
                elif input_statement.lower() == "no":
                    self.flags = ["name", "ignore_previously_used"]

                    return 1

            # Getting name of program (if available)
            extracted_name = self.extract_name(user_input)
            if self.program_data["name"] is "":
                if extracted_name is not "":
                    self.program_data["name"] = extracted_name
                    self.flags = ["name"]
            elif self.program_data[
                    "name"] is not extracted_name and extracted_name is not "":
                self.program_data["name"] = extracted_name
                self.flags = ["name"]

            # Getting path of program (if available)
            extracted_path = self.extract_path(user_input)
            if self.program_data["path"] is "":
                if extracted_path is not "":
                    self.program_data["path"] = extracted_path
                    self.flags.append("path")
            elif self.program_data[
                    "path"] is not extracted_path and extracted_path is not "":
                self.program_data["path"] = extracted_path
                self.flags.append("path")

        if ' '.join(self.flags) != "":
            confidence = 1

        if ' '.join(self.flags) is not "name path":
            # Read through the programs
            for program in self.data["programs_run"]:
                if self.program_data[
                        "name"] == program and ignore_previously_used == False:
                    # Use a suggested path if the program has been used before
                    self.flags.append("previously_used")
                    self.program_data["suggested_path"] = self.data[
                        "programs_run"][program]

        return confidence

    def extract_name(self, user_input):
        """
        Return the program's name if it is included somewhere in the
        conversation.
        """
        name = ""

        # The following assumes that the user_input is simply: "run program_x"
        # @TODO: Change this to a more advanced parsing of the user_input. It
        #   requires additional functions within the chatterbot.utils module
        #   and some more thought on how to implement a better system
        # @TODO: Implement more ways a user can communicate the name for
        #   a program
        has_asked_run = False
        for token in self.tagger.tokenize(user_input):
            if has_asked_run:
                if "/" in token:
                    name = token.split("/")[len(token.split("/")) - 1]
                else:
                    name = token
                break

            if "run" in token:
                has_asked_run = True

        return name

    def extract_path(self, user_input):
        """
        Return the program's path if it is included somewhere in the
        conversation.
        """
        path = ""

        # Identifies the path if one is in user_input
        # @TODO: Rewrite to remove false positives (which can be created
        #   easily with the current implementation)
        # @TODO: Implement more ways a user can communicate the path for
        #   a program
        for word in self.tagger.tokenize(user_input):
            if "/" in word:
                if word.endswith("/"):
                    path = word
                else:
                    split = word.split("/")
                    path = "/".join(split[:len(split) - 1]) + "/"
                break

        return path

    def determine_program_type(self, path, name):
        """
        Determines the type of file that is being asked to run.
        This in turn allows DevAssist to run the file properly.
        """
        # The program is a python program if it ends in ".py"
        if name.endswith(".py"):
            return "python"

        return "unknown"