Exemple #1
0
    def translate(self,
                  entry: Entry,
                  source_language: str,
                  target_language: str = 'mg') -> Entry:
        out_definitions = []
        for definition in entry.definitions:
            out_entry_dict = entry.serialise()
            for method in self.methods:
                extracted_definition = method(entry.part_of_speech, definition,
                                              source_language, target_language)
                if isinstance(extracted_definition, TranslatedDefinition):
                    out_definitions.append(extracted_definition)

            out_entry_dict['definitions'] = out_definitions
            return Entry(**out_entry_dict)
Exemple #2
0
    def render(self, info: Entry, link=True) -> str:
        data = info.serialise()
        s = """
{{-%(language)s-}}
'''{{subst:BASEPAGENAME}}'''""" % data
        if link:
            s += "\n# %s" % ', '.join(
                ['[[%s]]' % (d) for d in info.definitions])
        else:
            s += "\n# %s" % ', '.join(['%s' % (d) for d in info.definitions])
        additional_note = '\n{{bot-made translation|%s}}' % info.origin_wiktionary_page_name
        s = s + additional_note
        try:
            return s
        except UnicodeDecodeError:
            return s.decode('utf8')
Exemple #3
0
 def dictionary_service_update_database(self, info: Entry):
     """updates database"""
     # Adapt to expected format
     log.info(info.serialise())
     definitions = [{
         'definition': d,
         'definition_language': self.content_language
     } for d in info.definitions]
     data = {
         'definitions':
         definitions,
         'word':
         info.entry,
         'part_of_speech':
         info.part_of_speech,
         'translation_method':
         info.translation_method
         if hasattr(info, 'translation_method') else None
     }
     response = dictionary_service.post('entry/%s/create' % info.language,
                                        json=data)
     if response.status_code == WordAlreadyExists.status_code:
         word_response = dictionary_service.get(
             'entry/%s/%s' %
             (info.language, info.entry)).json()  # fetch its ID
         edit_response = dictionary_service.put(
             'entry/%d/edit' % word_response[0]['id'],
             json=data)  # edit using its ID
         if edit_response.status_code == WordAlreadyExists.status_code:
             log.debug(
                 '%s [%s] > Attempted to create an already-existing entry.'
                 % (info.entry, info.language))
         elif edit_response.status_code != 200:
             log.error(
                 '%s [%s] > Entry update failed (%d).' %
                 (info.entry, info.language, edit_response.status_code))
Exemple #4
0
 def batchfile(self, info: Entry):
     "return batch format (see doc)"
     string = "%(entry)s -> %(entry_definition)s -> %(part_of_speech)s -> %(language)s\n" % info.serialise(
     )
     return string