PersonaTitleNormalizer.psm1

# Persona title normaliser: turns messy job titles into a seniority level and a function, from fixed lookup tables (no guessing, no network).
$script:Abbrev = @{
    'vp'='vice president'; 'svp'='senior vice president'; 'evp'='executive vice president'; 'avp'='assistant vice president'
    'sr'='senior'; 'jr'='junior'; 'mgr'='manager'; 'dir'='director'; 'exec'='executive'; 'mktg'='marketing'; 'mkt'='marketing'
    'eng'='engineering'; 'ops'='operations'; 'biz'='business'; 'dev'='development'; 'bd'='business development'
    'sdr'='sales development representative'; 'bdr'='business development representative'; 'ae'='account executive'
    'csm'='customer success manager'; 'hr'='human resources'; 'it'='information technology'; 'mgmt'='management'
}
# Ordered: the first level with a matching phrase wins. Phrases are matched as whole words on the expanded title.
$script:Levels = @(
    @{ Level='C-level';   Phrases=@('chief','ceo','cro','cmo','cfo','cto','coo','cio','ciso','cpo','founder','co founder','cofounder','managing partner') }
    @{ Level='VP';        Phrases=@('vice president','head of','general manager') }
    @{ Level='C-level';   Phrases=@('president','owner') }
    @{ Level='Director';  Phrases=@('director','principal','group lead') }
    @{ Level='Manager';   Phrases=@('manager','team lead','lead','supervisor') }
    @{ Level='Intern';    Phrases=@('intern','trainee','apprentice') }
    @{ Level='Individual contributor'; Phrases=@('senior','junior','specialist','analyst','associate','representative','executive','engineer','developer','consultant','coordinator','administrator','assistant','designer','strategist','recruiter','accountant') }
)
$script:Functions = @(
    @{ Function='Revenue operations'; Phrases=@('revenue operations','revops','sales operations','marketing operations','gtm operations','go to market operations') }
    @{ Function='Customer success';   Phrases=@('customer success','customer support','customer experience','account management','support') }
    @{ Function='Sales';              Phrases=@('sales','business development','account executive','sdr','bdr','revenue','partnerships','channel','sales development representative','business development representative') }
    @{ Function='Marketing';          Phrases=@('marketing','growth','demand generation','demand gen','brand','content','seo','communications','events') }
    @{ Function='Security';           Phrases=@('security','ciso','infosec','cybersecurity','compliance') }
    @{ Function='Engineering';        Phrases=@('engineering','engineer','developer','software','devops','data scientist','architect','cto','technology') }
    @{ Function='IT';                 Phrases=@('information technology','systems administrator','helpdesk','cio') }
    @{ Function='Product';            Phrases=@('product','ux','design','designer') }
    @{ Function='Finance';            Phrases=@('finance','financial','accounting','accountant','controller','cfo','treasury') }
    @{ Function='People';             Phrases=@('human resources','people','talent','recruiting','recruiter','hr') }
    @{ Function='Operations';         Phrases=@('operations','coo','supply chain','logistics','procurement') }
    @{ Function='Executive';          Phrases=@('founder','co founder','cofounder','owner','ceo','president','managing partner','general manager') }
)
function Test-Phrase([string[]]$Words, [string]$Phrase) {
    $p = $Phrase.Split(' ')
    for ($i = 0; $i -le $Words.Count - $p.Count; $i++) {
        $ok = $true
        for ($j = 0; $j -lt $p.Count; $j++) { if ($Words[$i + $j] -ne $p[$j]) { $ok = $false; break } }
        if ($ok) { return $true }
    }
    return $false
}
function ConvertTo-PersonaTitle {
<#
.SYNOPSIS
Normalise a job title into a seniority level and a function.
.DESCRIPTION
Expands common abbreviations (VP, Sr, Mgr, SDR...), then looks the words up in two ordered tables: seniority (C-level, VP, Director, Manager,
Individual contributor, Intern) and function (Sales, Marketing, Revenue operations, Customer success, Security, Engineering, IT, Product,
Finance, People, Operations, Executive). The first matching row wins; no match returns "Unknown". Offline and deterministic.
.EXAMPLE
'VP, Sales & Marketing', 'Sr. Mgr Demand Gen' | ConvertTo-PersonaTitle
#>

    [CmdletBinding()]
    param([Parameter(Mandatory, ValueFromPipeline)][AllowEmptyString()][string]$Title)
    process {
        $clean = New-Object System.Text.StringBuilder
        foreach ($ch in $Title.ToLowerInvariant().ToCharArray()) { if ([char]::IsLetterOrDigit($ch)) { [void]$clean.Append($ch) } else { [void]$clean.Append(' ') } }
        $words = New-Object System.Collections.Generic.List[string]
        foreach ($w in ($clean.ToString().Split(' ', [StringSplitOptions]::RemoveEmptyEntries))) {
            if ($script:Abbrev.ContainsKey($w)) { foreach ($x in $script:Abbrev[$w].Split(' ')) { $words.Add($x) } } else { $words.Add($w) }
        }
        $arr = $words.ToArray()
        $level = 'Unknown'
        :lv foreach ($row in $script:Levels) { foreach ($p in $row.Phrases) { if (Test-Phrase $arr $p) { $level = $row.Level; break lv } } }
        $fn = 'Unknown'
        :fx foreach ($row in $script:Functions) { foreach ($p in $row.Phrases) { if (Test-Phrase $arr $p) { $fn = $row.Function; break fx } } }
        [pscustomobject]@{ Title = $Title; Normalized = ($arr -join ' '); Seniority = $level; Function = $fn }
    }
}
Export-ModuleMember -Function ConvertTo-PersonaTitle