Using the API to validate words in a game

SimoneSimone Administrator admin

A common use of Oxford Dictionaries data is to validate whether a word entered into a game should be considered valid or not.

Is it spelled ‘ecstasy’ or ‘ecstacy’? Is ‘syzygy’ really a word?
The Oxford Dictionaries API offers developers the ability to solve this problem using the Entries and Lemmatron endpoints. In fact, we use it ourselves as part of our process to validate suggestions of new words!

This post shows you how we do it.

First, construct the queries to the Entries and Lemmatron endpoints, like below:

uri_entry = '' + language + '/' + word + '/lexicalCategory=suffix,noun,determiner,adverb,combining_form,idiomatic,predeterminer,particle,residual,adjective,preposition,prefix,other,verb,numeral,conjunction,pronoun,interjection,contraction'

uri_lemmatron = '' + language + '/' + word + '/lexicalCategory=suffix,noun,determiner,adverb,combining_form,idiomatic,predeterminer,particle,residual,adjective,preposition,prefix,other,verb,numeral,conjunction,pronoun,interjection,contraction'
headers = {'app_id': app_id, 'app_key': app_key}

You may notice that we use a filter here for Lexical Category (e.g., noun, verb, determiner, etc.). This is because both the Entries and Lemmatron endpoints require a filter in order to work with headwords that contain punctuation like ‘/’ (e.g., ‘n/a’). By adding the filter, but by including all the possible values, we ensure the whole database is searched.

Next, use requests to query the API:

uris = [uri_entry,uri_lemmatron]
is_in = False
for uri in uris:
r = requests.get(uri, headers=headers)
if r.status_code == 200:
is_in = True
if is_in:

If the call to the Lemmatron returns a 200 response you can be certain that the word exists in the dictionary, or is a valid inflection of a word in the dictionary. The request to the Entries endpoint works better for phrases so is used as a backup means of validation.

If both return failed searches, you can be sure the word doesn’t exist in our dictionaries. At least not yet.

Have you still got any questions about this?
Let us know in the comments!


Sign In or Register to comment.