Skip to content

Instantly share code, notes, and snippets.

@kadiks
Created August 1, 2022 18:14
Show Gist options
  • Save kadiks/edae31112bcdf5ff9ae12acf7aa1b71f to your computer and use it in GitHub Desktop.
Save kadiks/edae31112bcdf5ff9ae12acf7aa1b71f to your computer and use it in GitHub Desktop.
Get the french definition of a word
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