-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweetFilter.cpp
185 lines (155 loc) · 6.43 KB
/
tweetFilter.cpp
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
void filterAndFixTweets( string fromFileName, string toFileName, string words[] )
{
int count=0;
// Input stream from a file to read from
ifstream fromFile(fromFileName);
// Output stream to a file to write onto
ofstream toFile(toFileName);
// Some Temporary string variables to store the whole line and a specific word
string currentLine="", word="";
// Looping over the file to read from
while( fromFile )
{
// Checking if the file has a next line and saving it temporarily in the variable "currentLine"
if(getline( fromFile, currentLine ))
{
// Adding a space to make sure the last word gets read
currentLine += " ";
// Looping on the extracted line of string to get the number of words present in it
for(int i = 0; i < currentLine.length(); i++)
{
// Checking if a word is completed, by comparing the character to a space ' '
if(currentLine[i]==' ')
{
// Counting the number of words into the variable 'count'
count++;
}
}
// Looping over the current file to save the edited/new string with filtered tweets into a new file
for (int i = 0; i < currentLine.length(); i++)
{
// Checking if a word is reached to save it in another file
if (currentLine[i] == ' ' )
{
// Comparing the word to all the banned words in the list
for( int i=0; i<count; i++ )
{
transform(word.begin(), word.end(), word.begin(), ::tolower);
transform(words[i].begin(), words[i].end(), words[i].begin(), ::tolower);
// Comparing the current word in the string line with the
if(words[i]==word)
{
word="***";
}
}
toFile << word + currentLine[i] ;
word = "";
}
else
{
word += currentLine[i];
}
}
toFile << endl;
}
}
// Closing both the files
fromFile.close();
toFile.close();
}
void filterAndShowTweets( string fileName, string bannedWords[], int bannedWordCount )
{
// Declaring and Initializing variable for counting and storing the length of the tweet's arrays
int count=0, tweetWordCount=0;;
// Declaring and Initializing array variable to store the words of the tweet file
string tweetWords[500], tempStr;
// Opening the tweet file to read its words
ifstream currentFile( fileName );
// Looping over all the words in tweet file and saving it in an array
// while( currentFile >> tweetWords[tweetWordCount++] );
while( currentFile )
{
// Checking if the file has a next line and saving it temporarily in the variable "currentLine"
if(getline( currentFile, tempStr ))
{
// Adding a space to make sure the last word gets read
tempStr += " ";
string word="";
// Looping over the current file to save the edited/new string with filtered tweets into a new file
for (int i = 0; i < tempStr.length(); i++)
{
// Checking if a word is reached to save it in another file
if (tempStr[i] == ' ' || tempStr[i] == '.' || tempStr[i] == ',' || tempStr[i] == '!'
|| tempStr[i] == '?' || tempStr[i] == ':' || tempStr[i] == ';' )
{
transform(word.begin(), word.end(), word.begin(), ::tolower);
tweetWords[tweetWordCount++] = word ;
word = "";
}
else
{
word += tempStr[i];
}
}
}
}
// Looping Over the banned words
for( int i=0; i<bannedWordCount-1; i++ )
{
// Resetting the count to count another word
count=0;
// Looping Over the tweet words
for( int j=0; j<tweetWordCount; j++ )
{
// Checking if the banned word is found in the tweets any number of times
if( bannedWords[i] == tweetWords[j] )
{
// Counting the number of times the banned word is found in the tweet
count++;
}
}
// Printing the banned words along with the counts of their existance in the tweet
cout << "\'" << bannedWords[i] << "\' found " << count << " times" << endl;
}
}
int main()
{
// Declaring and Initializing variables for counting, and storing the length of banned word array
int count=0, bannedWordCount=0;
// Declaring and Initializing array variable to store the words of the banned word file
string bannedWords[100];
// Declaring a file stream variable for reading a file
fstream currentFile;
// Opening the banned words file to read its words
currentFile.open( "banned.txt", ios::in );
// Looping over all the words in the banned words file and saving it in an array
while( currentFile )
{
// Saving each banned word line-by-line in an array
getline( currentFile, bannedWords[bannedWordCount++] );
}
// Closinig the Banned Words File
currentFile.close();
cout << "\n TWEETS1.TXT :-\n" ;
filterAndShowTweets("tweets1.txt", bannedWords, bannedWordCount);
cout << "\n\n TWEETS2.TXT :-\n" ;
filterAndShowTweets("tweets2.txt", bannedWords, bannedWordCount);
cout << "\n\n TWEETS3.TXT :-\n" ;
filterAndShowTweets("tweets3.txt", bannedWords, bannedWordCount);
cout << "\n\n TWEETS4.TXT :-\n" ;
filterAndShowTweets("tweets4.txt", bannedWords, bannedWordCount);
filterAndFixTweets("tweets1.txt", "tweets1Filtered.txt", bannedWords);
filterAndFixTweets("tweets2.txt", "tweets2Filtered.txt", bannedWords);
filterAndFixTweets("tweets3.txt", "tweets3Filtered.txt", bannedWords);
filterAndFixTweets("tweets4.txt", "tweets4Filtered.txt", bannedWords);
// Closinig the 1st Tweet File
currentFile.close();
// Ending the main function by returning 0
return 0;
}
//