Merge pull request #89 from chrox/add_stardict

add a dictionary backend stardict
pull/90/head
{Qingping,Dave} Hou 12 years ago
commit 9b61cfe086

1
.gitignore vendored

@ -9,6 +9,7 @@ lua-*
git-rev
*.o
tags
test/*
emu

@ -25,10 +25,10 @@ XGETTEXT_BIN=$(KOREADER_MISC_TOOL)/gettext/lua_xgettext.py
MO_DIR=i18n
all: $(KOR_BASE)/koreader-base $(KOR_BASE)/extr mo
all: $(KOR_BASE)/koreader-base $(KOR_BASE)/extr $(KOR_BASE)/sdcv mo
$(KOR_BASE)/koreader-base $(KOR_BASE)/extr:
make -C $(KOR_BASE) koreader-base extr
$(KOR_BASE)/koreader-base $(KOR_BASE)/extr $(KOR_BASE)/sdcv:
make -C $(KOR_BASE) koreader-base extr sdcv
fetchthirdparty:
git submodule init
@ -50,6 +50,7 @@ bootstrapemu:
test -d $(EMU_DIR)/resources || (cd $(EMU_DIR) && ln -s ../resources ./)
test -e $(EMU_DIR)/koreader-base || (cd $(EMU_DIR) && ln -s ../$(KOR_BASE)/koreader-base ./)
test -e $(EMU_DIR)/extr || (cd $(EMU_DIR) && ln -s ../$(KOR_BASE)/extr ./)
test -e $(EMU_DIR)/sdcv || (cd $(EMU_DIR) && ln -s ../$(KOR_BASE)/sdcv ./)
test -e $(EMU_DIR)/reader.lua || (cd $(EMU_DIR) && ln -s ../reader.lua ./)
test -e $(EMU_DIR)/history || (mkdir $(EMU_DIR)/history)
test -e $(EMU_DIR)/$(MO_DIR) || (cd $(EMU_DIR) && ln -s ../$(MO_DIR) ./)
@ -63,8 +64,8 @@ customupdate: all
rm -rf $(INSTALL_DIR)
# create new dir for package
mkdir -p $(INSTALL_DIR)/{history,screenshots,clipboard,libs}
cp -p README.md COPYING $(KOR_BASE)/{koreader-base,extr} koreader.sh $(LUA_FILES) $(INSTALL_DIR)
$(STRIP) --strip-unneeded $(INSTALL_DIR)/koreader-base $(INSTALL_DIR)/extr
cp -p README.md COPYING $(KOR_BASE)/{koreader-base,extr,sdcv} koreader.sh $(LUA_FILES) $(INSTALL_DIR)
$(STRIP) --strip-unneeded $(INSTALL_DIR)/koreader-base $(INSTALL_DIR)/extr $(INSTALL_DIR)/sdcv
mkdir $(INSTALL_DIR)/data
cp -L koreader-base/$(DJVULIB) $(KOR_BASE)/$(CRELIB) \
$(KOR_BASE)/$(LUALIB) $(KOR_BASE)/$(K2PDFOPTLIB) \

@ -20,7 +20,7 @@ A global LRU cache
]]--
Cache = {
-- cache configuration:
max_memsize = 1024*1024*15, -- 15MB cache size
max_memsize = 1024*1024*10, -- 10MB cache size
-- cache state:
current_memsize = 0,
-- associative cache

@ -7,7 +7,7 @@ require "ui/reader/readerconfig"
KoptInterface = {
tessocr_data = "data",
ocr_lang = "eng",
ocr_type = 0, -- default, for more accuracy use 3
ocr_type = 3, -- default 0, for more accuracy use 3
}
ContextCacheItem = CacheItem:new{}
@ -19,6 +19,15 @@ function ContextCacheItem:onFree()
end
end
OCREngine = CacheItem:new{}
function OCREngine:onFree()
if self.ocrengine.freeOCR then
DEBUG("free OCREngine", self.ocrengine)
self.ocrengine:freeOCR()
end
end
function KoptInterface:waitForContext(kc)
-- if koptcontext is being processed in background thread
-- the isPreCache will return 1.
@ -107,6 +116,11 @@ function KoptInterface:getPageText(doc, pageno)
end
function KoptInterface:getOCRWord(doc, pageno, rect)
local ocrengine = "ocrengine"
if not Cache:check(ocrengine) then
local dummy = KOPTContext.new()
Cache:insert(ocrengine, OCREngine:new{ ocrengine = dummy })
end
local bbox = doc:getPageBBox(pageno)
local context_hash = self:getContextHash(doc, pageno, bbox)
local hash = "ocrword|"..context_hash..rect.x..rect.y..rect.w..rect.h
@ -117,14 +131,10 @@ function KoptInterface:getOCRWord(doc, pageno, rect)
if cached then
local kc = self:waitForContext(cached.kctx)
local fullwidth, fullheight = kc:getPageDim()
--os.execute("echo 3 > /proc/sys/vm/drop_caches")
local ok, word = pcall(
kc.getOCRWord, kc,
self.tessocr_data,
self.ocr_lang,
self.ocr_type,
rect.x, rect.y,
rect.w, rect.h)
kc.getTOCRWord, kc,
rect.x, rect.y, rect.w, rect.h,
self.tessocr_data, self.ocr_lang, self.ocr_type, 0, 1)
Cache:insert(hash, CacheItem:new{ ocrword = word })
return word
end

@ -7,14 +7,15 @@ Device = {
}
function Device:getModel()
if self.model then return self.model end
local std_out = io.popen("grep 'MX' /proc/cpuinfo | cut -d':' -f2 | awk {'print $2'}", "r")
local cpu_mod = std_out:read()
if not cpu_mod then
local ret = os.execute("grep 'Hardware : Mario Platform' /proc/cpuinfo", "r")
if ret ~= 0 then
return nil
self.model = nil
else
return "KindleDXG"
self.model = "KindleDXG"
end
end
if cpu_mod == "MX50" then
@ -25,20 +26,21 @@ function Device:getModel()
-- another special file for KT is Neonode zForce touchscreen:
-- /sys/devices/platform/zforce.0/
if pw_test_fd then
return "KindlePaperWhite"
self.model = "KindlePaperWhite"
elseif kt_test_fd then
return "KindleTouch"
self.model = "KindleTouch"
else
return "Kindle4"
self.model = "Kindle4"
end
elseif cpu_mod == "MX35" then
-- check if we are running on Kindle 3 (additional volume input)
return "Kindle3"
self.model = "Kindle3"
elseif cpu_mod == "MX3" then
return "Kindle2"
self.model = "Kindle2"
else
return nil
self.model = nil
end
return self.model
end
function Device:isKindle4()

@ -7,33 +7,52 @@ function ReaderDictionary:init()
local dev_mod = Device:getModel()
if dev_mod == "KindlePaperWhite" or dev_mod == "KindleTouch" then
require("liblipclua")
JSON = require("JSON")
DEBUG("init lipc handler com.github.koreader.dictionary")
self.lipc_handle = lipc.init("com.github.koreader.dictionary")
end
JSON = require("JSON")
end
function ReaderDictionary:onLookupWord(word)
self:stardictLookup(word)
end
function ReaderDictionary:nativeDictLookup(word)
DEBUG("lookup word:", word)
--self:quickLookup()
if self.lipc_handle and JSON and word then
if self.lipc_handle and word then
self.lipc_handle:set_string_property(
"com.github.koreader.kpvbooklet.dict", "lookup", word)
local results_str = self.lipc_handle:get_string_property(
"com.github.koreader.kpvbooklet.word", word)
if results_str then
--DEBUG("def str:", word, definitions)
--DEBUG("result str:", word, results_str)
local ok, results_tab = pcall(JSON.decode, JSON, results_str)
--DEBUG("lookup result table:", word, results_tab)
if results_tab and results_tab[1] then
self:showDict(results_tab[1])
end
end
end
end
function ReaderDictionary:stardictLookup(word)
DEBUG("lookup word:", word)
if word then
local std_out = io.popen("./sdcv -nj "..'\"'..word..'\"', "r")
local results_str = std_out:read("*all")
if results_str then
--DEBUG("result str:", word, results_str)
local ok, results_tab = pcall(JSON.decode, JSON, results_str)
DEBUG("lookup result table:", word, results_tab)
if results_tab[1] then
self:quickLookup(results_tab[1])
--DEBUG("lookup result table:", word, results_tab)
if results_tab and results_tab[1] then
self:showDict(results_tab[1])
end
end
end
return true
end
function ReaderDictionary:quickLookup(result)
function ReaderDictionary:showDict(result)
-- UIManager:show(DictQuickLookup:new{
-- dict = "Oxford Dictionary of English",
-- definition = "coordination n. [mass noun] 1 the organization of the different elements of a \

@ -68,6 +68,10 @@ function ReaderHighlight:onHold(arg, ges)
local screen_rect = self.view:pageToScreenTransform(self.pos.page, self.word_info.box)
DEBUG("highlight word rect", screen_rect)
if screen_rect then
screen_rect.x = screen_rect.x - screen_rect.h * 0.2
screen_rect.y = screen_rect.y - screen_rect.h * 0.2
screen_rect.w = screen_rect.w + screen_rect.h * 0.4
screen_rect.h = screen_rect.h + screen_rect.h * 0.4
self.view.highlight.rect = screen_rect
UIManager:setDirty(self.dialog, "partial")
-- if we extracted text directly
@ -77,11 +81,11 @@ function ReaderHighlight:onHold(arg, ges)
else
UIManager:scheduleIn(0.1, function()
local word_box = self.word_info.box
word_box.x = word_box.x - math.floor(word_box.h * 0.1)
word_box.y = word_box.y - math.floor(word_box.h * 0.1)
word_box.w = word_box.w + math.floor(word_box.h * 0.2)
word_box.h = word_box.h + math.floor(word_box.h * 0.2)
--local word = self.ui.document:getOCRWord(self.pos.page, word_box)
word_box.x = word_box.x - math.floor(word_box.h * 0.2)
word_box.y = word_box.y - math.floor(word_box.h * 0.4)
word_box.w = word_box.w + math.floor(word_box.h * 0.4)
word_box.h = word_box.h + math.floor(word_box.h * 0.6)
local word = self.ui.document:getOCRWord(self.pos.page, word_box)
DEBUG("OCRed word:", word)
self.ui:handleEvent(Event:new("LookupWord", word))
end)

@ -289,12 +289,7 @@ function ReaderView:drawScrollView(bb, x, y)
end
function ReaderView:drawHightlight(bb, x, y, rect)
-- slightly enlarge the highlight box
-- for better viewing experience
local x = rect.x - rect.h * 0.01
local y = rect.y - rect.h * 0.01
local w = rect.w + rect.h * 0.02
local h = rect.h + rect.h * 0.02
local x, y, w, h = rect.x, rect.y, rect.w, rect.h
self.highlight.drawer = self.highlight.drawer or "underscore"
if self.highlight.drawer == "underscore" then

@ -11,7 +11,6 @@ DictQuickLookup = InputContainer:new{
title_face = Font:getFace("tfont", 20),
content_face = Font:getFace("cfont", 18),
width = Screen:getWidth() - 100
}
function DictQuickLookup:init()
@ -44,13 +43,13 @@ function DictQuickLookup:init()
TextBoxWidget:new{
text = self.dict,
face = self.title_face,
width = self.width,
width = Screen:getWidth() - 100,
},
VerticalSpan:new{ width = 20 },
TextBoxWidget:new{
text = self.definition,
face = self.content_face,
width = self.width,
width = Screen:getWidth() - 100,
}
}
}

@ -12,6 +12,9 @@ cd /mnt/us/koreader/
# export trained OCR data directory
export TESSDATA_PREFIX="data"
# export dict directory
export STARDICT_DATA_DIR="data/dict"
# bind-mount system fonts
if ! grep /mnt/us/koreader/fonts/host /proc/mounts; then
mount -o bind /usr/java/lib/fonts /mnt/us/koreader/fonts/host

Loading…
Cancel
Save