Ejemplo n.º 1
0
def print_help():
    announce('ccg2lambda QA Assistant: Help')
    info(' › q, quit:     \tQuits the software')
    info(' › ?, h, help:  \tDisplays this page')
    info(' › v, verbose:  \tEnables verbose mode')
    # noinspection SpellCheckingInspection
    info(' › nv, noverbose:\tDisables verbose mode')
Ejemplo n.º 2
0
 def build(self):
     """
     Add triples to the basic query template (sparql/queryBuilder/query_template.sparql) and sends the request
     :return: the result of the request.
     """
     verbose("Filling variables dictionary...")
     self.__fill_dictionary()
     for triple in self.__triples:
         self.__query += ' ' + triple[0] + ' ' + triple[1] + \
                         ' ' + triple[2] + ' .'
     announce("Sending query: " + self.__query)
     info("The answer is: " + self.__request())
Ejemplo n.º 3
0
def main():
    announce('Starting ccg2lambda QA Assistant Server...')
    from network.server import server

    # Here we can register commands if we ever need to
    server.register_command('request', request)
    server.register_command('choice', client_choice)

    # The server starts listening to requests
    try:
        server.run()
    except KeyboardInterrupt:
        print('\nThe user killed the server...')
Ejemplo n.º 4
0
    def run(self):
        """
        Launches this server. This method will block, and clients will begin to be handled.
        """

        announce("Server: Running")
        while self.is_running:
            try:
                self.socket.settimeout(1)
                client, infos = self.socket.accept()
                Thread(target=self.__connect_to_client, args=[client]).start()
            except (socket.timeout, OSError):
                pass
        warning("Server: The socket was closed, stopped listening.")
Ejemplo n.º 5
0
def choose(client, *args: str):
    verbose('The server is asking:', *args)
    announce('\nWhat did you mean?')
    options = [arg.replace('~', ' ').split('|') for arg in args]

    for option in options:
        print(' › ' + option[0] + '\t\x1b[32m' + option[1] + '\x1b[0m\t' +
              option[2] + ' \x1b[1;35mhttps:' + option[3] + '\x1b[0m')

    while True:
        user: str = input("Type the number of your choice: ")

        user_i: int = int(user)
        if 0 <= user_i < len(options):
            break

    client.send('choice', str(user))
Ejemplo n.º 6
0
def main():
    announce("\n Starting...")

    print_help()
    while True:
        user = input("\nSend a request to the server: ")
        # noinspection SpellCheckingInspection
        if user == "quit" or user == "q":
            break
        elif user == "help" or user == "h" or user == "?":
            print_help()
        elif user == "verbose" or user == "v":
            set_verbose(True)
        elif user == "noverbose" or user == "nv":
            set_verbose(False)
        else:
            try:
                request_sentence(user)
            except ConnectionRefusedError:
                warning('Server unreachable. Please check that it is running.')

    announce('Stopping')
Ejemplo n.º 7
0
def convert(sentences: List[str], output_file=False) -> List[Expression]:
    """
    Converts a list of questions to an Abstract Syntax Tree (AST), using:
     - spaCy to annotate each word with its grammatical class,
     - depccg to convert the natural language to a CCG tree,
     - ccg2lambda to convert the CCG tree to a λ expressions,
     - nltk to parse that λ-expressions into an AST represented in Python objects.

    :param output_file: if True, will output an xml file of the parsed sentences
    :param sentences: a list of questions in English
    :exception if less than 1 sentence is provided
    :return a list of ASTs that each correspond to one of the given sentences (in the same order).
    """
    if len(sentences) < 1:
        raise Exception("Cannot run the pipeline with less than 1 sentence: " +
                        str(len(sentences)))

    announce("Beginning conversion of", len(sentences),
             "sentences, the first one is [", sentences[0], "]")
    annotated_sentences, split_sentences = __annotate_spacy(sentences)
    ccg_of_each_sentence = __convert_to_ccg(split_sentences)
    lambda_expressions = __ccg_to_jigg_xml(ccg_of_each_sentence,
                                           annotated_sentences)

    formulas = __ccg_jigg_xml_to_lambda(lambda_expressions)

    # Creates an XML file to be used
    if output_file:
        # Can be used in ccg2lambda python script visualize.py to output
        # sentences.html to give better overview
        info("Creating visualisation in file sentences.sem.xml")
        visualisation.visualize(lambda_expressions, "sentences.html")

    expr = __lambda_to_python(formulas)
    verbose("Conversion done.")
    return expr