Beispiel #1
0
    def request_translation(self, trans_request):
        translator = trans_request.translator
        source_lang = trans_request.translation_project.project.source_language
        target_lang = trans_request.translation_project.language
        if not translator.is_language_pair_supported(source_lang, target_lang):
            raise UnsupportedLanguagePair(translator, source_lang, target_lang)

        if translator.type == SERVERLAND:
            request_id = utils.generate_request_id()
            shortname = "wt_%s" % request_id
            src = utils.get_iso639_2(source_lang.code)
            tgt = utils.get_iso639_2(target_lang.code)
            contents = {
                "token": str(self.token),
                "shortname": str(shortname),
                "worker": str(translator.shortname),
                "source_language": str(src),
                "target_language": str(tgt),
            }
            source_file_id = "%s-%s-%s" % (request_id, source_lang.code, target_lang.code)
            store = Store.objects.get(translation_project=trans_request.translation_project)
            sentences = [unicode(unit) for unit in store.units]

            boundary = "-----" + mimetools.choose_boundary() + "-----"
            body = utils.generate_request_body(boundary, contents, source_file_id, sentences)
            header = utils.generate_request_header(boundary, body)

            response = self.request(str(self.url) + "requests/", method="POST", body=body, header=header)

            print "Submitted new request..."
            print "=> Response status:", response[0].status
            print "=> Reason:", response[0].reason
            print "=> Shortname:", shortname

            trans_request.external_id = shortname
            trans_request.status = STATUS_IN_PROGRESS
            trans_request.save()
Beispiel #2
0
def request_translation(translator, sentences, source_language,
                        target_language):
    """
    Request a MachineTranslator to perform a translation. The request process is implemented
    based on the type of MachineTranslator. For example, a SERVERLAND type uses a MT Server Land
    to request a translation.
    Preconditions:
        translator:    A MachineTranslator object.
        sentences:     A list of strings.
    Exceptions:
        UnsupportedLanguagePair:The target translator doesn't support the language pair.
        UndefinedTranslator:    When looking up the ServerlandHost for a MachineTranslator, if none exists (this should not happen).
        ServerlandConfigError:  The ServerlandHost has an error.
    """

    # Make sure that the translator supports the language pair
    if not translator.is_language_pair_supported(source_language,
                                                 target_language):
        raise UnsupportedLanguagePair(translator, source_language,
                                      target_language)

    # Generate a request id
    request_id = utils.generate_request_id()

    # Make sure that sentences is a list.
    sentences = utils.cast_to_list(sentences)
    #    if not isinstance(sentences, list):
    #        print "Retrieved only one sentence."
    #        sentences = [sentences]

    # One sentence per line, to make it easier for the translator to do its job.
    text = "\n".join(sentence for sentence in sentences)

    # Determine the machine translator type.
    if translator.type == SERVERLAND:
        # Get the ServerlandHost
        try:
            # TODO: Change this host reference. Currently, only one ServerLand host is allowed.
            serverland_host = translator.serverlandhost_set.all()[0]
        except IndexError:
            raise UndefinedTranslator(translator)

        source_file_id = "%s-%s-%s" % (request_id, source_language.code,
                                       target_language.code)

        try:
            #            print "Requesting the translation"
            #            print serverland_host.token, request_id, translator.shortname
            #            print utils.get_iso639_2(source_language.code), utils.get_iso639_2(target_language.code)
            #            print source_file_id
            #            print text

            # Request the translation.
            proxy = xmlrpclib.ServerProxy(serverland_host.url)
            result = proxy.create_translation(
                serverland_host.token,  # Authentication token
                request_id,  # Custom request ID
                translator.shortname,  # MT ServerLand worker
                utils.get_iso639_2(source_language.code
                                   ),  # Source language (in bibliographic)
                utils.get_iso639_2(target_language.code
                                   ),  # Target language (in bibliographic)
                source_file_id,  # Composite id
                text)  # Sentence(s) to translate

            # TODO: For now, just print the results.
            print result

            # TODO: Return the request_id
            return request_id
        except xmlrpclib.Fault as ex:
            raise ServerlandConfigError(serverland_host, ex)
def request_translation(translator, sentences, source_language, target_language):
    """
    Request a MachineTranslator to perform a translation. The request process is implemented
    based on the type of MachineTranslator. For example, a SERVERLAND type uses a MT Server Land
    to request a translation.
    Preconditions:
        translator:    A MachineTranslator object.
        sentences:     A list of strings.
    Exceptions:
        UnsupportedLanguagePair:The target translator doesn't support the language pair.
        UndefinedTranslator:    When looking up the ServerlandHost for a MachineTranslator, if none exists (this should not happen).
        ServerlandConfigError:  The ServerlandHost has an error.
    """
    
    # Make sure that the translator supports the language pair
    if not translator.is_language_pair_supported(source_language, target_language):
        raise UnsupportedLanguagePair(translator, source_language, target_language)
    
    # Generate a request id
    request_id = utils.generate_request_id()
    
    # Make sure that sentences is a list.
    sentences = utils.cast_to_list(sentences)
#    if not isinstance(sentences, list):
#        print "Retrieved only one sentence."
#        sentences = [sentences]
    
    # One sentence per line, to make it easier for the translator to do its job.
    text = "\n".join(sentence for sentence in sentences)
        
    # Determine the machine translator type.
    if translator.type == SERVERLAND:
        # Get the ServerlandHost
        try:
            # TODO: Change this host reference. Currently, only one ServerLand host is allowed.
            serverland_host = translator.serverlandhost_set.all()[0]
        except IndexError:
            raise UndefinedTranslator(translator)
        
        source_file_id = "%s-%s-%s" % (request_id, source_language.code, target_language.code)
        
        try:
#            print "Requesting the translation"
#            print serverland_host.token, request_id, translator.shortname
#            print utils.get_iso639_2(source_language.code), utils.get_iso639_2(target_language.code)
#            print source_file_id
#            print text
            
            # Request the translation.
            proxy = xmlrpclib.ServerProxy(serverland_host.url)
            result = proxy.create_translation(serverland_host.token,            # Authentication token
                                     request_id,                                # Custom request ID
                                     translator.shortname,                      # MT ServerLand worker
                                     utils.get_iso639_2(source_language.code),  # Source language (in bibliographic)
                                     utils.get_iso639_2(target_language.code),  # Target language (in bibliographic)
                                     source_file_id,                            # Composite id
                                     text)                                      # Sentence(s) to translate
            
            # TODO: For now, just print the results.
            print result
            
            # TODO: Return the request_id
            return request_id
        except xmlrpclib.Fault as ex:
            raise ServerlandConfigError(serverland_host, ex)