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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
#include <iostream>
#include <fstream>
#include <string>
#include "sensitiveWords.h"
#include "message.h"
/*
*returns true if character is punctuation
*/
std::string toLowerCase (std::string word)
{
std::string result = word;
for (int i = 0; i < word.length(); ++i)
if (word[i] >= 'A' && word[i] <= 'Z')
result[i] = result[i] + 'a' - 'A';
return result;
}
bool sentencePunctuation (char c)
{
return c == '.' || c == '?' || c == '!';
}
/*
*Return true if char is alpha numeric
*/
bool isAlphanumeric (char c)
{
return (c >= 'A' && c <= 'Z')
|| (c >= 'a' && c <= 'z')
|| (c >= '0' && c <= '9');
}
int findSentenceStop (const std::string& msg, int pos)
{
while (pos < msg.length()
&& !paragraphEnd(msg,pos)
&& !(pos > 0 && sentencePunctuation(msg[pos-1])))
++pos;
return pos;
}
/*
* Replace all characters of a sentence containing the index pos with '@'
* except for '/n'.
*/
void censorSentenceAt (std::string& msg, int pos)
{
int censorStart;
int endPos;
endPos = findSentenceStop(msg, pos);
censorStart = findSentenceStart(msg, pos);
for (censorStart; censorStart != endPos; censorStart++)
{
if(msg.at(censorStart) == '\n')
{
}
else
{
msg.at(censorStart) = '@';
}
}
}
int findSentenceStart (const std::string& msg, int pos)
{
while (pos > 0 && !paragraphStart(msg,pos) && !sentencePunctuation(msg[pos]))
--pos;
if (sentencePunctuation(msg[pos]))
++pos;
return pos;
}
/*returns true if the index pos has reached the end of a paragraph
*or the end of the message
*/
bool paragraphEnd (const std::string& msg, int pos)
{
if (pos == msg.length())
return true;
if (pos == msg.length()-1)
return false;
return msg[pos] == '\n' && msg[pos+1] == '\n';
}
/*
* Return true if the character at position pos is the first character
* of a word.
*/
bool wordBeginsAt (const std::string& message, int pos)
{
if (pos==0)
return true;
else if (!isAlphanumeric(message[pos-1]))
return true;
return false;
}
/*
* Return the word that begins at
* position beginningAt as a string
*/
std::string extractWord (const std::string& fromMessage, int beginningAt)
{
std::string wordEx;
int x = 0;
while(isAlphanumeric(fromMessage[beginningAt+x]))
{
x++;
}
wordEx = fromMessage.substr(beginningAt, x);
return wordEx;
}
/*
* Returns true if pos is the index of the beginning of the message
*or if pos is the index of the beginning of a paragraph.
*/
bool paragraphStart (const std::string& msg, int pos)
{
if (pos == 0)
return true;
if (pos == 1)
return false;
return msg[pos-1] == '\n' && msg[pos-2] == '\n';
}
|