Module:Lorebook/Debug: Difference between revisions
上传带详细调试输出的 Lorebook 模块 |
Fix context trigger logic and SMW query - same fixes as main module |
||
Line 22: | Line 22: | ||
local function containsAny(haystack, needles) | local function containsAny(haystack, needles) | ||
local s = | local s = haystack:lower() | ||
for _, n in ipairs(needles) do | for _, n in ipairs(needles) do | ||
if n ~= '' then | if n ~= '' then | ||
local escaped = | local needle = n:lower() | ||
-- Escape special pattern characters | |||
local escaped = needle:gsub('[%^%$%(%)%%%.%[%]%*%+%-%?]', '%%%1') | |||
-- Try multiple matching strategies | |||
-- 1. Exact match | |||
if s == needle then | |||
return true | |||
end | |||
-- 2. Word boundary patterns | |||
local patterns = { | local patterns = { | ||
' | '^' .. escaped .. '%W', -- At start followed by non-word | ||
' | '%W' .. escaped .. '%W', -- Surrounded by non-word chars | ||
'%W' .. escaped .. '$', -- Preceded by non-word at end | |||
' | '^' .. escaped .. '$' -- Exact match (redundant but safe) | ||
'^' .. escaped .. '$' | |||
} | } | ||
for _, pattern in ipairs(patterns) do | for _, pattern in ipairs(patterns) do | ||
if s:find(pattern) then | if s:find(pattern) then | ||
return true | return true | ||
end | end | ||
end | |||
-- 3. Simple substring match as last resort | |||
if s:find(escaped, 1, true) then | |||
return true | |||
end | end | ||
end | end | ||
Line 71: | Line 86: | ||
local query = string.format('[[Category:Concept]][[Belongs to world::%s]]', world) | local query = string.format('[[Category:Concept]][[Belongs to world::%s]]', world) | ||
local res = mw.smw.ask(query, { | local res = mw.smw.ask(query, { | ||
['?Plist'] = 'plist', | ['?Plist'] = 'plist', | ||
['?AliChat'] = 'alichat', | ['?AliChat'] = 'alichat', | ||
Line 89: | Line 103: | ||
for i, row in ipairs(res) do | for i, row in ipairs(res) do | ||
normalized[i] = { | normalized[i] = { | ||
page = normalizeValue(row[1]), | page = normalizeValue(row[1]), -- First element is the page title when mainlabel is not '-' | ||
plist = normalizeValue(row.plist), | plist = normalizeValue(row.plist), | ||
alichat = normalizeValue(row.alichat), | alichat = normalizeValue(row.alichat), |