-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlyricsChecker.cpp
139 lines (114 loc) · 3.14 KB
/
lyricsChecker.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
//WMFO
//Andy Sayler
//lyricsCheck.cpp
#include "lyricsChecker.hpp"
//LyricChecker Constructors
LyricChecker::LyricChecker(void) {
checkREs = readREs(DEFAULT_FILENAME);
}
LyricChecker::LyricChecker(const char* filename) {
checkREs = readREs(filename);
}
//LyricChecker Destructors
LyricChecker::~LyricChecker(void) {
delete checkREs;
}
//Private Functions
list<boost::regex>* LyricChecker::readREs(const char* filename) {
ifstream reFile;
reFile.open(filename);
list<boost::regex>* regexs = new list<boost::regex>;
string sre;
boost::regex re;
if(reFile.is_open()) {
while(!reFile.eof()) {
getline(reFile,sre);
if(sre.length() && (sre[0] != '#')) {
try {
//Set up RE with case insensitivity
re.assign(sre, boost::regex_constants::icase);
}
catch(boost::regex_error& e) {
cerr << sre << " is not a valid regual expression: \""
<< e.what() << "\"" << endl;
continue;
}
regexs->push_back(re);
}
}
}
else {
cerr << "Could not open file: " << filename << endl;
}
return regexs;
}
//Public Functions
void LyricChecker::printREs(void) {
list<boost::regex>::iterator iter;
for (iter = checkREs->begin(); iter != checkREs->end(); iter++) {
cout << iter->str() << endl;
}
}
list< list<int> > LyricChecker::checkLyrics(string title,
string artist, string album) {
list< list<int> > occurances;
//Fetch linked list of lyrics
list<string>* lyrics = lyricsGrabber::getLyrics(title, artist, album);
cout << "Lyrics grabbed." << endl;
//Check for empty list
if (lyrics->empty() == true) {
delete lyrics;
return occurances;
}
//Loop through all regexs
list<boost::regex>::iterator re_iter;
for (re_iter = checkREs->begin(); re_iter != checkREs->end(); re_iter++) {
boost::regex re = *re_iter;
list<int> counts;
//Loop through all lyric sets
list<string>::iterator s_iter;
for (s_iter = lyrics->begin(); s_iter != lyrics->end(); s_iter++) {
string s = *s_iter;
//Perform Search
boost::sregex_token_iterator p(s.begin(), s.end(), re, 0);
boost::sregex_token_iterator end;
//Count occurances
int count = 0;
while(p != end){
count++;
p++;
}
counts.push_back(count);
}
occurances.push_back(counts);
}
cout << "Counts calculated." << endl;
delete lyrics;
return occurances;
}
lyric_code LyricChecker::isPositiveMatch(string title, string artist, string album){
bool posMatch = false;
list< list<int> > occurances = checkLyrics(title, artist, album);
if (occurances.empty() == true)
return NOT_FOUND;
//Loop through occurances for each regex
list< list<int> >::iterator out_iter;
for (out_iter = occurances.begin();
(out_iter != occurances.end()) && (posMatch == false);
out_iter++) {
//Loop throguh occurances for each lyric set
list<int>::iterator in_iter;
for (in_iter = out_iter->begin();
(in_iter != out_iter->end()) && (posMatch == false);
in_iter++) {
if ((*in_iter) > 0) {
posMatch = true;
}
}
}
if (posMatch == true) {
return EXPLICIT;
} else {
return SAFE;
}
}