2014-08-20 06:41:45 +00:00
|
|
|
local ReaderDictionary = require("apps/reader/modules/readerdictionary")
|
|
|
|
local Translator = require("ui/translator")
|
|
|
|
local Wikipedia = require("ui/wikipedia")
|
|
|
|
local DEBUG = require("dbg")
|
|
|
|
local _ = require("gettext")
|
|
|
|
|
|
|
|
-- Wikipedia as a special dictionary
|
2014-10-15 10:01:58 +00:00
|
|
|
local ReaderWikipedia = ReaderDictionary:extend{
|
2014-08-20 06:41:45 +00:00
|
|
|
-- identify itself
|
|
|
|
wiki = true,
|
|
|
|
no_page = _("No wiki page found."),
|
|
|
|
}
|
|
|
|
|
2014-10-15 10:01:58 +00:00
|
|
|
-- the super "class" ReaderDictionary has already registers a menu entry
|
|
|
|
-- we should override the init function in ReaderWikipedia
|
|
|
|
function ReaderWikipedia:init()
|
|
|
|
end
|
|
|
|
|
2014-08-20 06:41:45 +00:00
|
|
|
function ReaderWikipedia:onLookupWikipedia(word, box)
|
2014-08-20 10:04:19 +00:00
|
|
|
-- detect language of the text
|
2014-08-20 07:46:43 +00:00
|
|
|
local ok, lang = pcall(Translator.detect, Translator, word)
|
2015-10-03 16:16:31 +00:00
|
|
|
if not ok then return end
|
2014-08-20 06:41:45 +00:00
|
|
|
-- convert "zh-CN" and "zh-TW" to "zh"
|
|
|
|
lang = lang:match("(.*)-") or lang
|
2014-08-20 10:04:19 +00:00
|
|
|
-- strip punctuation characters around selected word
|
|
|
|
word = string.gsub(word, "^%p+", '')
|
|
|
|
word = string.gsub(word, "%p+$", '')
|
|
|
|
-- seems lower case phrase has higher hit rate
|
|
|
|
word = string.lower(word)
|
2014-08-20 06:41:45 +00:00
|
|
|
local results = {}
|
2016-02-15 09:33:48 +00:00
|
|
|
local pages
|
|
|
|
ok, pages = pcall(Wikipedia.wikintro, Wikipedia, word, lang)
|
2014-08-20 06:41:45 +00:00
|
|
|
if ok and pages then
|
|
|
|
for pageid, page in pairs(pages) do
|
|
|
|
local result = {
|
|
|
|
dict = _("Wikipedia"),
|
|
|
|
word = page.title,
|
|
|
|
definition = page.extract or self.no_page,
|
|
|
|
}
|
|
|
|
table.insert(results, result)
|
|
|
|
end
|
|
|
|
DEBUG("lookup result:", word, results)
|
2014-08-20 10:25:37 +00:00
|
|
|
self:showDict(word, results, box)
|
2014-08-20 06:41:45 +00:00
|
|
|
else
|
|
|
|
DEBUG("error:", pages)
|
|
|
|
-- dummy results
|
|
|
|
results = {
|
|
|
|
{
|
|
|
|
dict = _("Wikipedia"),
|
|
|
|
word = word,
|
|
|
|
definition = self.no_page,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DEBUG("dummy result table:", word, results)
|
2014-08-20 10:25:37 +00:00
|
|
|
self:showDict(word, results, box)
|
2014-08-20 06:41:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-28 07:58:04 +00:00
|
|
|
-- override onSaveSettings in ReaderDictionary
|
|
|
|
function ReaderWikipedia:onSaveSettings()
|
|
|
|
end
|
|
|
|
|
2014-08-20 06:41:45 +00:00
|
|
|
return ReaderWikipedia
|