當前位置: 首頁>>代碼示例>>Python>>正文


Python wordnet.synset方法代碼示例

本文整理匯總了Python中nltk.corpus.wordnet.synset方法的典型用法代碼示例。如果您正苦於以下問題:Python wordnet.synset方法的具體用法?Python wordnet.synset怎麽用?Python wordnet.synset使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在nltk.corpus.wordnet的用法示例。


在下文中一共展示了wordnet.synset方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: get_static_welcome_message

# 需要導入模塊: from nltk.corpus import wordnet [as 別名]
# 或者: from nltk.corpus.wordnet import synset [as 別名]
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>
""" 
開發者ID:rafasashi,項目名稱:razzy-spinner,代碼行數:25,代碼來源:wordnet_app.py

示例2: senti_synset

# 需要導入模塊: from nltk.corpus import wordnet [as 別名]
# 或者: from nltk.corpus.wordnet import synset [as 別名]
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 
開發者ID:rafasashi,項目名稱:razzy-spinner,代碼行數:18,代碼來源:sentiwordnet.py

示例3: get_synset_embedding

# 需要導入模塊: from nltk.corpus import wordnet [as 別名]
# 或者: from nltk.corpus.wordnet import synset [as 別名]
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 
開發者ID:ruotianluo,項目名稱:Context-aware-ZSR,代碼行數:24,代碼來源:convert_gcn.py

示例4: get_synset_embedding

# 需要導入模塊: from nltk.corpus import wordnet [as 別名]
# 或者: from nltk.corpus.wordnet import synset [as 別名]
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 
開發者ID:ruotianluo,項目名稱:Context-aware-ZSR,代碼行數:24,代碼來源:convert_from_bansal.py

示例5: create_parent_children_file

# 需要導入模塊: from nltk.corpus import wordnet [as 別名]
# 或者: from nltk.corpus.wordnet import synset [as 別名]
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)) 
開發者ID:gnodisnait,項目名稱:nball4tree,代碼行數:25,代碼來源:util_file.py

示例6: clean_wordsense_path

# 需要導入模塊: from nltk.corpus import wordnet [as 別名]
# 或者: from nltk.corpus.wordnet import synset [as 別名]
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)) 
開發者ID:gnodisnait,項目名稱:nball4tree,代碼行數:19,代碼來源:util_file.py

示例7: get_static_welcome_message

# 需要導入模塊: from nltk.corpus import wordnet [as 別名]
# 或者: from nltk.corpus.wordnet import synset [as 別名]
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>
""" 
開發者ID:V1EngineeringInc,項目名稱:V1EngineeringInc-Docs,代碼行數:24,代碼來源:wordnet_app.py

示例8: senti_synset

# 需要導入模塊: from nltk.corpus import wordnet [as 別名]
# 或者: from nltk.corpus.wordnet import synset [as 別名]
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 
開發者ID:V1EngineeringInc,項目名稱:V1EngineeringInc-Docs,代碼行數:23,代碼來源:sentiwordnet.py

示例9: lemma_property

# 需要導入模塊: from nltk.corpus import wordnet [as 別名]
# 或者: from nltk.corpus.wordnet import synset [as 別名]
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]) 
開發者ID:rafasashi,項目名稱:razzy-spinner,代碼行數:11,代碼來源:wordnet_app.py

示例10: _get_synset

# 需要導入模塊: from nltk.corpus import wordnet [as 別名]
# 或者: from nltk.corpus.wordnet import synset [as 別名]
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) 
開發者ID:rafasashi,項目名稱:razzy-spinner,代碼行數:8,代碼來源:wordnet_app.py

示例11: _collect_one_synset

# 需要導入模塊: from nltk.corpus import wordnet [as 別名]
# 或者: from nltk.corpus.wordnet import synset [as 別名]
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' 
開發者ID:rafasashi,項目名稱:razzy-spinner,代碼行數:43,代碼來源:wordnet_app.py

示例12: _collect_all_synsets

# 需要導入模塊: from nltk.corpus import wordnet [as 別名]
# 或者: from nltk.corpus.wordnet import synset [as 別名]
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))) 
開發者ID:rafasashi,項目名稱:razzy-spinner,代碼行數:11,代碼來源:wordnet_app.py

示例13: toggle_synset_relation

# 需要導入模塊: from nltk.corpus import wordnet [as 別名]
# 或者: from nltk.corpus.wordnet import synset [as 別名]
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 
開發者ID:rafasashi,項目名稱:razzy-spinner,代碼行數:16,代碼來源:wordnet_app.py

示例14: toggle_synset

# 需要導入模塊: from nltk.corpus import wordnet [as 別名]
# 或者: from nltk.corpus.wordnet import synset [as 別名]
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 
開發者ID:rafasashi,項目名稱:razzy-spinner,代碼行數:12,代碼來源:wordnet_app.py

示例15: all_senti_synsets

# 需要導入模塊: from nltk.corpus import wordnet [as 別名]
# 或者: from nltk.corpus.wordnet import synset [as 別名]
def all_senti_synsets(self):
        from nltk.corpus import wordnet as wn
        for key, fields in self._db.items():
            pos, offset = key
            pos_score, neg_score = fields
            synset = wn._synset_from_pos_and_offset(pos, offset)
            yield SentiSynset(pos_score, neg_score, synset) 
開發者ID:rafasashi,項目名稱:razzy-spinner,代碼行數:9,代碼來源:sentiwordnet.py


注:本文中的nltk.corpus.wordnet.synset方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。