kev-cao / rofi-dictionary

A Python script that uses the Oxford Dictionary API to look up definitions using Rofi.
MIT License
12 stars 1 forks source link

Not fetching all categories #3

Open madprops opened 2 years ago

madprops commented 2 years ago

When testing with the word "train" I would only get verbs.

madprops commented 2 years ago

I don't really want to use categories so I made a function to fetch all definitions:

# Extract definitions from result
def get_all_definitions(results):
    definitions = []

    if "lexicalEntries" in results[0]:
        for lexical_entry in results[0]["lexicalEntries"]:
            if "entries" in lexical_entry:
                for entry in lexical_entry["entries"]:
                    if "senses" in entry:
                        for sense in entry["senses"]:
                            if "definitions" in sense:
                                for definition in sense["definitions"]:
                                    definitions.append(definition)
                            elif "shortDefinitions" in sense:
                                for definition in sense["shortDefinitions"]:
                                    definitions.append(definition)

    return list(set(definitions))
kev-cao commented 2 years ago

Interesting -- I haven't looked at the code in a while and I just remember being really confused by the structure of the API response. How has this been working for you? Any cases where it hasn't been working?

madprops commented 2 years ago

Yeah the response is kindof confusing. I think definitions reside inside senses, every lexicalEntry has a list of senses, so I iterate through those. Sometimes I'd get some errors due to things not existing, hence all those ifs. Also some words like "clave" wouldn't show anything, at least in my version, since it only had a "shortDefinition", so I added an elif to get those if no definitions were found. What I've been doing is copying the json response, fixing it to make it pure json, and using a beautifier to check the structure.

kev-cao commented 2 years ago

Sounds good - looks like yours may be working better than what I have now then! If you would be so kind to make a PR, I'd to be glad to integrate your changes into the main branch.

kev-cao commented 2 years ago

Ah, I noticed you have a fork of this as well - I'll take a look over that then.