Python nltk.corpus.wordnet.synset() Examples

The following are 30 code examples of nltk.corpus.wordnet.synset(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module nltk.corpus.wordnet , or try the search function .
Example #1
Source File: wordnet_app.py    From luscan-devel with GNU General Public License v2.0 6 votes vote down vote up
def get_static_welcome_message():
    """
    Get the static welcome page.
    """
    return \
"""
<h3>Search Help</h3>
<ul><li>The display below the line is an example of the output the browser
shows you when you enter a search word. The search word was <b>green</b>.</li>
<li>The search result shows for different parts of speech the <b>synsets</b>
i.e. different meanings for the word.</li>
<li>All underlined texts are hypertext links. There are two types of links:
word links and others. Clicking a word link carries out a search for the word
in the Wordnet database.</li>
<li>Clicking a link of the other type opens a display section of data attached
to that link. Clicking that link a second time closes the section again.</li>
<li>Clicking <u>S:</u> opens a section showing the relations for that synset.</li>
<li>Clicking on a relation name opens a section that displays the associated
synsets.</li>
<li>Type a search word in the <b>Next Word</b> field and start the search by the
<b>Enter/Return</b> key or click the <b>Search</b> button.</li>
</ul>
""" 
Example #2
Source File: convert_from_bansal.py    From Context-aware-ZSR with MIT License 6 votes vote down vote up
def get_synset_embedding(synset, word_vectors, get_vector):
    class_name = wn.synset(synset).lemma_names()
    class_name = ', '.join([_.replace('_', ' ') for _ in class_name])
    class_name = class_name.lower()

    feat = np.zeros(feat_len)

    options = class_name.split(',')
    cnt_word = 0
    for j in range(len(options)):
        now_feat = get_embedding(options[j].strip(), word_vectors, get_vector)
        if np.abs(now_feat.sum()) > 0:
            cnt_word += 1
            feat += now_feat
    if cnt_word > 0:
        feat = feat / cnt_word

    if np.abs(feat.sum()) == 0:
        return None
    else:
        # feat = feat / (np.linalg.norm(feat) + 1e-6)
        return feat 
Example #3
Source File: sentiwordnet.py    From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
def senti_synset(self, *vals):
        from nltk.corpus import wordnet as wn

        if tuple(vals) in self._db:
            pos_score, neg_score = self._db[tuple(vals)]
            pos, offset = vals
            if pos == 's':
                pos = 'a'
            synset = wn.synset_from_pos_and_offset(pos, offset)
            return SentiSynset(pos_score, neg_score, synset)
        else:
            synset = wn.synset(vals[0])
            pos = synset.pos()
            if pos == 's':
                pos = 'a'
            offset = synset.offset()
            if (pos, offset) in self._db:
                pos_score, neg_score = self._db[(pos, offset)]
                return SentiSynset(pos_score, neg_score, synset)
            else:
                return None 
Example #4
Source File: wordnet_app.py    From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
def get_static_welcome_message():
    """
    Get the static welcome page.
    """
    return """
<h3>Search Help</h3>
<ul><li>The display below the line is an example of the output the browser
shows you when you enter a search word. The search word was <b>green</b>.</li>
<li>The search result shows for different parts of speech the <b>synsets</b>
i.e. different meanings for the word.</li>
<li>All underlined texts are hypertext links. There are two types of links:
word links and others. Clicking a word link carries out a search for the word
in the Wordnet database.</li>
<li>Clicking a link of the other type opens a display section of data attached
to that link. Clicking that link a second time closes the section again.</li>
<li>Clicking <u>S:</u> opens a section showing the relations for that synset.</li>
<li>Clicking on a relation name opens a section that displays the associated
synsets.</li>
<li>Type a search word in the <b>Next Word</b> field and start the search by the
<b>Enter/Return</b> key or click the <b>Search</b> button.</li>
</ul>
""" 
Example #5
Source File: wordnet_app.py    From razzy-spinner with GNU General Public License v3.0 6 votes vote down vote up
def get_static_welcome_message():
    """
    Get the static welcome page.
    """
    return \
"""
<h3>Search Help</h3>
<ul><li>The display below the line is an example of the output the browser
shows you when you enter a search word. The search word was <b>green</b>.</li>
<li>The search result shows for different parts of speech the <b>synsets</b>
i.e. different meanings for the word.</li>
<li>All underlined texts are hypertext links. There are two types of links:
word links and others. Clicking a word link carries out a search for the word
in the Wordnet database.</li>
<li>Clicking a link of the other type opens a display section of data attached
to that link. Clicking that link a second time closes the section again.</li>
<li>Clicking <u>S:</u> opens a section showing the relations for that synset.</li>
<li>Clicking on a relation name opens a section that displays the associated
synsets.</li>
<li>Type a search word in the <b>Next Word</b> field and start the search by the
<b>Enter/Return</b> key or click the <b>Search</b> button.</li>
</ul>
""" 
Example #6
Source File: sentiwordnet.py    From razzy-spinner with GNU General Public License v3.0 6 votes vote down vote up
def senti_synset(self, *vals):        
        from nltk.corpus import wordnet as wn
        if tuple(vals) in self._db:
            pos_score, neg_score = self._db[tuple(vals)]
            pos, offset = vals
            synset = wn._synset_from_pos_and_offset(pos, offset)
            return SentiSynset(pos_score, neg_score, synset)
        else:
            synset = wn.synset(vals[0])
            pos = synset.pos()
            offset = synset.offset()
            if (pos, offset) in self._db:
                pos_score, neg_score = self._db[(pos, offset)]
                return SentiSynset(pos_score, neg_score, synset)
            else:
                return None 
Example #7
Source File: convert_sync.py    From Context-aware-ZSR with MIT License 6 votes vote down vote up
def get_synset_embedding(synset, word_vectors, get_vector):
    class_name = wn.synset(synset).lemma_names()
    class_name = ', '.join([_.replace('_', ' ') for _ in class_name])
    class_name = class_name.lower()

    feat = np.zeros(feat_len)

    options = class_name.split(',')
    cnt_word = 0
    for j in range(len(options)):
        now_feat = get_embedding(options[j].strip(), word_vectors, get_vector)
        if np.abs(now_feat.sum()) > 0:
            cnt_word += 1
            feat += now_feat
    if cnt_word > 0:
        feat = feat / cnt_word

    if np.abs(feat.sum()) == 0:
        return feat
    else:
        # feat = feat / (np.linalg.norm(feat) + 1e-6)
        return feat 
Example #8
Source File: util_file.py    From nball4tree with MIT License 6 votes vote down vote up
def create_parent_children_file(ln, ofile="/Users/tdong/data/glove_wordSenseChildren.txt",
                                w2vile= "/Users/tdong/data/glove/glove.6B.50d.txt"):
    """
    the problem of this method is: a->b->c, but b is not in the w2v file, a and c are in the w2v.
    the relation between a->c is brocken
    :param ln:
    :param ofile:
    :param w2vile:
    :return:
    """
    lst = ln.split()
    lines = [" ".join(["*root*"] + lst + ["\n"])]
    with open(w2vile, 'r') as vfh:
        vocLst = [word.split()[0] for word in vfh.readlines()]
    while lst:
        parent = lst.pop(0)
        children = [ele.name() for ele in wn.synset(parent).hyponyms() if ele.name().split('.')[0] in vocLst]
        newLine = " ".join([parent] + children + ["\n"])
        lines.append(newLine)
        print(parent, "::", children)
        lst += children
    with open(ofile, 'w') as ofh:
        ofh.write("".join(lines)) 
Example #9
Source File: convert_gcn.py    From Context-aware-ZSR with MIT License 6 votes vote down vote up
def get_synset_embedding(synset, word_vectors, get_vector):
    class_name = wn.synset(synset).lemma_names()
    class_name = ', '.join([_.replace('_', ' ') for _ in class_name])
    class_name = class_name.lower()

    feat = np.zeros(feat_len)

    options = class_name.split(',')
    cnt_word = 0
    for j in range(len(options)):
        now_feat = get_embedding(options[j].strip(), word_vectors, get_vector)
        if np.abs(now_feat.sum()) > 0:
            cnt_word += 1
            feat += now_feat
    if cnt_word > 0:
        feat = feat / cnt_word

    if np.abs(feat.sum()) == 0:
        return feat
    else:
        # feat = feat / (np.linalg.norm(feat) + 1e-6)
        return feat 
Example #10
Source File: util_file.py    From nball4tree with MIT License 6 votes vote down vote up
def clean_wordsense_path(ifile="", w2vFile ="", ofile=""):
    lines = []
    with open(w2vFile, 'r') as ifh:
        vocLst = [ele.split()[0] for ele in ifh.readlines()]
    with open(ifile, 'r') as ifh:
        for ln in ifh:
            wlst = ln.strip().split()
            if len(wlst) > 2:
                node = wlst[0]
                lsts = [[ele.name() for ele in lst if ele.name().split(".")[0] in vocLst]
                        for lst in wn.synset(node).hypernym_paths()]
                wspath = sorted(lsts, key = len)[-1]
                lines.append(" ".join(wlst[:2] + wspath+["\n"]))
            else:
                lines.append(ln)
    with open(ofile, 'w') as ofh:
        ofh.write("".join(lines)) 
Example #11
Source File: wordnet_app.py    From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
def _get_synset(synset_key):
    """
    The synset key is the unique name of the synset, this can be
    retrived via synset.name()
    """
    return wn.synset(synset_key) 
Example #12
Source File: wordnet_app.py    From luscan-devel with GNU General Public License v2.0 5 votes vote down vote up
def toggle_synset_relation(self, synset, relation):
        """
        Toggle the display of the relations for the given synset and
        relation type.

        This function will throw a KeyError if the synset is currently
        not being displayed.
        """
        if relation in self.synset_relations[synset.name]:
            self.synset_relations[synset.name].remove(relation)
        else:
            self.synset_relations[synset.name].add(relation)

        return self 
Example #13
Source File: wordnet_app.py    From luscan-devel with GNU General Public License v2.0 5 votes vote down vote up
def toggle_synset(self, synset):
        """
        Toggle displaying of the relation types for the given synset
        """
        if synset.name in self.synset_relations.keys():
            del self.synset_relations[synset.name]
        else:
            self.synset_relations[synset.name] = set()

        return self 
Example #14
Source File: poincare.py    From poincare_embeddings with MIT License 5 votes vote down vote up
def get_hyponyms(synset, level):
    if (level == last_level):
        levelOfNode[str(synset)] = level
        return
    # BFS
    if not str(synset) in network:
        network[str(synset)] = [str(s) for s in synset.hyponyms()]
        levelOfNode[str(synset)] = level
    for hyponym in synset.hyponyms():
        get_hyponyms(hyponym, level + 1) 
Example #15
Source File: poincare_adam.py    From poincare_embeddings with MIT License 5 votes vote down vote up
def get_hyponyms(synset, level):
    if (level == last_level):
        levelOfNode[str(synset)] = level
        return
    # BFS
    if not str(synset) in network:
        network[str(synset)] = [str(s) for s in synset.hyponyms()]
        levelOfNode[str(synset)] = level
    for hyponym in synset.hyponyms():
        get_hyponyms(hyponym, level + 1) 
Example #16
Source File: process.py    From nball4tree with MIT License 5 votes vote down vote up
def count_frequency(ws):
    """
    count frequency of a word sense
    :param ws:
    :return:
    """
    synset = wn.synset(ws)
    freq = 0
    for lemma in synset.lemmas():
        if lemma.count() == 0:
            freq +=1
        else:
            freq += lemma.count()
    return freq 
Example #17
Source File: wordnet_app.py    From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
def lemma_property(word, synset, func):
    def flattern(l):
        if l == []:
            return []
        else:
            return l[0] + flattern(l[1:])

    return flattern([func(l) for l in synset.lemmas if l.name == word]) 
Example #18
Source File: wordnet_app.py    From luscan-devel with GNU General Public License v2.0 5 votes vote down vote up
def _collect_all_synsets(word, pos, synset_relations=dict()):
    """
    Return a HTML unordered list of synsets for the given word and
    part of speech.
    """
    return '<ul>%s\n</ul>\n' % \
        ''.join((_collect_one_synset(word, synset, synset_relations)
                 for synset
                 in wn.synsets(word, pos))) 
Example #19
Source File: wordnet_app.py    From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
def _collect_all_synsets(word, pos, synset_relations=dict()):
    """
    Return a HTML unordered list of synsets for the given word and
    part of speech.
    """
    return '<ul>%s\n</ul>\n' % ''.join(
        (
            _collect_one_synset(word, synset, synset_relations)
            for synset in wn.synsets(word, pos)
        )
    ) 
Example #20
Source File: wordnet_app.py    From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
def __init__(self, word, synset_relations=dict()):
        """
        Build a reference to a new page.

        word is the word or words (separated by commas) for which to
        search for synsets of

        synset_relations is a dictionary of synset keys to sets of
        synset relation identifaiers to unfold a list of synset
        relations for.
        """
        self.word = word
        self.synset_relations = synset_relations 
Example #21
Source File: wordnet_app.py    From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
def toggle_synset(self, synset):
        """
        Toggle displaying of the relation types for the given synset
        """
        if synset.name() in self.synset_relations:
            del self.synset_relations[synset.name()]
        else:
            self.synset_relations[synset.name()] = set()

        return self 
Example #22
Source File: sentiwordnet.py    From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
def senti_synsets(self, string, pos=None):
        from nltk.corpus import wordnet as wn

        sentis = []
        synset_list = wn.synsets(string, pos)
        for synset in synset_list:
            sentis.append(self.senti_synset(synset.name()))
        sentis = filter(lambda x: x, sentis)
        return sentis 
Example #23
Source File: sentiwordnet.py    From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
def __init__(self, pos_score, neg_score, synset):
        self._pos_score = pos_score
        self._neg_score = neg_score
        self._obj_score = 1.0 - (self._pos_score + self._neg_score)
        self.synset = synset 
Example #24
Source File: sentiwordnet.py    From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
def __str__(self):
        """Prints just the Pos/Neg scores for now."""
        s = "<"
        s += self.synset.name() + ": "
        s += "PosScore=%s " % self._pos_score
        s += "NegScore=%s" % self._neg_score
        s += ">"
        return s 
Example #25
Source File: sentiwordnet.py    From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
def __repr__(self):
        return "Senti" + repr(self.synset) 
Example #26
Source File: test_wordnet.py    From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
def test_hyperhyponyms(self):
        # Not every synset as hypernyms()
        self.assertEqual(S('travel.v.01').hypernyms(), [])
        self.assertEqual(S('travel.v.02').hypernyms(), [S('travel.v.03')])
        self.assertEqual(S('travel.v.03').hypernyms(), [])

        # Test hyper-/hyponyms.
        self.assertEqual(S('breakfast.n.1').hypernyms(), [S('meal.n.01')])
        first_five_meal_hypo = [
            S('banquet.n.02'),
            S('bite.n.04'),
            S('breakfast.n.01'),
            S('brunch.n.01'),
            S('buffet.n.02'),
        ]
        self.assertEqual(sorted(S('meal.n.1').hyponyms()[:5]), first_five_meal_hypo)
        self.assertEqual(S('Austen.n.1').instance_hypernyms(), [S('writer.n.01')])
        first_five_composer_hypo = [
            S('ambrose.n.01'),
            S('bach.n.01'),
            S('barber.n.01'),
            S('bartok.n.01'),
            S('beethoven.n.01'),
        ]
        self.assertEqual(
            S('composer.n.1').instance_hyponyms()[:5], first_five_composer_hypo
        )

        # Test root hyper-/hyponyms
        self.assertEqual(S('person.n.01').root_hypernyms(), [S('entity.n.01')])
        self.assertEqual(S('sail.v.01').root_hypernyms(), [S('travel.v.01')])
        self.assertEqual(
            S('fall.v.12').root_hypernyms(), [S('act.v.01'), S('fall.v.17')]
        ) 
Example #27
Source File: sentiwordnet.py    From razzy-spinner with GNU General Public License v3.0 5 votes vote down vote up
def __repr__(self):
        return "Senti" + repr(self.synset) 
Example #28
Source File: wordnet_app.py    From razzy-spinner with GNU General Public License v3.0 5 votes vote down vote up
def _get_synset(synset_key):
    """
    The synset key is the unique name of the synset, this can be
    retrived via synset.name()
    """
    return wn.synset(synset_key) 
Example #29
Source File: wordnet_app.py    From razzy-spinner with GNU General Public License v3.0 5 votes vote down vote up
def _collect_one_synset(word, synset, synset_relations):
    '''
    Returns the HTML string for one synset or word

    :param word: the current word
    :type word: str
    :param synset: a synset
    :type synset: synset
    :param synset_relations: information about which synset relations
    to display.
    :type synset_relations: dict(synset_key, set(relation_id))
    :return: The HTML string built for this synset
    :rtype: str
    '''
    if isinstance(synset, tuple): # It's a word
        raise NotImplementedError("word not supported by _collect_one_synset")

    typ = 'S'
    pos_tuple = _pos_match((synset.pos(), None, None))
    assert pos_tuple is not None, "pos_tuple is null: synset.pos(): %s" % synset.pos()
    descr = pos_tuple[2]
    ref = copy.deepcopy(Reference(word, synset_relations))
    ref.toggle_synset(synset)
    synset_label = typ + ";"
    if synset.name() in synset_relations:
        synset_label = _bold(synset_label)
    s = '<li>%s (%s) ' % (make_lookup_link(ref, synset_label), descr)
    def format_lemma(w):
        w = w.replace('_', ' ')
        if w.lower() == word:
            return _bold(w)
        else:
            ref = Reference(w)
            return make_lookup_link(ref, w)

    s += ', '.join(format_lemma(l.name()) for l in synset.lemmas())

    gl = " (%s) <i>%s</i> " % \
        (synset.definition(),
         "; ".join("\"%s\"" % e for e in synset.examples()))
    return s + gl + _synset_relations(word, synset, synset_relations) + '</li>\n' 
Example #30
Source File: wordnet_app.py    From razzy-spinner with GNU General Public License v3.0 5 votes vote down vote up
def _collect_all_synsets(word, pos, synset_relations=dict()):
    """
    Return a HTML unordered list of synsets for the given word and
    part of speech.
    """
    return '<ul>%s\n</ul>\n' % \
        ''.join((_collect_one_synset(word, synset, synset_relations)
                 for synset
                 in wn.synsets(word, pos)))