strip punctuations in word before wikipedia lookup

This commit is contained in:
chrox
2014-08-20 18:04:19 +08:00
parent 50d45a13d1
commit a275a8cc89
3 changed files with 6 additions and 2 deletions

View File

@@ -18,6 +18,7 @@ local ReaderWikipedia = ReaderDictionary:new{
}
function ReaderWikipedia:onLookupWikipedia(word, box)
-- detect language of the text
local ok, lang = pcall(Translator.detect, Translator, word)
-- prompt users to turn on Wifi if network is unreachable
if not ok and lang and lang:find("Network is unreachable") then
@@ -26,6 +27,11 @@ function ReaderWikipedia:onLookupWikipedia(word, box)
end
-- convert "zh-CN" and "zh-TW" to "zh"
lang = lang:match("(.*)-") or lang
-- 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)
local results = {}
local ok, pages = pcall(Wikipedia.wikintro, Wikipedia, word, lang)
if ok and pages then