feat(client): 全面增强站名模糊匹配逻辑,支持中英文混排关键词提取

1. 新增 `Utils.getKeywords(text)`:自动提取所有关键信息:
   - 包含中划线的字母数字组合(如 CE-03)
   - 连续的字母数字(如 CRH01)
   - 中文字符串,并在提取时自动过滤掉“站台”、“轨道”、“号”、“线”等噪音词。
2. 将 `Utils.getKeywords` 的匹配逻辑集成至 `Utils.isFuzzyMatch` 的最终校验中,即使在 `Utils.getBaseName` 无法精确比较的情况下,也能通过核心关键字(特别是纯中文关键字)判断两者是否为同一站台。
3. 将列车在站判定(`isAtStation`)对未知列车的目的站比较逻辑统一切换为使用强化的 `Utils.isFuzzyMatch`,保证时刻表、列车站名、NBT、显示器等来源的中英文站名信息均能互相精准匹配。
This commit is contained in:
2026-08-04 07:42:57 +08:00
parent 6398146ee6
commit c86fca8449
+40 -6
View File
@@ -676,6 +676,33 @@ function Utils.getBaseName(name)
return name:match("^%s*(.-)%s*$") return name:match("^%s*(.-)%s*$")
end end
function Utils.getKeywords(text)
if not text then return {} end
local s = tostring(text):lower()
local keywords = {}
for code in s:gmatch("[%a%d]+%-[%a%d]+") do
keywords[code] = true
end
for code in s:gmatch("%a+%d+") do
keywords[code] = true
end
for zh in s:gmatch("[\128-\255]+") do
local cleanZh = zh:gsub("站台", ""):gsub("轨道", ""):gsub("号", ""):gsub("线", "")
if cleanZh and cleanZh ~= "" then
keywords[cleanZh] = true
end
end
local list = {}
for k, _ in pairs(keywords) do
table.insert(list, k)
end
return list
end
function Utils.isFuzzyMatch(str, pattern) function Utils.isFuzzyMatch(str, pattern)
if not str or not pattern then return false end if not str or not pattern then return false end
str = tostring(str):lower():match("^%s*(.-)%s*$") str = tostring(str):lower():match("^%s*(.-)%s*$")
@@ -710,6 +737,17 @@ function Utils.isFuzzyMatch(str, pattern)
return true return true
end end
-- 4. Extracted Keywords comparison (especially for Chinese + Code mixes)
local k1 = Utils.getKeywords(str)
local k2 = Utils.getKeywords(pattern)
for _, w1 in ipairs(k1) do
for _, w2 in ipairs(k2) do
if w1 == w2 or w1:find(w2, 1, true) or w2:find(w1, 1, true) then
return true
end
end
end
return false return false
end end
@@ -2729,9 +2767,7 @@ function Main.loop()
if State.currentTrain then if State.currentTrain then
if State.currentTrain.id == trainName then if State.currentTrain.id == trainName then
if trainName == "???" or trainName == "Unknown" or trainName == "" then if trainName == "???" or trainName == "Unknown" or trainName == "" then
local currentDest = Utils.getBaseName(State.currentTrain.dest) if Utils.isFuzzyMatch(State.currentTrain.dest, destination) then
local nbtDest = Utils.getBaseName(destination)
if currentDest == nbtDest or currentDest:find(nbtDest, 1, true) or nbtDest:find(currentDest, 1, true) then
isAtStation = true isAtStation = true
end end
else else
@@ -2880,9 +2916,7 @@ function Main.loop()
if State.currentTrain then if State.currentTrain then
if State.currentTrain.id == tName then if State.currentTrain.id == tName then
if tName == "???" or tName == "Unknown" or tName == "" then if tName == "???" or tName == "Unknown" or tName == "" then
local currentDest = Utils.getBaseName(State.currentTrain.dest) if Utils.isFuzzyMatch(State.currentTrain.dest, dest) then
local nbtDest = Utils.getBaseName(dest)
if currentDest == nbtDest or currentDest:find(nbtDest, 1, true) or nbtDest:find(currentDest, 1, true) then
isAtStation = true isAtStation = true
end end
else else