This Gist was automatically created by Carbide, a free online programming environment.
Created
February 4, 2022 22:54
-
-
Save carbide-public/7e56bc76b02de874a47196d0fbeb203a to your computer and use it in GitHub Desktop.
Wiktionary API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(' ', ' ') | |
} | |
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