gpt4 book ai didi

python - 打印词性以及单词的同义词

转载 作者:行者123 更新时间:2023-12-04 03:12:06 25 4
gpt4 key购买 nike

我有以下代码用于从输入文本文件中获取一个单词并使用 WordNet 打印该单词的同义词、定义和例句。它根据词性将同义词与同义词分开,即动词的同义词和形容词的同义词分开打印。

单词 flabbergasted 的同义词有 1) flabbergast , boggle , bowl over 是动词和 2) dumbfounded , dumfounded , flabbergasted , stupefied , Thunderstruck ,dumbstruck ,dumbstricken 是形容词。

如何打印词性和同义词?我在下面提供了到目前为止的代码:


import nltk
from nltk.corpus import wordnet as wn
tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
fp = open('sample.txt','r')
data = fp.read()
tokens= nltk.wordpunct_tokenize(data)
text = nltk.Text(tokens)
words = [w.lower() for w in text]
for a in words:
print a
syns = wn.synsets(a)
for s in syns:
print
print "definition:" s.definition
print "synonyms:"
for l in s.lemmas:
print l.name
print "examples:"
for b in s.examples:
print b
print

最佳答案

只需调用pos()在同义词集上。列出引理的所有 POS:

>>> from nltk.corpus import wordnet as wn
>>> syns = wn.synsets('dog')
>>> set([x.pos() for x in syns])
{'n', 'v'}

不幸的是,除了 source code 之外,其他任何地方似乎都没有记录。 ,它显示了可以在同义词集上调用的其他方法。

Synset attributes, accessible via methods with the same name:

  • name: The canonical name of this synset, formed using the first lemma of this synset. Note that this may be different from the name passed to the constructor if that string used a different lemma to
    identify the synset.
  • pos: The synset's part of speech, matching one of the module level attributes ADJ, ADJ_SAT, ADV, NOUN or VERB.
  • lemmas: A list of the Lemma objects for this synset.
  • definition: The definition for this synset.
  • examples: A list of example strings for this synset.
  • offset: The offset in the WordNet dict file of this synset.
  • lexname: The name of the lexicographer file containing this synset.

关于python - 打印词性以及单词的同义词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5966773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com