diff --git a/station_broadcast.lua b/station_broadcast.lua index f30caf4..8b725ea 100644 --- a/station_broadcast.lua +++ b/station_broadcast.lua @@ -676,6 +676,33 @@ function Utils.getBaseName(name) return name:match("^%s*(.-)%s*$") 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) if not str or not pattern then return false end str = tostring(str):lower():match("^%s*(.-)%s*$") @@ -710,6 +737,17 @@ function Utils.isFuzzyMatch(str, pattern) return true 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 end @@ -2729,9 +2767,7 @@ function Main.loop() if State.currentTrain then if State.currentTrain.id == trainName then if trainName == "???" or trainName == "Unknown" or trainName == "" then - local currentDest = Utils.getBaseName(State.currentTrain.dest) - local nbtDest = Utils.getBaseName(destination) - if currentDest == nbtDest or currentDest:find(nbtDest, 1, true) or nbtDest:find(currentDest, 1, true) then + if Utils.isFuzzyMatch(State.currentTrain.dest, destination) then isAtStation = true end else @@ -2880,9 +2916,7 @@ function Main.loop() if State.currentTrain then if State.currentTrain.id == tName then if tName == "???" or tName == "Unknown" or tName == "" then - local currentDest = Utils.getBaseName(State.currentTrain.dest) - local nbtDest = Utils.getBaseName(dest) - if currentDest == nbtDest or currentDest:find(nbtDest, 1, true) or nbtDest:find(currentDest, 1, true) then + if Utils.isFuzzyMatch(State.currentTrain.dest, dest) then isAtStation = true end else