Skip to content

Useful snippets

ageorgou edited this page May 17, 2018 · 1 revision

Getting the longest meaning

With thanks to StackOverflow eg here:

import requests
r = requests.get('http://localhost:5000/search_all')
def mng_len(entry):
    return sum(len(mng) for mng in entry['senses_mng'])
lengths = [mng_len(e) for e in r.json()]
from operator import itemgetter
max_ind, max_len = max(enumerate(lengths), key=itemgetter(1))

We can then see the results:

>>> max_ind
11603
>>> max_len
784
>>> r.json()[max_ind]["headword"]
'šakānu[put]V'
>>> ", ".join(r.json()[max_ind]["senses_mng"])
'accomplishing, afflict, allocate, apply, appoint, appointing, assign, assigning, be appointed, be established, be placed, be put, be(come) brought about, be(come) placed, be(come) provided with, be(come) put in place, be(come) put, become established, bring about, carry out, cause to be placed, cause, conclude, conclusion, constantly deposit, constantly establish, constantly put, creating, decree, deposit, determine, determining, display, encourage, establish, establishing, fix, form, grant, have established, have placed, have set up, hold (a celebration), hold, imposing, include in a share, inflict, issue, issue, keep being placed, keep putting, keep spreading, keeping, lay, locate, make to be present, occur, perform, performing, pitch, place constantly, place, placement, placing, pronounce, provide with, provide, put down, put on, put, putting down, putting, set in place, set up, set, settle, staging, take place, turn, undergo'

This is a bit rough (for example it doesn't return all entries in case of a tie). A more accurate definition, given how we display search results, would be:

return (sum(len(mng) for mng in entry['senses_mng'])
        + 2 * len(entry['senses_mng']))  # for ', ' between meangings

but this doesn't change the result with this particular dataset.

Can be adapted for other fields.

Clone this wiki locally