<commit_before>require "ui/device"
require "ui/widget/dict"

ReaderDictionary = EventListener:new{}

function ReaderDictionary:init()
	JSON = require("JSON")
end

function ReaderDictionary:onLookupWord(word)
	self:stardictLookup(word)
end

function ReaderDictionary:stardictLookup(word)
	DEBUG("lookup word:", word)
	if word then
		-- strip punctuation characters around selected word
		word = string.gsub(word, "^%p+", '')
		word = string.gsub(word, "%p+$", '')
		DEBUG("stripped word:", word)
		-- escape quotes and other funny characters in word
		local std_out = io.popen("./sdcv --utf8-input --utf8-output -nj "..("%q"):format(word), "r")
		local results_str = std_out:read("*all")
		if results_str then
			--DEBUG("result str:", word, results_str)
			local ok, results = pcall(JSON.decode, JSON, results_str)
			--DEBUG("lookup result table:", word, results)
			self:showDict(results)
		end
	end
end

function ReaderDictionary:showDict(results)
	if results and results[1] then
		DEBUG("showing quick lookup dictionary window")
		UIManager:show(DictQuickLookup:new{
			ui = self.ui,
			dialog = self.dialog,
			results = results,
			dictionary = self.default_dictionary,
			width = Screen:getWidth() - scaleByDPI(120),
			height = Screen:getHeight()*0.43,
		})
	end
end

function ReaderDictionary:onUpdateDefaultDict(dict)
	DEBUG("make default dictionary:", dict)
	self.default_dictionary = dict<commit_msg>check json output before decoding This should fix #332.<commit_after>require "ui/device"
require "ui/widget/dict"

ReaderDictionary = EventListener:new{}

function ReaderDictionary:init()
	JSON = require("JSON")
end

function ReaderDictionary:onLookupWord(word)
	self:stardictLookup(word)
end

function ReaderDictionary:stardictLookup(word)
	DEBUG("lookup word:", word)
	if word then
		-- strip punctuation characters around selected word
		word = string.gsub(word, "^%p+", '')
		word = string.gsub(word, "%p+$", '')
		DEBUG("stripped word:", word)
		-- escape quotes and other funny characters in word
		local std_out = io.popen("./sdcv --utf8-input --utf8-output -nj "..("%q"):format(word), "r")
		local results_str = nil
		if std_out then results_str = std_out:read("*all") end
		if results_str then
			--DEBUG("result str:", word, results_str)
			local ok, results = pcall(JSON.decode, JSON, results_str)
			--DEBUG("lookup result table:", word, results)
			self:showDict(results)
		end
	end
end

function ReaderDictionary:showDict(results)
	if results and results[1] then
		DEBUG("showing quick lookup dictionary window")
		UIManager:show(DictQuickLookup:new{
			ui = self.ui,
			dialog = self.dialog,
			results = results,
			dictionary = self.default_dictionary,
			width = Screen:getWidth() - scaleByDPI(120),
			height = Screen:getHeight()*0.43,
		})
	end
end

function ReaderDictionary:onUpdateDefaultDict(dict)
	DEBUG("make default dictionary:", dict)
	self.default_dictionary = dict
{'fraction_non_alphanumeric': 0.13041982105987612, 'fraction_numerical': 0.0072264280798348245, 'mean_word_length': 4.9447852760736195, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7543802', 'n_tokens_mistral': 1001, 'n_tokens_neox': 910, 'n_words': 243}