Created
August 1, 2022 18:14
-
-
Save kadiks/edae31112bcdf5ff9ae12acf7aa1b71f to your computer and use it in GitHub Desktop.
Get the french definition of a word
This file contains hidden or 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
import {load} from 'cheerio'; | |
const getDefinition = async (word) => { | |
const url = `https://fr.wiktionary.org/wiki/${word}`; | |
// fetch with Node 18+ (or use Axios) | |
const response = await fetch(url); | |
const html = await response.text(); | |
const $ = load(html); | |
// Get the first definition of the word | |
const contentSelector = '#mw-content-text ol li'; | |
// Usage of the word in a sentence are either in <UL /> ou <OL /> | |
// So I remove them | |
$(`${contentSelector} ul`).remove(); | |
$(`${contentSelector} ol`).remove(); | |
const content = $(contentSelector).eq(0).text(); | |
return content; | |
}; | |
export default getDefinition; | |
// Example: not needed for the module to work | |
// const start = async () => { | |
// const definition = await getDefinition('informatique'); | |
// console.log({definition}); | |
// // { definition : "Science du traitement automatique de l’information.\n" } | |
// }; | |
// start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment