Skip to content

Instantly share code, notes, and snippets.

@carbide-public
Created February 4, 2022 22:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save carbide-public/7e56bc76b02de874a47196d0fbeb203a to your computer and use it in GitHub Desktop.
Save carbide-public/7e56bc76b02de874a47196d0fbeb203a to your computer and use it in GitHub Desktop.
Wiktionary API
let word = "cat"
let endpoint = 'https://en.wiktionary.org/api/rest_v1/page/definition'
let filter = new RegExp('< *\\/? *[a-z]+ *( [a-z]+="[^<>"]+" *)* *\\/? *>', 'ig')
let url = `${endpoint}/${word}`
let resObj = await fetch(url, {orgin:'test'})
let res = await resObj.json()
function cleanString(str) {
return str.replaceAll(filter, '').replaceAll('&nbsp;', ' ')
}
let output = ''
for (let meaning of res.en) {
output += meaning.partOfSpeech
for (let definition of meaning.definitions) {
if (definition.definition) {
let filteredDefinition = cleanString(definition.definition)
output += `\n- ${filteredDefinition}`
if (definition.examples) {
for (let example of definition.examples) {
let filteredExample = cleanString(example)
output += `\n "${filteredExample}"`
}
}
}
}
output += '\n\n'
}
output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment