Module:ResourcePlaceClassifier: Difference between revisions

From Trans People Together Wiki
No edit summary
No edit summary
 
Line 7: Line 7:
     local pages = mw.text.split(input, ";", true)
     local pages = mw.text.split(input, ";", true)
      
      
     if string.find(pages, "Pacific Northwest") and not string.find(pages, "United States of America") then
     if string.find(input, "Pacific Northwest") and not string.find(input, "United States of America") then
         return "Pacific Northwest"
         return "Pacific Northwest"
     end
     end

Latest revision as of 17:25, 22 May 2025

Documentation for this module may be created at Module:ResourcePlaceClassifier/doc

-- Module:PNWClassifier
local p = {}

function p.classify(frame)
    local input = frame.args[1] or ""
    -- split and trim input into individual page titles
    local pages = mw.text.split(input, ";", true)
    
    if string.find(input, "Pacific Northwest") and not string.find(input, "United States of America") then
        return "Pacific Northwest"
    end

    -- flags for global classification checks
    local hasOutside, hasOregon, hasWashington = false, false, false
    local isPortlandCandidate, isSeattleCandidate = true, true
    local sawPortlandCounty, sawSeattleCounty = false, false

    -- set of the four Portland-area counties (without “, Oregon” suffix)
    local portlandCounties = {
        ["Multnomah County"] = true,
        ["Clackamas County"] = true,
        ["Washington County"] = true,
        ["Clark County"] = true
    }
    local seattleCountyName = "King County"

    for _, raw in ipairs(pages) do
        local titleText = mw.text.trim(raw)

        -- run an SMW inline query to fetch state & county for this title
        local query = {
            "[[" .. titleText .. "]]",
            "?Has state=state",
            "?Has county=county"
        }
        local results = mw.smw.ask(query) or {}
        local state, county = nil, nil
        if #results > 0 then
            local row = results[1]
            state  = row.state
            county = row.county
        end

        -- track which states we see
        if state == "Oregon" then
            hasOregon = true
        elseif state == "Washington" then
            hasWashington = true
        else
            hasOutside = true
        end

        -- if there's no county, it's a state-only page → disqualify metro-area
        if not county or county == "" then
            isPortlandCandidate = false
            isSeattleCandidate  = false
        else
            -- strip off the “, State” to get just the county name
            local c = county:match("^(.-),")
            if portlandCounties[c] then
                sawPortlandCounty = true
            else
                isPortlandCandidate = false
            end
            if c == seattleCountyName then
                sawSeattleCounty = true
            else
                isSeattleCandidate = false
            end
        end
    end

    -- now classify in precedence order
    if hasOutside then
        return "Outside PNW"
    elseif hasOregon and hasWashington then
        return "Pacific Northwest"
    elseif isPortlandCandidate and sawPortlandCounty then
        return "Portland Area"
    elseif isSeattleCandidate and sawSeattleCounty then
        return "Seattle Area"
    elseif hasOregon and not hasWashington then
        return "Other Oregon"
    elseif hasWashington and not hasOregon then
        return "Other Washington"
    end

    -- fallback (shouldn’t happen)
    return "Unknown"
end

return p