Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // project 3
- //
- // Created by Cunpu Bo on 11-10-6.
- // Copyright 2011年 __MyCompanyName__. All rights reserved.
- //
- #include <iostream>
- #include <string>
- #include <cctype>
- using namespace std;
- string getFirstWord(string text);
- string getLastWord(string text);
- string extractWord(string& text);
- bool isUppercase(string text);
- string makeUppercase(string text);
- bool hasMultipleExclamations(string text);
- bool isGibberishWord(string text);
- bool isConsonant(char letter); //returns true if the argument is a consonant, return it it is a non-letter.
- bool keyWord(string word); //returns true if the argument is a keyword regardless of case
- void checkTheTitle(string title,int& point); //will change the point according to spam rule.
- void checkThebody(string body,int& point); //will change the point according to spam rule.
- int main ()
- {
- int numOfSpam = 0,numOfLegi = 0;
- string ans;
- do
- {
- string title,body,newLine;
- int point = 0; //the point is reset every time.
- cout << "Enter the subject line of the email: ";
- getline(cin,title);
- checkTheTitle(title,point);
- cout << "Enter the body of the email. Press Enter on an empty line to finish." << endl;
- while(true)
- {
- getline(cin,newLine);
- if (newLine == "") //if the input is empty, the exit the input.
- break;
- body += '\n';
- body += newLine;
- }
- checkThebody(body,point);
- if (point > 100)
- {
- cout << "This email is classified as spam, because its spam score is " << point << "." <<endl;
- numOfSpam++;
- }
- else
- {
- cout << "This email is classified as legitimate, because its spam score is " << point << "." <<endl;
- numOfLegi++;
- }
- cout << "Would you like to classify another email (y or n)?";
- getline(cin,ans);
- while (ans != "y" && ans != "n" )
- {
- cout << "Please enter y or n." << endl;
- cout << "Would you like to classify another email (y or n)?";
- getline(cin,ans);
- }
- }while (ans=="y");
- cout <<endl;
- cout << "Number of spam messages: " << numOfSpam << endl;
- cout << "Number of legitimate messages: " << numOfLegi << endl;
- }
- //search for the first letter, and then search for the fisrt non-letter; the length between them will be the length of the substring starting from the first returned.
- string getFirstWord(string text)
- {
- int first = 0,last = 0;
- for (; first < text.size(); first++)
- {
- if (isalpha(text[first]))
- {
- last = first;
- for (; last < text.size(); last++)
- if (!isalpha(text[last]))
- break;
- }
- if (last != 0) //break if the last if changed
- break;
- }
- return text.substr(first,last - first);
- }
- //the process if similar to getFirstWord, but the search starts from the end.
- string getLastWord(string text)
- {
- int size = static_cast<int>(text.size());
- int first = size - 1,last = size - 1;
- for (; last < text.size(); last--)
- {
- if (isalpha(text[last]))
- {
- first = last;
- for (; first >= 0; first--)
- if (!isalpha(text[first]))
- break;
- }
- if (first != size - 1) //break if the first is changed
- break;
- }
- return text.substr(first + 1,last - first);
- }
- string extractWord(string& text)
- {
- string result = getFirstWord(text);
- int index = static_cast<int>(text.find(result));// find the postion the first word
- if (result == "") //if there is no word in the string, then the string itself also is changed to empty string.
- text = "";
- else
- text = text.substr(index + result.size()); //change the text to a string starting from the next character of the first word.
- return result;
- }
- bool isUppercase(string text)
- {
- if (text == makeUppercase(text))
- return true;
- else
- return false;
- }
- string makeUppercase(string text)
- {
- string temp;
- for (int i = 0; i < text.size(); i++)
- {
- temp += toupper(text[i]);
- }
- return temp;
- }
- //search for the first exclamation, and the search for the first non-exclamation;if the length between them is no less than 3, then return true.
- bool hasMultipleExclamations(string text)
- {
- int first = 0, last = 0;
- for (; first < text.size(); first++)
- {
- if (text[first] == '!')
- {
- last = first; //set the last equal to fisrt.
- for (; last < text.size(); last++)
- if (text[last] != '!')
- {
- first = last; //restart search from the last.
- break;
- }
- }
- if (last - first >= 3) //if the length between is over 3,then break.
- break;
- }
- if (last - first >= 3)
- return true;
- else
- return false;
- }
- //search for the first consonant, and the search for the first non-econsonant;if the length between them is no less than 3, then return true.
- bool isGibberishWord(string text)
- {
- int first = 0, last = 0;
- for (; first < text.size(); first++)
- {
- if (isConsonant(text[first]))
- {
- last = first; //set the last equal to fisrt.
- for (; last < text.size(); last++)
- if (!isConsonant(text[last]))
- {
- first = last; //restart search from the last.
- break;
- }
- }
- if (last - first >= 3) //if the length between is over 3,then break.
- break;
- }
- if (last - first >= 3)
- return true;
- else
- return false;
- }
- bool isConsonant(char letter)
- {
- if (!isalpha(letter)) //if not a letter, return false.
- return false;
- char upperLetter = toupper(letter);
- if (upperLetter == 'A' || upperLetter == 'E' || upperLetter == 'I' || upperLetter == 'O' || upperLetter == 'U')
- return false;
- else
- return true;
- }
- bool keyWord(string word)
- {
- word = makeUppercase(word);
- if (word == "BUY" || word == "CHEAP" || word == "CLICK" || word == "DIPLOMA" || word == "FREE" || word == "LIMITED" || word == "ENLARGE" ||
- word == "MONEY" || word == "NOW" || word == "OFFER" || word == "ONLY" || word == "PILLS" || word == "SEX")
- return true;
- else
- return false;
- }
- void checkTheTitle(string line,int& point)
- {
- int numOfUpper = 0,totalNum = 0; //count the number of upper words and the total words.
- if (hasMultipleExclamations(line))
- {
- point += 20;
- }
- string lastWord;
- lastWord = getLastWord(line);
- if (isGibberishWord(lastWord))
- {
- point += 40;
- }
- while (true)
- {
- string newWord;
- newWord = extractWord(line);
- if (newWord == "")
- break;
- if (isUppercase(newWord))
- numOfUpper++;
- totalNum++;
- }
- if ((numOfUpper*1.0/totalNum) > 0.9)
- {
- point+=30;
- }
- }
- void checkThebody(string line,int& point)
- {
- int numOfUpper = 0,totalNum = 0;
- while (true)
- {
- string newWord;
- newWord = extractWord(line);
- if (newWord == "")
- break;
- if (keyWord(newWord))
- point += 5;
- if (isUppercase(newWord))
- numOfUpper++;
- totalNum++;
- }
- if ((numOfUpper*1.0/totalNum) > 0.5)
- {
- point+=40;
- }
- }
Add Comment
Please, Sign In to add comment