def get_word_id(word, word_id, part_of_speech='noun_pl'): words = requests.get(get_url('lexemes/{}'.format(word))).json() if part_of_speech is None: searched_sense = [w['sense_id'] for w in words if w['lemma'] == word][0] else: searched_sense = [ w['sense_id'] for w in words if w['part_of_speech'] == 'noun_pl' and w['lemma'] == word ][0] senses = requests.get(get_url('senses/{}'.format(searched_sense))).json() if word_id is None: if part_of_speech is None: ids = [w['id'] for w in senses['homographs']] else: ids = [ w['id'] for w in senses['homographs'] if w['part_of_speech'] == 'noun_pl' ] else: if part_of_speech is None: ids = [ w['id'] for w in senses['homographs'] if w['sense_index'] == word_id ] else: ids = [ w['id'] for w in senses['homographs'] if w['part_of_speech'] == 'noun_pl' and w['sense_index'] == word_id ] return ids
def get_hyponyms(sense_id): s = requests.get(get_url('senses/{}'.format(sense_id))).json() hyponyms_data = [x for x in s['outgoing'] if x['relation_id'] == 10] if len(hyponyms_data) == 0: return [] hyponyms = [(x['lemma'], x['sense_index']) for x in hyponyms_data[0]['senses']] return hyponyms
def get_hypernyms(word_id): sense = requests.get(get_url('senses/{}'.format(word_id))).json() try: hypernyms = [h['senses'] for h in sense['incoming'] if h['language'] == 'pl_PL' and h['relation_id'] == 10][0] except IndexError: hypernyms = [] return [h['id'] for h in hypernyms]
def get_meaning_and_synonyms(sense_id): senses = requests.get(get_url('senses/{}'.format(sense_id))).json() d = { 'definition': senses['definition'], 'synonyms': [(s['lemma'], s['sense_index']) for s in senses['synset']['senses'] if (s['lemma'], s['sense_index']) != (senses['lemma'], senses['sense_index'])] } return (senses['lemma'], senses['sense_index']), d
def get_word_relations(word_id): rels = set() labels = {} senses = requests.get(get_url('senses/{}'.format(word_id))).json() if senses['language'] != 'pl_PL': return None, None rels_out = senses['outgoing'] rels_in = senses['incoming'] for r in rels_in: relation = get_relation(r['relation_id']) if relation in ['hiponimia', 'hiperonimia']: for s in r['senses']: rels.add((s['id'], word_id)) labels[(s['id'], word_id)] = relation for r in rels_out: relation = get_relation(r['relation_id']) if relation in ['hiponimia', 'hiperonimia']: for s in r['senses']: rels.add((word_id, s['id'])) labels[(word_id, s['id'])] = relation return rels, labels
def get_id(word): words = requests.get(get_url('lexemes/{}'.format(word))).json() searched_noun_sense = [w['sense_id'] for w in words][0] sense = requests.get(get_url('senses/{}'.format(searched_noun_sense))).json() return sense['id']
def id_to_word(word_id): sense = requests.get(get_url('senses/{}'.format(word_id))).json() return regex.sub(r'\s+', r'\n', sense['lemma'])