本文整理汇总了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>
"""
示例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
示例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
示例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
示例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))
示例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))
示例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>
"""
示例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
示例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])
示例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)
示例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'
示例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)))
示例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
示例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
示例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)