| 565 | } |
| 566 | |
| 567 | function matchScriptToApp( |
| 568 | regexTags: string[], |
| 569 | tagAppMap: Record<string, string[]>, |
| 570 | ): string | string[] | null { |
| 571 | const scores: Record<string, number> = {}; |
| 572 | |
| 573 | for (const tag of regexTags) { |
| 574 | const appIds = tagAppMap[tag]; |
| 575 | if (!appIds) continue; |
| 576 | for (const appId of appIds) { |
| 577 | scores[appId] = (scores[appId] || 0) + 1; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | const entries = Object.entries(scores); |
| 582 | if (entries.length === 0) return null; |
| 583 | |
| 584 | entries.sort((a, b) => b[1] - a[1]); |
| 585 | const topScore = entries[0][1]; |
| 586 | const topApps = entries.filter(([, s]) => s === topScore).map(([id]) => id); |
| 587 | return topApps.length === 1 ? topApps[0] : topApps; |
| 588 | } |
| 589 | |
| 590 | function classifyScript(script: InternalScript, regexTags: string[]): string { |
| 591 | const replaceStr = script.replaceString || ''; |