Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spanish dictionary support #47

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

ArtemSkrebkov
Copy link

Hello @z1lc
Thanks for a useful addon!

I added support for spanish dictionary. I think it might be useful for others.

Please take a look at the approach suggested. I think it can help to make AutoDefine a bit easier to extend.
If you are OK with it, I can add an instruction how to add a new dictionary to the addon.

Hope you will have time to review the PR.

Thanks!

Copy link
Owner

@z1lc z1lc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for submitting this PR!

I'm not opposed to this reorganization but am curious if it is necessary. There's a more-specific comment in these notes, but the macro question is, what would the code look like for addDefinition, addPronunciation etc if you used the xml API? Could you simply re-use most of the existing parsing code?

Comment on lines +28 to +29
myPath = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, myPath + '/libs')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason to have this up here? would prefer not to mix imports & code

Comment on lines +144 to +147
if settings.PREFERRED_DICTIONARY == "COLLEGIATE" or settings.PREFERRED_DICTIONARY == "MEDICAL":
cardBuilder = cardbuilder.CollegiateCardBuilder(word)
elif settings.PREFERRED_DICTIONARY == "SPANISH":
cardBuilder = cardbuilder.SpanishCardBuilder(word)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's add an else case here and raise or print an error message -- it looks like validate_settings may just allow continued execution

Comment on lines +6 to +8
myPath = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, myPath + '/../')
sys.path.insert(0, myPath + '/../libs')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment as above

Comment on lines +85 to +87
if not settings.PREFERRED_DICTIONARY in ["COLLEGIATE", "MEDICAL", "SPANISH"]:
message = "Setting PREFERRED_DICTIONARY must be set to COLLEGIATE, MEDICAL or SPANISH. Current setting: '%s'" \
% settings.PREFERRED_DICTIONARY
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we pull out ["COLLEGIATE", "MEDICAL", "SPANISH"] into a set as a global variable somewhere? Then we can re-use it in the message and potentially refer to it elsewhere in code as well

def getCard(self) -> Card:
return self._card

class CollegiateCardBuilder(CardBuilder):
Copy link
Owner

@z1lc z1lc Dec 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is functioning as a card builder for both medical as well as collegiate? Let's rename this as GenericCardBuilder or something and then subclass Collegiate and Medical from it (even if their bodies will, for now, be empty).


self._card.fields[settings.PRONUNCIATION_FIELD] = to_print

class SpanishCardBuilder(CardBuilder):
Copy link
Owner

@z1lc z1lc Dec 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we're using the JSON api here instead of the xml one -- I am assuming that's why there is much less code re-use between the two. Can you describe why to make this choice? I'm concerned this is duplicating a significant amount of parsing code, which can really easily start to drift. Did you try to simply re-use the existing code to parse the xml-based responses? If we want to wholesale switch to JSON, that is another possibility. But trying to consume both XML+JSON response types from the same API is not ideal.

Copy link
Author

@ArtemSkrebkov ArtemSkrebkov Dec 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the reply!

The thight is that XML API is deprecated and there is no way to get a key for it. At least, I didn't find one. If you know how to get a key for XML version of Spanish dictionary, please share, I will definitely give it a try.

I want to extend AutoDefine to work with Spanish language and the fastest way (rather the only :)) I came up with is to implement a JSON parser for merriam webster dictionary API. I guess that the parser I implemented in SpanishCardBuilder should work with other dictionaries but I need to do more testing to confirm it.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I don't mean to block you too much but with this large of a refactoring I would prefer if we switch all the code over to the same base parsing code, ideally JSON. Happy to review that if you add commits to this PR!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not a problem. I am glad to help if it makes the addon better.
Does it make sense to keep XML as an option? We can say that XML is deprecated and will be removed in future.
It might be useful if JSON parser introduces some bug.
In that case, a user can switch back to XML instead of re-installing an older version of the extention.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bias would generally be to remove that support altogether as I think the option will almost never be used by users & will mean a higher volume of code to maintain. If M-W decides to actually break the JSON API for a long period of time, we should just push out a full add-on update to go back to XML. We can always find that code in the git history. But if you find a simple way to keep it we can do so -- I'll let you make the call.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants