Improve moderation controls and term management
This commit is contained in:
@@ -3,6 +3,7 @@ import {
|
||||
assessCommunityModeration,
|
||||
findLocalModerationMatches,
|
||||
normalizeModerationTerm,
|
||||
parseModerationTermBatch,
|
||||
type CommunityModerationPolicy
|
||||
} from './community-moderation';
|
||||
|
||||
@@ -34,6 +35,27 @@ describe('community moderation policy', () => {
|
||||
expect(normalizeModerationTerm(' Rogue\u00a0Agent ')).toBe('rogue agent');
|
||||
});
|
||||
|
||||
it('parses pasted terms with automatic word and phrase matching', () => {
|
||||
expect(parseModerationTermBatch('weapon\nkill, machine gun; assault rifle')).toEqual({
|
||||
terms: [
|
||||
{ value: 'weapon', normalizedValue: 'weapon', matchMode: 'word' },
|
||||
{ value: 'kill', normalizedValue: 'kill', matchMode: 'word' },
|
||||
{ value: 'machine gun', normalizedValue: 'machine gun', matchMode: 'phrase' },
|
||||
{ value: 'assault rifle', normalizedValue: 'assault rifle', matchMode: 'phrase' }
|
||||
],
|
||||
duplicateCount: 0
|
||||
});
|
||||
});
|
||||
|
||||
it('deduplicates pasted terms after compatibility and case normalization', () => {
|
||||
const parsed = parseModerationTermBatch('Weapon\nweapon\nMACHINE GUN\nmachine gun');
|
||||
expect(parsed.terms).toEqual([
|
||||
{ value: 'Weapon', normalizedValue: 'weapon', matchMode: 'word' },
|
||||
{ value: 'MACHINE GUN', normalizedValue: 'machine gun', matchMode: 'phrase' }
|
||||
]);
|
||||
expect(parsed.duplicateCount).toBe(2);
|
||||
});
|
||||
|
||||
it('allows an exact local exception to suppress a matching banned term', () => {
|
||||
const matches = findLocalModerationMatches('Rogue Agent tactics', 'comment', policy({
|
||||
detectPii: false,
|
||||
|
||||
@@ -107,6 +107,37 @@ export function normalizeModerationTerm(value: string) {
|
||||
return value.normalize('NFKC').trim().replace(/\s+/g, ' ').toLocaleLowerCase();
|
||||
}
|
||||
|
||||
export type ParsedModerationTermBatch = {
|
||||
terms: Array<{
|
||||
value: string;
|
||||
normalizedValue: string;
|
||||
matchMode: 'word' | 'phrase';
|
||||
}>;
|
||||
duplicateCount: number;
|
||||
};
|
||||
|
||||
export function parseModerationTermBatch(value: string): ParsedModerationTermBatch {
|
||||
const entries = value.normalize('NFKC')
|
||||
.split(/[\r\n,;]+/u)
|
||||
.map((entry) => entry.trim().replace(/\s+/g, ' '))
|
||||
.filter(Boolean);
|
||||
const terms: ParsedModerationTermBatch['terms'] = [];
|
||||
const seen = new Set<string>();
|
||||
let duplicateCount = 0;
|
||||
for (const entry of entries) {
|
||||
const normalizedValue = normalizeModerationTerm(entry);
|
||||
const matchMode = /\s/u.test(normalizedValue) ? 'phrase' : 'word';
|
||||
const key = `${matchMode}:${normalizedValue}`;
|
||||
if (seen.has(key)) {
|
||||
duplicateCount += 1;
|
||||
continue;
|
||||
}
|
||||
seen.add(key);
|
||||
terms.push({ value: entry, normalizedValue, matchMode });
|
||||
}
|
||||
return { terms, duplicateCount };
|
||||
}
|
||||
|
||||
export function hashModeratedContent(value: string) {
|
||||
return createHash('sha256').update(value.normalize('NFC')).digest('hex');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user