本文整理汇总了Python中nltk.corpus.reader.wordnet.Synset方法的典型用法代码示例。如果您正苦于以下问题:Python wordnet.Synset方法的具体用法?Python wordnet.Synset怎么用?Python wordnet.Synset使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nltk.corpus.reader.wordnet
的用法示例。
在下文中一共展示了wordnet.Synset方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: morphy
# 需要导入模块: from nltk.corpus.reader import wordnet [as 别名]
# 或者: from nltk.corpus.reader.wordnet import Synset [as 别名]
def morphy(token):
"""emulates wn.morphy"""
if ' ' in token:
return None
if token in data._words:
return token
morph = data._morphy(token)
if morph in data._words:
return morph
return None
###############################################################################
################ Synset Wrapper, Replacement, and Access Methods ##############
###############################################################################
# updates synset based on current language
示例2: _synset_update
# 需要导入模块: from nltk.corpus.reader import wordnet [as 别名]
# 或者: from nltk.corpus.reader.wordnet import Synset [as 别名]
def _synset_update(synset):
language = data._language
if dir(synset)[0] == _indicator:
if synset._language != language:
PWN_name = synset._PWN_name
if language == 'en':
setattr(synset, '_name', PWN_name)
else:
setattr(synset, '_name', data._synset2lang.get(PWN_name, _dummy+PWN_name))
setattr(synset, '_language', language)
else:
PWN_name = synset._name
setattr(synset, '_PWN_name', PWN_name)
if language != 'en':
setattr(synset, '_name', data._synset2lang.get(PWN_name, _dummy+PWN_name))
setattr(synset, _indicator, None)
setattr(synset, '_language', language)
# wrapper to update class 'Synset' before function execution
示例3: get_siblings
# 需要导入模块: from nltk.corpus.reader import wordnet [as 别名]
# 或者: from nltk.corpus.reader.wordnet import Synset [as 别名]
def get_siblings(self, synset: Synset):
"""
Returns up to five siblings of the synset.
:param synset: The synset to obtain the siblings from
:return: The siblings obtained from the synset
"""
siblings = []
sibling_count = 0
parent = self.get_parent(synset)
for sibling in parent.hyponyms():
if sibling_count == 5:
break
if sibling != synset and self.valid_synset(sibling):
siblings.insert(sibling_count, sibling)
sibling_count += 1
return siblings
示例4: _lemma
# 需要导入模块: from nltk.corpus.reader import wordnet [as 别名]
# 或者: from nltk.corpus.reader.wordnet import Synset [as 别名]
def _lemma(name, synset):
language = data._language
synset_name = synset.name()
for lemma_name, lemma in _lemmas[language][synset_name].items():
if name == lemma_name:
return lemma
lemma = Lemma(WordNetCorpusReader, synset, name, 0, 0, None)
_lemmas[language][synset_name][name] = lemma
return lemma
# replacement for 'lemmas' method of class 'Synset'
示例5: _synset_lemmas
# 需要导入模块: from nltk.corpus.reader import wordnet [as 别名]
# 或者: from nltk.corpus.reader.wordnet import Synset [as 别名]
def _synset_lemmas(self):
if data._language == 'en':
return self._lemmas
return [_lemma(name, self) for name in data._synset2lemmas.get(self.name(), [])]
# replacement for 'lemma_names' method of class 'Synset'
示例6: get_synset_id
# 需要导入模块: from nltk.corpus.reader import wordnet [as 别名]
# 或者: from nltk.corpus.reader.wordnet import Synset [as 别名]
def get_synset_id(synset: Synset):
"""
Get the corresponding synset id of the synset.
:param synset: The synset to extract the id from
:return: The corresponding synset id
"""
sid = "n{}".format(str(synset.offset()).zfill(8))
return sid
示例7: valid_synset
# 需要导入模块: from nltk.corpus.reader import wordnet [as 别名]
# 或者: from nltk.corpus.reader.wordnet import Synset [as 别名]
def valid_synset(self, synset: Synset):
"""
Determines if the synset is valid by checking to see that it is in ImageNet.
:param synset: The synset to check for validity
:return: A boolean determining whether or not the synset is in ImageNet
"""
sid = self.get_synset_id(synset)
return sid in self.API.all_synsets
示例8: get_parent
# 需要导入模块: from nltk.corpus.reader import wordnet [as 别名]
# 或者: from nltk.corpus.reader.wordnet import Synset [as 别名]
def get_parent(synset: Synset):
"""
Returns one of the parents of the synset.
:param synset: The synset to obtain the parent from
:return: One of the parents of the synset
"""
return random.choice(synset.hypernyms())
示例9: get_grandparents
# 需要导入模块: from nltk.corpus.reader import wordnet [as 别名]
# 或者: from nltk.corpus.reader.wordnet import Synset [as 别名]
def get_grandparents(synset: Synset):
"""
Returns all grandparents of the synset.
:param synset: The synset to obtain the grandparents from
:return: The grandparents of the synset
"""
grandparents = []
for parent in synset.hypernyms():
grandparents.extend(parent.hypernyms())
return grandparents
示例10: _synset_relations
# 需要导入模块: from nltk.corpus.reader import wordnet [as 别名]
# 或者: from nltk.corpus.reader.wordnet import Synset [as 别名]
def _synset_relations(word, synset, synset_relations):
'''
Builds the HTML string for the relations of a synset
:param word: The current word
:type word: str
:param synset: The synset for which we're building the relations.
:type synset: Synset
:param synset_relations: synset keys and relation types for which to display relations.
:type synset_relations: dict(synset_key, set(relation_type))
:return: The HTML for a synset's relations
:rtype: str
'''
if not synset.name() in synset_relations:
return ""
ref = Reference(word, synset_relations)
def relation_html(r):
if isinstance(r, Synset):
return make_lookup_link(Reference(r.lemma_names()[0]), r.lemma_names()[0])
elif isinstance(r, Lemma):
return relation_html(r.synset())
elif isinstance(r, tuple):
# It's probably a tuple containing a Synset and a list of
# similar tuples. This forms a tree of synsets.
return "%s\n<ul>%s</ul>\n" % \
(relation_html(r[0]),
''.join('<li>%s</li>\n' % relation_html(sr) for sr in r[1]))
else:
raise TypeError("r must be a synset, lemma or list, it was: type(r) = %s, r = %s" % (type(r), r))
def make_synset_html(db_name, disp_name, rels):
synset_html = '<i>%s</i>\n' % \
make_lookup_link(
copy.deepcopy(ref).toggle_synset_relation(synset, db_name).encode(),
disp_name)
if db_name in ref.synset_relations[synset.name()]:
synset_html += '<ul>%s</ul>\n' % \
''.join("<li>%s</li>\n" % relation_html(r) for r in rels)
return synset_html
html = '<ul>' + \
'\n'.join(("<li>%s</li>" % make_synset_html(*rel_data) for rel_data
in get_relations_data(word, synset)
if rel_data[2] != [])) + \
'</ul>'
return html