You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在Python中查询英语词典及对接Oxford API获取词源?

Hey there! Let's tackle your questions about building a Python etymology tool— I've messed around with similar projects before, so here's a breakdown of what works:

1. Connecting Oxford API to Your Python Program

First, you'll need to sign up for an account on the Oxford Dictionaries Developer Portal to get your app_id and app_key (they offer free tiers for non-commercial use). Once you have those, the requests library is your best friend for making API calls. Here's a working example:

First install requests if you haven't:

pip install requests

Then the code:

import requests

def fetch_oxford_etymology(word, app_id, app_key):
    base_url = "https://od-api.oxforddictionaries.com/api/v2/entries/en-us/"
    # Target the etymologies endpoint specifically
    endpoint = f"{word.lower()}/etymologies"
    
    # Required headers for authentication
    headers = {
        "app_id": app_id,
        "app_key": app_key
    }
    
    response = requests.get(base_url + endpoint, headers=headers)
    
    if response.status_code == 200:
        data = response.json()
        # Extract etymology entries from the nested JSON structure
        etymology_texts = []
        for result in data.get("results", []):
            for lex_entry in result.get("lexicalEntries", []):
                etymology_texts.extend(lex_entry.get("etymologies", []))
        return "\n".join(etymology_texts)
    else:
        return f"Failed to retrieve data: {response.status_code} - {response.text}"

# Example usage (replace with your actual credentials)
my_app_id = "YOUR_APP_ID"
my_app_key = "YOUR_APP_KEY"
print(fetch_oxford_etymology("serendipity", my_app_id, my_app_key))

The key here is targeting the /etymologies endpoint to get direct access to word origin data, instead of pulling full dictionary entries.

2. Practical Ways to Query English Dictionaries in Python

You've got a few solid options depending on your needs (local vs. online, simplicity vs. depth):

  • NLTK's WordNet (Local, No API Keys)
    Great for quick, offline lookups of definitions and synonyms (though etymology is limited). First set up NLTK:
pip install nltk

Then download WordNet via NLTK:

import nltk
nltk.download('wordnet')

Usage example:

from nltk.corpus import wordnet

def get_wordnet_details(word):
    synsets = wordnet.synsets(word)
    if synsets:
        return {
            "definition": synsets[0].definition(),
            "synonyms": [lemma.name() for lemma in synsets[0].lemmas()]
        }
    return "No matches found in WordNet."

print(get_wordnet_details("perspicacious"))
  • PyDictionary (Quick Prototyping)
    A lightweight library that wraps multiple online dictionaries. Install it with:
pip install PyDictionary

Usage:

from PyDictionary import PyDictionary

dict_tool = PyDictionary()
# Get word meanings
print(dict_tool.meaning("ephemeral"))
# Get etymology (when available)
print(dict_tool.etymology("ephemeral"))

Note: It can be inconsistent at times since it relies on third-party sources, but it's perfect for testing ideas fast.

  • Other Free APIs
    Merriam-Webster and Cambridge also offer free API tiers (similar to Oxford) that you can call with requests— just sign up for their developer accounts to get access keys.
3. Fixing WordNet's Coverage Gaps

WordNet is awesome but far from complete, especially for rare or niche words. Here are ways to fill the gaps:

  • Combine Local + Online Sources
    Build a fallback system: first check WordNet, and if no results come up, call an API like Oxford or Merriam-Webster. Example logic:
def get_complete_word_info(word, app_id, app_key):
    wordnet_result = get_wordnet_details(word)
    if isinstance(wordnet_result, dict):
        return f"WordNet Definition: {wordnet_result['definition']}"
    else:
        oxford_result = fetch_oxford_etymology(word, app_id, app_key)
        return oxford_result if oxford_result else "No data found across all sources."
  • Use Open Multilingual WordNet (OMW)
    OMW extends the original WordNet with more words and multilingual support. Install it via:
pip install omw

Then use it with NLTK:

from nltk.corpus import wordnet, omw

# Look up rare words (OMW often has entries WordNet misses)
synsets = wordnet.synsets("quintessential", lang="eng")
if synsets:
    print(synsets[0].definition())
  • Scrape Reliable Dictionary Sites (With Caution)
    If API limits are an issue, some reputable dictionary sites have public etymology pages you can scrape with BeautifulSoup. Just make sure to check their robots.txt and terms of service first to avoid legal issues. Example skeleton code:
from bs4 import BeautifulSoup
import requests

def scrape_etymology(word):
    # Replace with a real, allowed-to-scrape dictionary URL
    url = f"https://example-dictionary.com/etymology/{word}"
    response = requests.get(url)
    if response.status_code == 200:
        soup = BeautifulSoup(response.text, "html.parser")
        etymology_section = soup.find("div", class_="etymology-content")
        if etymology_section:
            return etymology_section.get_text(strip=True)
    return "No etymology found via scraping."

内容的提问来源于stack exchange,提问作者Núria Bosch

火山引擎 最新活动