How to use the nltk.corpus.wordnet.synsets function in nltk

To help you get started, we’ve selected a few nltk examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sujitpal / nltk-examples / src / book / ch03_ex.py View on Github external
for word in words:
      try:
        postings.append((word.lower(), (pos, wpos)))
        offset = wn.synsets(word)[0].offset
        postings.append((offset, (pos, wpos)))
        poffset = wn.synsets(word)[0].hypernyms()[0].offset
        postings.append((poffset, (pos, wpos)))
      except IndexError:
        continue
      wpos = wpos + 1
  index = nltk.Index(postings)
  query = "canine"
  qpostings = []
  qpostings.extend([(pos, wpos) for (pos, wpos) in index[query]])
  try:
    offset = wn.synsets(query)[0].offset
    qpostings.extend([(pos, wpos) for (pos, wpos) in index[offset]])
  except IndexError:
    pass
  for (pos, wpos) in qpostings:
    left = webtext.words(docids[pos])[wpos-4:wpos]
    right = webtext.words(docids[pos])[wpos:wpos+4]
    print left, right
github sujitpal / nltk-examples / src / semantic / short_sentence_similarity.py View on Github external
def get_best_synset_pair(word_1, word_2):
    """ 
    Choose the pair with highest path similarity among all pairs. 
    Mimics pattern-seeking behavior of humans.
    """
    max_sim = -1.0
    synsets_1 = wn.synsets(word_1)
    synsets_2 = wn.synsets(word_2)
    if len(synsets_1) == 0 or len(synsets_2) == 0:
        return None, None
    else:
        max_sim = -1.0
        best_pair = None, None
        for synset_1 in synsets_1:
            for synset_2 in synsets_2:
               sim = wn.path_similarity(synset_1, synset_2)
               if sim > max_sim:
                   max_sim = sim
                   best_pair = synset_1, synset_2
        return best_pair
github snorkel-team / snorkel-tutorials / getting_started / getting_started.py View on Github external
def get_synonyms(word):
    """Get the synonyms of word from Wordnet."""
    lemmas = set().union(*[s.lemmas() for s in wn.synsets(word)])
    return list(set(l.name().lower().replace("_", " ") for l in lemmas) - {word})
github alumag / RePhraser / Synonym / synonym.py View on Github external
def getSynonym(self):
        """
        :return: synonyms of the object
        """
        return self.choseSynonym(wn.synsets(self._word))
github SamLynnEvans / Transformer / translate.py View on Github external
def get_synonym(word, SRC):
    syns = wordnet.synsets(word)
    for s in syns:
        for l in s.lemmas():
            if SRC.vocab.stoi[l.name()] != 0:
                return SRC.vocab.stoi[l.name()]
            
    return 0
github AbeHandler / WordNet-Word2Vec / wordnetter.py View on Github external
def synononymous(worda, wordb):
    return jaccard(wn.synsets(worda), wn.synsets(wordb))
github ptarau / DeepRank / sim.py View on Github external
def sim2(u,pu,v,pv) :
  m=0
  for i in wn.synsets(u,pu) :
    for j in wn.synsets(v,pv) :
      s=i.wup_similarity(j)
      if s : m=max(m,s)
  return m
github pdasigi / neural-event-model / onto_lstm / index_data.py View on Github external
wrd_lower = word.lower()
        if not pos:
            syns = []
            if wrd_lower in self.prep_senses:
                # This is a preposition, and we know its senses.
                # We'll add each prep sense as a separate hypernym list to be consistent
                # with the rest of the interface.
                for sense in self.prep_senses[wrd_lower]:
                    syns.append([sense])
        else:
            syns = wn.synsets(wrd_lower, pos=pos)
        hypernyms = []
        if wrd_lower in self.thing_prons:
            syns += wn.synsets("thing", "n")
        elif wrd_lower in self.male_prons:
            syns += wn.synsets("man", "n")
        elif wrd_lower in self.female_prons:
            syns += wn.synsets("woman", "n")
        elif wrd_lower in self.people_prons:
            syns += wn.synsets("people", "n")
        elif wrd_lower in self.person_prons:
            syns += wn.synsets("person", "n")
        elif re.match('^[12][0-9]{3}$', word) is not None:
            # The argument looks like an year
            syns += wn.synsets("year", "n") + wn.synsets("number", "n")
        elif re.match('^[0-9]+[.,-]?[0-9]*', word) is not None:
            syns += wn.synsets("number", "n")
        if len(hypernyms) == 0:
            if len(syns) != 0:
                pruned_synsets = list(syns) if self.word_syn_cutoff == -1 else syns[:self.word_syn_cutoff]
                for syn in pruned_synsets:
                    # syn is either a WordNet synset or a list if the word is a preposition, and we are
github aaronlaursen / STUFFS / STUFFS.py View on Github external
def getSimTerms(term):
    t = wordnet.synsets(term)
    terms=set()
    for syn in t:
        for name in syn.lemma_names:
            terms.add(name)
        for hypo in syn.hyponyms():
            for name in hypo.lemma_names:
                terms.add(name)
        for hyper in syn.hypernyms():
            for name in hyper.lemma_names:
                terms.add(name)
    return terms
github desilinguist / paraquery / para_wn.py View on Github external
def get_common_pos(word_a, word_b):
    a_pos = set()
    b_pos = set()
    common_pos = set()
    for x in wn.synsets(word_a):
        a_pos.add(x.pos)
    for x in wn.synsets(word_b):
        b_pos.add(x.pos)
    common_pos = a_pos.intersection(b_pos)
    return common_pos