如何使用Python中的Oxford API提取单词的含义?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用Python中的Oxford API提取单词的含义?相关的知识,希望对你有一定的参考价值。

我正在使用Oxford api来获取Python中单词的含义。这是我的代码:

import requests
import json
app_id = 'My-App-ID'
app_key = 'My-App-Key'
language = 'en'
word_id = 'help'
url = 'https://od-api.oxforddictionaries.com:443/api/v1/entries/'+ language + '/'+ word_id.lower()
#url Normalized frequency
urlFR = 'https://od-api.oxforddictionaries.com:443/api/v1/stats/frequency/word/'  + language + '/?corpus=nmc&lemma=' + word_id.lower()
r = requests.get(url, headers = {'app_id' : app_id, 'app_key' : app_key})
#print("code {}
".format(r.status_code))
print("text 
" + r.text)
#print("json 
" + json.dumps(r.json()))

我得到的输出如下

text 
{
    "metadata": {
        "provider": "Oxford University Press"
    },
    "results": [
        {
            "id": "help",
            "language": "en",
            "lexicalEntries": [
                {
                    "entries": [
                        {
                            "etymologies": [
                                "Old English helpan (verb), help (noun), of Germanic origin; related to Dutch helpen and German helfen"
                            ],
                            "grammaticalFeatures": [
                                {
                                    "text": "Transitive",
                                    "type": "Subcategorization"
                                },
                                {
                                    "text": "Present",
                                    "type": "Tense"
                                }
                            ],
                            "homographNumber": "000",
                            "senses": [
                                {
                                    "definitions": [
                                        "make it easier or possible for (someone) to do something by offering them one's services or resources"
                                    ],
                                    "examples": [
                                        {
                                            "text": "the teenager helped out in the corner shop"
                                        },
                                        {
                                            "text": "she helped him find a buyer"
                                        },
                                        {
                                            "text": "they helped her with domestic chores"
                                        }
                                    ],
                                    "id": "m_en_gbus0460970.006",
                                    "short_definitions": [
                                        "assist someone to do something"
                                    ],
                                    "subsenses": [
                                        {
                                            "definitions": [
                                                "improve (a situation or problem); be of benefit to"
                                            ],
                                            "examples": [
                                                {
                                                    "text": "legislation to fit all new cars with catalytic converters will help"
                                                },
                                                {
                                                    "text": "upbeat comments about prospects helped confidence"
                                                }
                                            ],
                                            "id": "m_en_gbus0460970.012",
                                            "short_definitions": [
                                                "improve situation"
                                            ],
                                            "thesaurusLinks": [
                                                {
                                                    "entry_id": "help",
                                                    "sense_id": "t_en_gb0006913.003"
                                                }
                                            ]
                                        },
                                        {
                                            "definitions": [
                                                "assist (someone) to move"
                                            ],
                                            "examples": [
                                                {
                                                    "text": "I helped her up"
                                                }
                                            ],
                                            "id": "m_en_gbus0460970.013",
                                            "notes": [
                                                {
                                                    "text": "with object and adverbial of direction",
                                                    "type": "grammaticalNote"
                                                }
                                            ],
                                            "short_definitions": [
                                                "assist someone to move"
                                            ]
                                        },
                                        {
                                            "definitions": [
                                                "assist someone to put on or take off (a garment)"
                                            ],
                                            "examples": [
                                                {
                                                    "text": "she would help him off with his coat"
                                                }
                                            ],
                                            "id": "m_en_gbus0460970.014",
                                            "notes": [
                                                {
                                                    "text": ""help someone on/off with"",
                                                    "type": "wordFormNote"
                                                }
                                            ],
                                            "short_definitions": [
                                                "assist someone with garment"
                                            ]
                                        }
                                    ],
                                    "thesaurusLinks": [
                                        {
                                            "entry_id": "help",
                                            "sense_id": "t_en_gb0006913.001"
                                        }
                                    ]
                                },
                                {
                                    "definitions": [
                                        "serve someone with (food or drink)"
                                    ],
                                    "examples": [
                                        {
                                            "text": "may I help you to some more meat?"
                                        },
                                        {
                                            "text": "she helped herself to a biscuit"
                                        }
                                    ],
                                    "id": "m_en_gbus0460970.017",
                                    "notes": [
                                        {
                                            "text": ""help someone to"",
                                            "type": "wordFormNote"
                                        }
                                    ],
                                    "short_definitions": [
                                        "serve someone with food or drink"
                                    ],
                                    "subsenses": [
                                        {
                                            "definitions": [
                                                "take something without permission"
                                            ],
                                            "examples": [
                                                {
                                                    "text": "he helped himself to the wages she had brought home"
                                                }
                                            ],
                                            "id": "m_en_gbus0460970.018",
                                            "notes": [
                                                {
                                                    "text": ""help oneself"",
                                                    "type": "wordFormNote"
                                                }
                                            ],
                                            "short_definitions": [
                                                "take something without permission"
                                            ],
                                            "thesaurusLinks": [
                                                {
                                                    "entry_id": "help_oneself_to",
                                                    "sense_id": "t_en_gb0006913.006"
                                                }
                                            ]
                                        }
                                    ]
                                },
                                {
                                    "definitions": [
                                        "cannot or could not avoid"
                                    ],
                                    "examples": [
                                        {
                                            "text": "he couldn't help laughing"
                                        },
                                        {
                                            "text": "I'm sorry to put you to any inconvenience, but it can't be helped"
                                        }
                                    ],
                                    "id": "m_en_gbus0460970.020",
                                    "notes": [
                                        {
                                            "text": ""can/could not help"",
                                            "type": "wordFormNote"
                                        }
                                    ],
                                    "short_definitions": [
                                        "cannot or could not avoid"
                                    ],
                                    "subsenses": [
                                        {
                                            "definitions": [
                                                "cannot or could not stop oneself from doing something"
                                            ],
                                            "examples": [
                                                {
                                                    "text": "she couldn't help herself; she burst into tears"
                                                }
                                            ],
                                            "id": "m_en_gbus0460970.021",
                                            "notes": [
                                                {
                                                    "text": ""can/could not help oneself"",
                                                    "type": "wordFormNote"
                                                }
                                            ],
                                            "short_definitions": [
                                                "cannot or could not stop oneself"
                                            ]
                                        }
                                    ],
                                    "thesaurusLinks": [
                                        {
                                            "entry_id": "cannot_help",
                                            "sense_id": "t_en_gb0006913.005"
                                        }
                                    ]
                                }
                            ]
                        }
                    ],
                    "language": "en",
                    "lexicalCategory": "Verb",
                    "pronunciations": [
                        {
                            "audioFile": "http://audio.oxforddictionaries.com/en/mp3/help_gb_1.mp3",
                            "dialects": [
                                "British English"
                            ],
                            "phoneticNotation": "IPA",
                            "phoneticSpelling": "hɛlp"
                        }
                    ],
                    "text": "help"
                },
                {
                    "entries": [
                        {
                            "grammaticalFeatures": [
                                {
                                    "text": "Mass",
                                    "type": "Countability"
                                },
                                {
                                    "text": "Singular",
                                    "type": "Number"
                                }
                            ],
                            "homographNumber": "001",
                            "senses": [
                                {
                                    "definitions": [
                                        "the action of helping someone to do something"
                                    ],
                                    "examples": [
                                        {
                                            "text": "I asked for help from my neighbours"
                                        }
                                    ],
                                    "id": "m_en_gbus0460970.023",
                                    "short_definitions": [
                                        "action of helping"
                                    ],
                                    "subsenses": [
                                        {
                                            "definitions": [
                                                "the fact of being useful"
                                            ],
                                            "examples": [
                                                {
                                                    "text": "the skimpy manual isn't much help for beginners"
                                                }
                                            ],
                                            "id": "m_en_gbus0460970.025",
                                            "short_definitions": [
                                                "fact of being useful"
                                            ],
                                            "thesaurusLinks": [
                                                {
                                                    "entry_id": "usefulness",
                                                    "sense_id": "t_en_gb0015781.001"
                                                }
                                            ]
                                        },
                                        {
                                            "definitions": [
                                                "a person or thing that helps"
                                            ],
                                            "examples": [
                                                {
                                                    "text": "he was a great help"
                                                },
                                                {
                                                    "text": "she's been given financial help with travel"
                                                }
                                            ],
                                            "id": "m_en_gbus0460970.026",
                                            "short_definitions": [
                                                "person or thing that helps"
                                            ],
                                            "thesaurusLinks": [
                                                {
                                                    "entry_id": "backup",
                                                    "sense_id": "t_en_gb0001080.001"
                                                }
                                            ]
                                        },
                                        {
                                            "definitions": [
                                                "a domestic employee"
                                            ],
                                            "examples": [
                                                {
                                                    "text": "she has taught herself to cook since the defection of the last of the village helps"
                                                },
                                                {
                                                    "text": "the help cleaned up the leftover food and half-drunk cocktails"
                                                }
                                            ],
                                            "id": "m_en_gbus0460970.027",
                                            "notes": [
                                                {
                                                    "text": "count noun",
                                                    "type": "grammaticalNote"
                                                }
                                            ],
                                            "short_definitions": [
                                                "domestic employee"
                                            ],
                                            "thesaurusLinks": [
                                                {
                                                    "entry_id": "help",
                                                    "sense_id": "t_en_gb0006913.009"
                                                }
                                            ]
                                        },
                                        {
                                            "definitions": [
                                                "giving assistance to a computer user in the form of displayed instructions"
                                            ],
                                            "domains": [
                                                "Computing"
                                            ],
                                            "examples": [
                                                {
                                                    "text": "a help menu"
                                                }
                                            ],
                                            "id": "m_en_gbus0460970.030",
                                            "notes": [
                                                {
                                                    "text": "as modifier",
                                                    "type": "grammaticalNote"
                                                }
                                            ],
                                            "short_definitions": [
                                                "giving assistance to computer user"
                                            ]
                                        }
                                    ],
                                    "thesaurusLinks": [
                                        {
                                            "entry_id": "help",
                                            "sense_id": "t_en_gb0006913.007"
                                        }
                                    ]
                                }
                            ]
                        }
                    ],
                    "language": "en",
                    "lexicalCategory": "Noun",
                    "pronunciations": [
                        {
                            "audioFile": "http://audio.oxforddictionaries.com/en/mp3/help_gb_1.mp3",
                            "dialects": [
                                "British English"
                            ],
                            "phoneticNotation": "IPA",
                            "phoneticSpelling": "hɛlp"
                        }
                    ],
                    "text": "help"
                },
                {
                    "entries": [
                        {
                            "homographNumber": "002",
                            "senses": [
                                {
                                    "definitions": [
                                        "used as an appeal for urgent assistance"
                                    ],
                                    "examples": [
                                        {
                                            "text": "Help! I'm drowning!"
                                        }
                                    ],
                                    "id": "m_en_gbus0460970.033",
                                    "short_definitions": [
                                        "appeal for urgent assistance"
                                    ]
                                }
                            ]
                        }
                    ],
                    "language": "en",
                    "lexicalCategory": "Interjection",
                    "pronunciations": [
                        {
                            "audioFile": "http://audio.oxforddictionaries.com/en/mp3/help_gb_1.mp3",
                            "dialects": [
                                "British English"
                            ],
                            "phoneticNotation": "IPA",
                            "phoneticSpelling": "hɛlp"
                        }
                    ],
                    "text": "help"
                }
            ],
            "type": "headword",
            "word": "help"
        }
    ]
}

这里的单词id“help”,其信息显示在输出中。我想只提取定义部分。见下文:

"definitions": [
                                    "make it easier or possible for (someone) to do something by offering them one's services or resources"
                                ],

您可以看到输出中有一个定义部分。如何在Python中执行此操作?可用的定义很多,我只想要第一个定义。

答案
y = json.loads(r.text) #filename should be the file where your json is saved
y = y["results"][0]["lexicalEntries"][0]["entries"][0]["senses"][0]["definitions"]

for definition in y:
    print (definition)
    print("
")

以上是关于如何使用Python中的Oxford API提取单词的含义?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Python 从 Github API 中的所有页面获取数据?

如何在Node.js中从简单的HTTPS请求构造和提取值?

使用 Python 从 JSON API 中提取数据 [重复]

使用 Python 代码从 Foursquare API 中提取特定场地类别

Python Coinbase Pro API 如何计算限价单的执行价值?

用ffmpeg提取知乎live中的音频数据