1goodday / Google-Dictionary.ankiaddon

Anki Addon to add Google Dictionary results (American and/or British English accents + meanings + translation) to vocabulary entries
MIT License
0 stars 0 forks source link

Meanings, grammar, translation, examples, etc . should be in separate fields . #1

Closed Shahxad-Akram closed 3 years ago

Shahxad-Akram commented 3 years ago

Thanks for this addon, it seems good but not the best, as it accumulates everything in the same field. There should be some options to assign different fields for grammar, translation, examples, etc.

1goodday commented 3 years ago

Hello Shahzad, Thanks for using this add-on and your feedback. I want it to be useful and since it's under development, changes can be applied to better suit the users' needs.

I will try to accommodate your suggestion, either in the current format or after changing the settings from the text-based Config file to a graphical interface in the next versions.

Meanwhile, if you are interested, please provide me with more details about the final format you have in mind. Do you want examples, synonyms and antonyms, ... be separated by lines or do you mean having control of adding or not adding these specific fields?

Thanks,

Roozbeh

Shahxad-Akram commented 3 years ago

Yeah, it would be better to provide more settings in the text-based config file to handle the field number for definition, grammar, translation, examples, etc. Actually last night I had to use it urgently and I made some rough changes according to my note's structure. I think the following code would help you to understand what I said.


def _add_1st_meaning(editor: editor.Editor) -> None:
    '''Adds only the first meaning given by api.dictionaryapi.dev to the
    editor "Back" box.'''
    # Same as _add_all_meanings, just breaking the loop after the first definition.

    word = _get_word_from_editor(editor)

    # Gets the word from api.dictionaryapi.dev (than returns a list of length 1).
    try:
        api_json = requests.get(
            "https://api.dictionaryapi.dev/api/v2/entries/en/" + word).json()[0]
    except:
        return

    # _answer will be constructed and used to fill the "Back" box.

    _definitions: str = ''
    _grammar: str = ''
    _examples: str = ''
    _synonyms: str = ''
    _antonyms: str = ''

    # Loop to add all the meanings, plus formatting.
    for meaning in api_json['meanings']:
        try:
            _grammar += meaning['partOfSpeech'] + '; '
        except:
            pass

        for definition in meaning['definitions']:
            try:
                _definitions += definition['definition'] + '; '
            except:
                pass

            try:
                if definition['example']:
                    _examples += definition['example'] + '; '
            except:
                pass

            try:
                if definition['synonyms']:
                    _synonyms += ", ".join(definition['synonyms']) + '; '
            except:
                pass

            try:
                if definition['antonyms']:
                    _antonyms += ", ".join(definition['antonyms']) + '; '

            except:
                pass

            break
        break

    editor.note.fields[1] = _definitions + '<br><br>' + \
        _synonyms + '<br><br>' + 'Antonyms: ' + _antonyms

    editor.note.fields[3] = _examples

    editor.note.fields[2] = _grammar

    # For debugging
    # aqt.utils.showText(str(editor.note.fields[1]))

    # Applies the new content.
    editor.loadNote()

    return
1goodday commented 3 years ago

Thanks Shahzad! Got your meaning, I'll work on it.

1goodday commented 3 years ago

Hello Shahzad, This capability is added in v4.0.0. Thanks if you start using the new version and tell me about the bugs/problems and your suggestions.

Regards,

Roozbeh

1goodday commented 3 years ago

Hello Shahzad, May I know what OS you are using? Someone reported the same issue with Mac.

Shahxad-Akram commented 3 years ago

Hey, sorry I deleted the comment, Actually, it sounds funny, it didn't work for 2 days and then started working on its own.

1goodday commented 3 years ago

Thanks for letting me know. How do you find the new version?