本文整理汇总了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))