UNPKG

linguee-client

Version:
93 lines 3.58 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.LingueeExtractor = void 0; const Extractor_1 = __importDefault(require("./Extractor")); const Lang_1 = __importDefault(require("../utils/Lang")); class LingueeExtractor extends Extractor_1.default { constructor(extractors) { super(extractors); } createExtratorStorage() { return { queryTerm: null, words: [], inexactWords: [], wiki: null, spelling: null, from: '', to: '', }; } run(content) { const storage = this.createExtratorStorage(); const queryTerm = content.find('#data').attr('data-query'); if (!queryTerm) { throw new Error('Query term not found'); } storage.queryTerm = queryTerm; if (content.find('h1.noresults').length) { return; } const sourceLang = content.find('#data').attr('data-sourcelang') || ''; const targetLang = content.find('#data').attr('data-targetlang') || ''; if (Lang_1.default.isLangCode(sourceLang)) { storage.from = sourceLang; } else { throw new Error('Error reading sourceLang code from response'); } if (Lang_1.default.isLangCode(targetLang)) { storage.to = targetLang; } else { throw new Error('Error reading targetLang code from response'); } const $exactContainer = content.find('.exact'); if ($exactContainer.length) { const $words = $exactContainer.children('.lemma'); for (let i = 0; i < $words.length; i++) { const word = this.extractors.word.run($words.eq(i)); storage.words.push(word); } } const $inexactContainer = content.find('.inexact'); if ($inexactContainer.length) { const $words = $inexactContainer.children('.lemma'); for (let i = 0; i < $words.length; i++) { const inexacWord = this.extractors.word.run($words.eq(i)); storage.inexactWords.push(inexacWord); } } const $wiki = content.find('#wikipedia-body'); if ($wiki.length) { storage.wiki = this.extractors.wiki.run($wiki); } const $spelling = content.find('h1.didyoumean'); if ($spelling.length) { const spellingHref = $spelling.children('a').attr('href'); if (spellingHref) { try { const spellingQuery = new URLSearchParams(spellingHref.split('?')[1]); const suggestion = { term: $spelling.find('.corrected').text() || '', queryTerm: spellingQuery.get('query') || '', querySource: spellingQuery.get('source') || '', }; if (suggestion.queryTerm && suggestion.querySource) { storage.spelling = suggestion; } } catch (error) { console.warn('Error parsing Term spelling suggestion URL'); } } } return storage; } } exports.LingueeExtractor = LingueeExtractor; exports.default = LingueeExtractor; //# sourceMappingURL=LingueeExtractor.js.map