本文整理匯總了Python中nltk.corpus.wordnet.all_synsets方法的典型用法代碼示例。如果您正苦於以下問題:Python wordnet.all_synsets方法的具體用法?Python wordnet.all_synsets怎麽用?Python wordnet.all_synsets使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類nltk.corpus.wordnet
的用法示例。
在下文中一共展示了wordnet.all_synsets方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: process_definitions
# 需要導入模塊: from nltk.corpus import wordnet [as 別名]
# 或者: from nltk.corpus.wordnet import all_synsets [as 別名]
def process_definitions(self):
self.definition_map = {}
self.lemmakey_to_synset = {}
n_empty_definitions = 0
print ("Processing definitions")
all_synsets = wn.all_synsets()
for s in tqdm(all_synsets):
definition = s.definition().strip()
if len(definition) == 0:
n_empty_definitions = n_empty_definitions + 1
self.definition_map[s.name()] = definition
lemmas = s.lemmas()
for lemma in lemmas:
key = lemma.key()
self.lemmakey_to_synset[key] = s.name()
print ("#Empty definitions {}/{}".format(n_empty_definitions, len(self.definition_map)))
synsets = sorted(self.definition_map.keys())
#self.synset_to_idx = {v:i for i,v in enumerate(self.synset_to_definition.keys())}
self.synset_to_idx = {v:i for i,v in enumerate(synsets)}
self.idx_to_synset = {v:i for i,v in self.synset_to_idx.items()}
self.definitions = [self.definition_map[k] for k in synsets]
示例2: buildNouns
# 需要導入模塊: from nltk.corpus import wordnet [as 別名]
# 或者: from nltk.corpus.wordnet import all_synsets [as 別名]
def buildNouns():
"""
Returns the set of all nouns of NLTK
"""
return {x.name().split('.', 1)[0] for x in wn.all_synsets('n')}
示例3: buildVerbs
# 需要導入模塊: from nltk.corpus import wordnet [as 別名]
# 或者: from nltk.corpus.wordnet import all_synsets [as 別名]
def buildVerbs():
"""
Returns the set of all verbs of NLTK
"""
return {x.name().split(".", 1)[0] for x in wn.all_synsets("v")}
示例4: extract_wordnet_from_nltk
# 需要導入模塊: from nltk.corpus import wordnet [as 別名]
# 或者: from nltk.corpus.wordnet import all_synsets [as 別名]
def extract_wordnet_from_nltk(entity_output_file, relation_output_file):
from nltk.corpus import wordnet as wn
import json
# each node is a synset or synset+lemma
# synsets have POS
# synsets have several lemmas associated with them
# each lemma is keyed by something like able%3:00:00::
# where string = lemma, first number is POS, then sense id
#
# in addition to the synset-synset and lemma-lemma relationships,
# we will also add synset_lemma relationship for lemmas contained
# in each synset
with open(entity_output_file, 'w') as fent, \
open(relation_output_file, 'w') as frel:
for synset in wn.all_synsets():
node = {
'id': synset.name(),
'pos': synset.pos(),
'lemmas': [lem.key() for lem in synset.lemmas()],
'examples': synset.examples(),
'definition': synset.definition(),
'type': 'synset',
}
fent.write(json.dumps(node) + '\n')
# synset-synset relationships
for relation in SYNSET_RELATIONSHIP_TYPES:
entity2 = [rel_synset.name()
for rel_synset in getattr(synset, relation)()]
for e2 in entity2:
frel.write('{}\t{}\t{}\n'.format(synset.name(), 'synset_' + relation, e2))
# now get synset-lemma and lemma-lemma relationships
for lemma in synset.lemmas():
node = {
'id': lemma.key(),
'pos': synset.pos(),
'synset': synset.name(),
'type': 'lemma',
'count': lemma.count(),
}
fent.write(json.dumps(node) + '\n')
frel.write('{}\t{}\t{}\n'.format(synset.name(), 'synset_lemma', lemma.key()))
# lemma-lemma
for relation in LEMMA_RELATIONSHIP_TYPES:
entity2 = [rel_lemma.key()
for rel_lemma in getattr(lemma, relation)()]
for e2 in entity2:
frel.write('{}\t{}\t{}\n'.format(synset.name(), 'lemma_' + relation, e2))