本文整理匯總了Python中nltk.corpus.reader.wordnet.Lemma方法的典型用法代碼示例。如果您正苦於以下問題:Python wordnet.Lemma方法的具體用法?Python wordnet.Lemma怎麽用?Python wordnet.Lemma使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類nltk.corpus.reader.wordnet
的用法示例。
在下文中一共展示了wordnet.Lemma方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: lemma
# 需要導入模塊: from nltk.corpus.reader import wordnet [as 別名]
# 或者: from nltk.corpus.reader.wordnet import Lemma [as 別名]
def lemma(name):
"""emulates wn.lemma"""
chunks = name.split('.')
i = 3
success = 0
while not success:
try:
synsetname = '.'.join(chunks[:i])
lemmas = data._synset2lemmas[synsetname]
success = 1
except KeyError:
i += 1
if name in lemmas:
lemmaname = '.'.join(chunks[i:])
return _lemma(lemmaname, synset(synsetname))
raise WordNetError('Lemma ' + name + ' Not Found')
示例2: synsets
# 需要導入模塊: from nltk.corpus.reader import wordnet [as 別名]
# 或者: from nltk.corpus.reader.wordnet import Lemma [as 別名]
def synsets(token, pos='anrsv'):
"""emulates wn.synsets"""
morph = morphy(token)
if morph:
return [wn.synset(name) for name in data._word2synsets[morph] if name.split('.')[-2] in pos]
return []
###############################################################################
################ Lemma Wrapper, Replacement, and Access Methods ###############
###############################################################################
# instantiation of Lemma object
示例3: _lemma
# 需要導入模塊: from nltk.corpus.reader import wordnet [as 別名]
# 或者: from nltk.corpus.reader.wordnet import Lemma [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'
示例4: _synset_lemma_names
# 需要導入模塊: from nltk.corpus.reader import wordnet [as 別名]
# 或者: from nltk.corpus.reader.wordnet import Lemma [as 別名]
def _synset_lemma_names(self):
if data._language == 'en':
return self._lemma_names
return [name for name in data._synset2lemmas.get(self.name(), [])]
# wrapper for lexical relation functions of class 'Lemma'
示例5: _lexical_relation_wrapper
# 需要導入模塊: from nltk.corpus.reader import wordnet [as 別名]
# 或者: from nltk.corpus.reader.wordnet import Lemma [as 別名]
def _lexical_relation_wrapper(func):
def wrapper(self):
if data._language == 'en':
return func(self)
return sum((lemma2.synset().lemmas() for lemma1 in wn.synset(self._PWN_name)._lemmas for lemma2 in func(lemma1)), [])
return wrapper
# replacement for 'count' method of class 'Lemma'
示例6: _synset_relations
# 需要導入模塊: from nltk.corpus.reader import wordnet [as 別名]
# 或者: from nltk.corpus.reader.wordnet import Lemma [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