This repository was archived by the owner on Feb 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewspeak.cpp
More file actions
96 lines (78 loc) · 2.55 KB
/
newspeak.cpp
File metadata and controls
96 lines (78 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* Perform text censorship and record matches information
*
* Jacob Sanchez Perez (G20812080) <jsanchez-perez@uclan.ac.uk>
* University of Central Lancashire
*
* newspeak.cpp
* 2021-04-08
*/
#include <string>
#include <vector>
#include "newspeak.h"
using namespace std;
using namespace sweary;
// Get number of partial (substring) matches of last censor job for a certain bad word
int NewspeakEngine::getPartialMatches(string key)
{
return partialMatches[key];
}
// Get number of full matches of last censor job for a certain bad word
int NewspeakEngine::getCompleteMatches(string key)
{
return completeMatches[key];
}
// Get number of matches of last censor job for a certain bad word
int NewspeakEngine::getTotalMatches(string key)
{
return getCompleteMatches(key) + getPartialMatches(key);
}
// Censor words from a text
string NewspeakEngine::censor(vector<string> bannedWords, string text, char censorChar, long unsigned int edgeChars)
{
// Make lowercase copy of text to search no-no words
string lowerCaseText = text;
for (char &c : lowerCaseText)
{
c = tolower(c);
}
// Search each banned word in the lowercase copy
for (string banned : bannedWords)
{
bool hasSearched = false;
size_t searchStart = 0;
partialMatches[banned] = 0;
completeMatches[banned] = 0;
while (searchStart != string::npos)
{
if (hasSearched)
{
// Arriving at this point means a match was found in the previous iteration of this while loop
// Redact word with desired character
for (long unsigned int i = edgeChars; i < banned.size() - edgeChars; i++)
{
text[searchStart + i] = censorChar;
}
// If any of neighbouring chars are alphanumeric, count match as partial, otherwise as complete
bool prevCharAlnum = searchStart > 0 && isalnum(text[searchStart - 1]);
unsigned int nextCharPos = searchStart + banned.size();
bool nextCharAlnum = nextCharPos < text.size() && isalnum(text[nextCharPos]);
if (prevCharAlnum || nextCharAlnum)
{
++partialMatches[banned];
}
else
{
++completeMatches[banned];
}
// Set start of search to end of current match
searchStart += banned.size();
}
// Find next instance of substring in text
size_t nextMatch = lowerCaseText.find(banned, searchStart);
searchStart = nextMatch;
hasSearched = true;
}
}
return text;
}