我编写了用于使用NLTK wordnet查找上位词和下位词的代码。

这是我的代码(这是下义词的示例):

from nltk.corpus import wordnet as wn

word1 = ['learn']
word2 = ['study']

def getSynonyms(words):
    synonymList1 = []
    wordnetSynset1 = wn.synsets(words)
    tempList1=[]
    for synset1 in wordnetSynset1:
        synLemmas = synset1.hyponyms()
        for i in xrange(len(synLemmas)):
            word = synLemmas[i] #.replace('_',' ')
            if word not in tempList1:
                tempList1.append(word)
    synonymList1.append(tempList1)
    return synonymList1

def cekSynonyms(word1, word2):
    tmp = 0
    for i in xrange(len(word1)):
        for j in xrange(len(word2)):
            getsyn1 = getSynonyms(word1[i])
            getsyn2 = getSynonyms(word2[j])
            ds1 = [x for y in getsyn1 for x in y]
            ds2 = [x for y in getsyn2 for x in y]
            print ds1,"\n",ds2,"\n\n"
            for k in xrange(len(ds1)):
            for l in xrange(len(ds2)):
                if ds1[k] == ds2[l]:
                    tmp = 1
    return tmp

print cekSynonyms(word1, word2)
print


这是输出:

[Synset('absorb.v.02'), Synset('catch_up.v.02'), Synset('relearn.v.01'), Synset('study.v.05'), Synset('ascertain.v.04'), Synset('discover.v.04'), Synset('get_the_goods.v.01'), Synset('trip_up.v.01'), Synset('wise_up.v.01'), Synset('understudy.v.01'), Synset('audit.v.02'), Synset('drill.v.03'), Synset('train.v.02'), Synset('catechize.v.02'), Synset('coach.v.01'), Synset('condition.v.01'), Synset('drill.v.04'), Synset('enlighten.v.01'), Synset('ground.v.04'), Synset('indoctrinate.v.01'), Synset('induct.v.05'), Synset('lecture.v.01'), Synset('mentor.v.01'), Synset('reinforce.v.02'), Synset('spoonfeed.v.02'), Synset('train.v.01'), Synset('tutor.v.01'), Synset('unteach.v.01'), Synset('unteach.v.02'), Synset('test.v.06')]

[Synset('resurvey.n.01'), Synset('assay.n.03'), Synset('blue_book.n.01'), Synset('case_study.n.01'), Synset('green_paper.n.01'), Synset('medical_report.n.01'), Synset('position_paper.n.01'), Synset('progress_report.n.01'), Synset('white_book.n.01'), Synset('allometry.n.01'), Synset('architecture.n.02'), Synset('bibliotics.n.01'), Synset('communications.n.01'), Synset('engineering.n.02'), Synset('escapology.n.01'), Synset('frontier.n.03'), Synset('futurology.n.01'), Synset('genealogy.n.02'), Synset('graphology.n.01'), Synset('humanistic_discipline.n.01'), Synset('major.n.04'), Synset('military_science.n.01'), Synset('numerology.n.01'), Synset('occultism.n.01'), Synset('ology.n.01'), Synset('protology.n.01'), Synset('science.n.01'), Synset('theogony.n.01'), Synset('theology.n.01'), Synset('design.n.06'), Synset('draft.n.03'), Synset('vignette.n.03'), Synset('lucubration.n.02'), Synset('anatomize.v.02'), Synset('assay.v.01'), Synset('audit.v.01'), Synset('check.v.01'), Synset('compare.v.01'), Synset('diagnose.v.01'), Synset('diagnose.v.02'), Synset('investigate.v.01'), Synset('review.v.01'), Synset('screen.v.02'), Synset('sieve.v.02'), Synset('survey.v.01'), Synset('survey.v.05'), Synset('trace.v.01'), Synset('view.v.02'), Synset('major.v.01'), Synset('compare.v.03'), Synset('factor.v.03'), Synset('audit.v.02'), Synset('drill.v.03'), Synset('train.v.02'), Synset('cram.v.03'), Synset('memorize.v.01')]

1


我的问题是如何删除上位词和下位词的Synset( ).pos_tags.numbers

所以只显示像['train', 'memorize']这样的词

我试过synLemmas = synset1.lemma_names()word = synLemmas[i].replace('_',' '),它可以工作。这是输出:

[u'learn', u'larn', u'acquire', u'hear', u'get word', u'get wind', u'pick up', u'find out', u'get a line', u'discover', u'see', u'memorize', u'memorise', u'con', u'study', u'read', u'take', u'teach', u'instruct', u'determine', u'check', u'ascertain', u'watch']

[u'survey', u'study', u'work', u'report', u'written report', u'discipline', u'subject', u'subject area', u'subject field', u'field', u'field of study', u'bailiwick', u'sketch', u'cogitation', u'analyze', u'analyse', u'examine', u'canvass', u'canvas', u'consider', u'learn', u'read', u'take', u'hit the books', u'meditate', u'contemplate']

最佳答案

通过编程,Synsets对象不是字符串; P

您可以使用内置的type函数检查任何Python对象的类型:

>>> x = 'Foo bar'
>>> type(x)
<class 'str'>

>>> from nltk.corpus import wordnet as wn
>>> wn.synsets('dog')
[Synset('dog.n.01'), Synset('frump.n.01'), Synset('dog.n.03'), Synset('cad.n.01'), Synset('frank.n.02'), Synset('pawl.n.01'), Synset('andiron.n.01'), Synset('chase.v.01')]

>>> type(wn.synsets('dog'))
<class 'list'>

>>> type(wn.synsets('dog')[0])
<class 'nltk.corpus.reader.wordnet.Synset'>


从语言上讲,同义词集是概念/含义/思想。
一个单词可以有多种含义,因此有多个同义词集。
一种含义可以用不同的词/词义表达。

如果我们查看单词dog,我们会看到它指向多个同义词集并具有不同的定义:

>>> from nltk.corpus import wordnet as wn

>>> wn.synsets('dog')
[Synset('dog.n.01'), Synset('frump.n.01'), Synset('dog.n.03'), Synset('cad.n.01'), Synset('frank.n.02'), Synset('pawl.n.01'), Synset('andiron.n.01'), Synset('chase.v.01')]

>>> for ss in wn.synsets('dog'):
...     print (ss, ':',  ss.definition())
...
Synset('dog.n.01') : a member of the genus Canis (probably descended from the common wolf) that has been domesticated by man since prehistoric times; occurs in many breeds
Synset('frump.n.01') : a dull unattractive unpleasant girl or woman
Synset('dog.n.03') : informal term for a man
Synset('cad.n.01') : someone who is morally reprehensible
Synset('frank.n.02') : a smooth-textured sausage of minced beef or pork usually smoked; often served on a bread roll
Synset('pawl.n.01') : a hinged catch that fits into a notch of a ratchet to move a wheel forward or prevent it from moving backward
Synset('andiron.n.01') : metal supports for logs in a fireplace
Synset('chase.v.01') : go after with the intent to catch


并且每个同义词集可以表示为不同的词/引理:

>>> for ss in wn.synsets('dog'):
...     print (ss, ':',  ss.lemma_names())
...
Synset('dog.n.01') : ['dog', 'domestic_dog', 'Canis_familiaris']
Synset('frump.n.01') : ['frump', 'dog']
Synset('dog.n.03') : ['dog']
Synset('cad.n.01') : ['cad', 'bounder', 'blackguard', 'dog', 'hound', 'heel']
Synset('frank.n.02') : ['frank', 'frankfurter', 'hotdog', 'hot_dog', 'dog', 'wiener', 'wienerwurst', 'weenie']
Synset('pawl.n.01') : ['pawl', 'detent', 'click', 'dog']
Synset('andiron.n.01') : ['andiron', 'firedog', 'dog', 'dog-iron']
Synset('chase.v.01') : ['chase', 'chase_after', 'trail', 'tail', 'tag', 'give_chase', 'dog', 'go_after', 'track']


由于我们知道每个单词都代表单词网中的多个同义词集,因此您无法从单词/引理中访问超级/同义字。

要访问超/高义词,您首先需要先消除上下文中单词的含义。


句子:我早餐吃了一条狗。
歧义词:狗
消除歧义的同义词集:同义词集('frank.n.02')


只有在知道哪个同义词集是上下文中单词的正确含义之后,您才能访问同义词集的上位词,例如

>>> wn.synsets('dog')[4]
Synset('frank.n.02')
>>> wn.synsets('dog')[4].hypernyms()
[Synset('sausage.n.01')]
>>> wn.synsets('dog')[4].hypernyms()[0]
Synset('sausage.n.01')
>>> wn.synsets('dog')[4].hypernyms()[0].lemma_names()
['sausage']

10-04 23:23