Skip to content
Snippets Groups Projects
Commit c86238da authored by Ruben Bimberg's avatar Ruben Bimberg :computer:
Browse files

Implement regular expression based filtering

parent ddb9347e
Branches
Tags
No related merge requests found
...@@ -23,7 +23,7 @@ import { ThemeService } from '../../../../theme/theme.service'; ...@@ -23,7 +23,7 @@ import { ThemeService } from '../../../../theme/theme.service';
import { TopicCloudAdministrationComponent } from '../_dialogs/topic-cloud-administration/topic-cloud-administration.component'; import { TopicCloudAdministrationComponent } from '../_dialogs/topic-cloud-administration/topic-cloud-administration.component';
import { WsCommentService } from '../../../services/websockets/ws-comment.service'; import { WsCommentService } from '../../../services/websockets/ws-comment.service';
import { CreateCommentWrapper } from '../../../utils/create-comment-wrapper'; import { CreateCommentWrapper } from '../../../utils/create-comment-wrapper';
import { TopicCloudAdminService } from '../../../services/util/topic-cloud-admin.service'; import { regexMaskKeyword, TopicCloudAdminService } from '../../../services/util/topic-cloud-admin.service';
import { TagCloudPopUpComponent } from './tag-cloud-pop-up/tag-cloud-pop-up.component'; import { TagCloudPopUpComponent } from './tag-cloud-pop-up/tag-cloud-pop-up.component';
import { TagCloudDataService, TagCloudDataTagEntry } from '../../../services/util/tag-cloud-data.service'; import { TagCloudDataService, TagCloudDataTagEntry } from '../../../services/util/tag-cloud-data.service';
import { WsRoomService } from '../../../services/websockets/ws-room.service'; import { WsRoomService } from '../../../services/websockets/ws-room.service';
...@@ -318,7 +318,8 @@ export class TagCloudComponent implements OnInit, OnDestroy, AfterContentInit { ...@@ -318,7 +318,8 @@ export class TagCloudComponent implements OnInit, OnDestroy, AfterContentInit {
if (rotation === null || this._currentSettings.randomAngles) { if (rotation === null || this._currentSettings.randomAngles) {
rotation = Math.floor(Math.random() * 30 - 15); rotation = Math.floor(Math.random() * 30 - 15);
} }
const filteredTag = tag.replace(maskedCharsRegex, '').trim(); const filteredTag = tag.replace(maskedCharsRegex, '')
.replace(regexMaskKeyword, '').replace(/ +/, ' ').trim();
newElements.push(new TagComment(filteredTag, tag, rotation, tagData.weight, tagData, newElements.length)); newElements.push(new TagComment(filteredTag, tag, rotation, tagData.weight, tagData, newElements.length));
} }
} }
......
...@@ -70,34 +70,33 @@ export class ProfanityFilterService { ...@@ -70,34 +70,33 @@ export class ProfanityFilterService {
censorPartialWordsCheck: boolean, censorPartialWordsCheck: boolean,
censorLanguageSpecificCheck: boolean, censorLanguageSpecificCheck: boolean,
lang?: string): [string, boolean] { lang?: string): [string, boolean] {
let filteredString = str; let profWords: any[];
let profWords = [];
if (censorLanguageSpecificCheck) { if (censorLanguageSpecificCheck) {
profWords = BadWords[(lang !== 'AUTO' ? lang.toLowerCase() : localStorage.getItem('currentLang'))]; profWords = BadWords[(lang !== 'AUTO' ? lang.toLowerCase() : localStorage.getItem('currentLang'))];
} else { } else {
profWords = this.profanityWords; profWords = this.profanityWords;
} }
str = str.replace(new RegExp(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi), ''); const list = profWords.concat(this.getProfanityListFromStorage());
const toCensoredString = censorPartialWordsCheck ? str.toLowerCase() : str.toLowerCase().split(/[\s,.]+/); if (list.length < 1) {
return [str, false];
}
const escapeRegex = /[.*+\-?^${}()|\[\]\\]/g;
const censoredWords = list
.reduce((acc, elem) => acc + (acc.length > 1 ? '|' : '') + elem.replace(escapeRegex, '\\$&'), '(') + ')';
const regex = new RegExp(censorPartialWordsCheck ? censoredWords : '\\b' + censoredWords + '\\b', 'gmi');
let result = '';
let censored = false; let censored = false;
profWords.concat(this.getProfanityListFromStorage()).forEach(word => { let m: RegExpExecArray;
if (toCensoredString.includes(word)) { let lastIndex = 0;
filteredString = this.replaceString(filteredString, word, this.generateCensoredWord(word.length)); while ((m = regex.exec(str)) !== null) {
censored = true; result += str.substring(lastIndex, m.index) + '*'.repeat(regex.lastIndex - m.index);
lastIndex = regex.lastIndex;
censored = true;
if (m.index === regex.lastIndex) {
regex.lastIndex++;
} }
});
return [filteredString, censored];
}
private replaceString(str: string, search: string, replace: string) {
return str.replace(new RegExp(search, 'gi'), replace);
}
private generateCensoredWord(count: number) {
let res = '';
for (let i = 0; i < count; i++) {
res += '*';
} }
return res; result += str.substring(lastIndex);
return [result, censored];
} }
} }
...@@ -17,6 +17,8 @@ import { UserRole } from '../../models/user-roles.enum'; ...@@ -17,6 +17,8 @@ import { UserRole } from '../../models/user-roles.enum';
import { CloudParameters } from '../../utils/cloud-parameters'; import { CloudParameters } from '../../utils/cloud-parameters';
import { RoomDataService } from './room-data.service'; import { RoomDataService } from './room-data.service';
export const regexMaskKeyword = /\b(frage|antwort|aufgabe|hallo|test|bzw|muss|more to come)\b/gmi;
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
}) })
...@@ -59,7 +61,6 @@ export class TopicCloudAdminService { ...@@ -59,7 +61,6 @@ export class TopicCloudAdminService {
roomDataService: RoomDataService, roomDataService: RoomDataService,
config: TopicCloudAdminData, config: TopicCloudAdminData,
keywordFunc: (SpacyKeyword, boolean) => void) { keywordFunc: (SpacyKeyword, boolean) => void) {
const regexMaskKeyword = /\b(frage|antwort|aufgabe|hallo|test|bzw|muss|more to come)\b/gmi;
let source = comment.keywordsFromQuestioner; let source = comment.keywordsFromQuestioner;
let censored = roomDataService.getCensoredInformation(comment).userKeywordsCensored; let censored = roomDataService.getCensoredInformation(comment).userKeywordsCensored;
let isFromQuestioner = true; let isFromQuestioner = true;
...@@ -80,8 +81,7 @@ export class TopicCloudAdminService { ...@@ -80,8 +81,7 @@ export class TopicCloudAdminService {
const wantedLabels = config.wantedLabels[comment.language.toLowerCase()]; const wantedLabels = config.wantedLabels[comment.language.toLowerCase()];
for (let i = 0; i < source.length; i++) { for (let i = 0; i < source.length; i++) {
const keyword = source[i]; const keyword = source[i];
keyword.text = keyword.text.replace(regexMaskKeyword, '').replace(/ +/, ' '); if (keyword.text.replace(regexMaskKeyword, '').replace(/ +/, ' ').trim().length < 3) {
if (keyword.text.trim().length < 1) {
continue; continue;
} }
if (censored[i]) { if (censored[i]) {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment