2
0

init dict

This commit is contained in:
Cedric Hoelzl 2019-10-15 20:46:01 +02:00
parent 67d503db0a
commit f09a268808
3 changed files with 208 additions and 3 deletions

View File

@ -1,3 +0,0 @@
# telebot-wiktionary
Query Wiktionary

58
config.js Normal file
View File

@ -0,0 +1,58 @@
const fs = require('fs');
const Markup = require('telegraf/markup');
const Extra = require('telegraf/extra');
const dic = require('./wiktionary.js');
//=== MAIN MODULE CONFIG
module.exports = function(){
return {
name : "Wiktionary",
key : "wikt",
version : 0.1,
requirements : {
gps: false,
},
text : [
{
trigger : ["/define"],
public : true,
desc : {0:"Search meaning",
1:"Search signification",
},
requirements : (bot,event,message)=>{
return new Promise((resolve, reject)=>{
return resolve(200);
});
},
action : (bot,event,message)=>{
return wiki(message.text_arg).then(res=>{
let txt = `*Definitions of ${message.text_arg}:\n`;
for(let r of res.definitions){
for(let l of r.lines){
txt+= `_${l.define}_\n`;
}
txt += `\n`;
}
return event.replyWithMarkdown(txt);
}).catch(err=>{
return event.reply("No word in dictionary");
})
}
},
],
reply : [],
regex : [],
media : [],
callback : [],
inline : [],
new_member : (bot,event)=>{},
weburl : [],
cron : []
}
}

150
wiktionary.js Normal file
View File

@ -0,0 +1,150 @@
var _regenerator = require("babel-runtime/regenerator");
var _regenerator2 = _interopRequireDefault(_regenerator);
var _asyncToGenerator2 = require("babel-runtime/helpers/asyncToGenerator");
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const unirest = require("unirest");
const cheerio = require("cheerio");
const speech = ['Noun', 'Pronoun', 'Adjectives', 'Numerals', 'Verb', 'Adverb', 'Article', 'Preposition', 'Conjunction', 'Interjection', 'Abbreviation'];
var patch = function patch(re, s) {
re = eval("/" + re + "/ig");
var k = s.match(re);
if (k) {
return k.length;
} else {
return 0;
}
};
var verify_word = function verify_word(word) {
return new Promise(function (resolve, reject) {
var req = unirest("GET", "https://en.wiktionary.org/w/api.php");
req.query({
"action": "query",
"list": "search",
"format": "json",
"utf8": "",
"srprop": "",
"srsearch": word,
// "srwhat": "nearmatch"
});
req.end(function (res) {
if (res.error) reject(res.error);
var data = res.body.query.search;
if (data.length == 0) {
reject({ info: 'word does not exist' });
} else {
resolve(data[0].title);
}
});
});
};
var get_wiki_entry = function get_wiki_entry(word) {
return new Promise(function (resolve, reject) {
var req = unirest("GET", "https://en.wiktionary.org/w/index.php");
req.query({ "title": word, "printable": "yes" });
req.end(function (res) {
if (res.error) reject(res.error);
var dictionary = {
word: word,
language: 'en',
definitions: []
};
// console.log(res.body);
var $ = cheerio.load(res.body);
var cnt = 0;
$('.toc').find('.toclevel-1').each(function (i, elem) {
if (i == 0) {
var text = $(elem).text();
for (var x in speech) {
cnt += patch(speech[x], text);
}
} else {
return;
}
});
$('.mw-parser-output').find('ol').each(function (i, elem) {
if (i < cnt) {
$(elem).find('ul').empty();
var curspeech = $(elem).prev().prev().text();
var onedefinition = {
speech: curspeech,
lines: []
};
$(elem).children().each(function (i1, elem1) {
var print = $(elem1).text().split('\n');
var oneline = {
define: '',
examples: []
};
for (var x in print) {
if (x == 0) {
oneline['define'] = print[x];
} else {
if (print[x]) {
oneline['examples'].push(print[x]);
}
}
}
onedefinition['lines'].push(oneline);
});
dictionary['definitions'].push(onedefinition);
}
});
resolve(dictionary);
});
});
};
var wiki = function () {
var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(word) {
var word1, dict;
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.prev = 0;
_context.next = 3;
return verify_word(word);
case 3:
word1 = _context.sent;
_context.next = 6;
return get_wiki_entry(word1);
case 6:
dict = _context.sent;
return _context.abrupt("return", dict);
case 10:
_context.prev = 10;
_context.t0 = _context["catch"](0);
return _context.abrupt("return", Promise.reject(_context.t0));
case 13:
case "end":
return _context.stop();
}
}
}, _callee, undefined, [[0, 10]]);
}));
return function wiki(_x) {
return _ref.apply(this, arguments);
};
}();
module.exports = wiki;