【问题标题】:Create Dictionary of Words and Synonyms创建单词和同义词词典
【发布时间】:2019-03-21 15:44:19
【问题描述】:

我是 Python 的初学者,我有一个字典,如下所示:

thisdict ={  "Animal",  "Metal",  "Car"}

我得到他们的同义词如下:

syns = {w : [] for w in thisdict}
for k, v in syns.items():
    for synset in wordnet.synsets(k):
        for lemma in synset.lemmas():
            v.append(lemma.name())
            print(syns)

目前,Animal 的 syns 输出为:

 {'Animal': ['animal']}
 {'Animal': ['animal', 'animate_being']}
 {'Animal': ['animal', 'animate_being', 'beast']}
 {'Animal': ['animal', 'animate_being', 'beast', 'brute']}
 {'Animal': ['animal', 'animate_being', 'beast', 'brute', 'creature']}
 {'Animal': ['animal', 'animate_being', 'beast', 'brute', 'creature', 'fauna']}
{'Animal': ['animal', 'animate_being', 'beast', 'brute', 'creature', 'fauna', 'animal']}
{'Animal': ['animal', 'animate_being', 'beast', 'brute', 'creature', 'fauna', 'animal', 'carnal']}
{'Animal': ['animal', 'animate_being', 'beast', 'brute', 'creature', 'fauna', 'animal', 'carnal', 'fleshly']}
{'Animal': ['animal', 'animate_being', 'beast', 'brute', 'creature', 'fauna', 'animal', 'carnal', 'fleshly', 'sensual']}

我的问题是,有没有办法创建一个字典,其中每行包含一个单词并且它是同义词,例如:

Animal: 'animal', 'animate_being', 'beast', 'brute', 'creature', 'fauna', 'animal', 'carnal', 'fleshly', 'sensual' 
Cat: ...

编辑 感谢 DannyMoshe,我在追加之前添加了 if not key.lower() == lemma.name(): 现在有以下输出:

['animate_being']
['animate_being', 'beast']
['animate_being', 'beast', 'brute']
['animate_being', 'beast', 'brute', 'creature']
['animate_being', 'beast', 'brute', 'creature', 'fauna']
['animate_being', 'beast', 'brute', 'creature', 'fauna', 'carnal']
['animate_being', 'beast', 'brute', 'creature', 'fauna', 'carnal', 'fleshly']
['animate_being', 'beast', 'brute', 'creature', 'fauna', 'carnal', 'fleshly', 'sensual']

有没有办法选择最后一行,['animate_being', 'beast', 'brute', 'creature', 'fauna', 'carnal', 'fleshly', 'sensual'],并匹配它在这个dict里给Animal?

【问题讨论】:

  • 这不是你已经创建的吗?还是我误解了你想要达到的目标。
  • 我试图只列出同义词,所以 animate_being、beast 等而不是 {'Animal': ['animal', 'animate_being', 'beast', 'brute' ,“生物”,“动物”,“动物”,“肉体”,“肉体”,“感性”]}。这个想法是我想创建一个字典(甚至是一个数据框),其中一列有一个单词列表,如果有意义的话,下一列有每个单词的同义词按行水平显示
  • 那么在您提供的示例中,您想从列表中删除动物?
  • 是的,并且只保留最后一行包含我想在 thisdict 中与 Animal 匹配的所有同义词。
  • 所以在追加之前,只需添加一个 if 语句:if not k.lower() == lema.name(): v.append(lemma.name())。我认为这应该会给你想要的输出..

标签: python-3.x dictionary wordnet


【解决方案1】:

我认为这是您正在寻找的完整答案:

syns = {w : [] for w in thisdict}
for k, v in syns.items():
    for synset in wordnet.synsets(k):
        for lemma in synset.lemmas():
            if not k.lower() == lemma.name(): 
                syns[k].append(lemma.name())
print(syns['Animal'])

或者如果您只想将同义词作为字符串:

print ' '.join(syns['Animal'])

【讨论】:

    【解决方案2】:

    您的代码中发生的情况是,您在添加每个元素后打印 sysn,因为您的打印语句位于所有 for 循环中,这导致每次添加元素时都会打印 sysn

    为了得到你需要的输出,打印语句应该在所有循环之外,以便打印语句在完成添加元素后打印

    thisdict = {"Animal","Metal","Car"}
    syns = {w : [] for w in thisdict}
    for k, v in syns.items():
       for synset in wordnet.synsets(k):
           for lemma in synset.lemmas():
               v.append(lemma.name())
    print(syns)
    

    【讨论】:

    • 您好,欢迎来到 SO,您能否简要说明您的答案应该如何帮助 OP?
    • @JamesWong-ReinstateMonica 确定我现在会添加解释
    猜你喜欢
    • 2014-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-06
    • 1970-01-01
    • 2019-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多