Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <algorithm>
- #include <iterator>
- #include <vector>
- #include <fstream>
- #include <unordered_set>
- using namespace std;
- string getNameFile();
- vector<string> getinput(string sFileName);
- vector<int> getPositions();
- unordered_set<string> getBannedWords();
- vector<string> Solve(vector<string> phrases);
- vector<string> deleteWords(unordered_set<string> banned_words, vector<string> phrases);
- void deletePhrases(vector<string> &phrases, vector<int> positions);
- void sortPhrases(vector<string> &phrases, int order);
- void lowerCases(vector<string> &phrases);
- void print(vector<string> &answer);
- /*
- Structs purpose:
- Used for sorting any type of data, ascending or descending.
- */
- struct ascending
- {
- template<class T>
- bool operator()(T const &a, T const &b) const { return a > b; }
- };
- struct descending
- {
- template<class T>
- bool operator()(T const &a, T const &b) const { return a < b; }
- };
- /*
- getNameFile() – Function
- Args:
- None
- Return:
- string sFileName: Name of the file to be open.
- */
- string getNameFile(){
- string sFileName;
- cout << "Place name of the file to process: ";
- cin >> sFileName;
- return sFileName;
- }
- /*
- getInput(string) – Function
- Args:
- sFileName: Name of the file to be open and read.
- Return:
- vector<string> input: List of phrases that were read from the file.
- */
- vector<string> getinput(string sFileName){
- vector<string> input;
- string sentence;
- ifstream file(sFileName);
- while(getline(file, sentence)){ //Agarrar cada oracion y guardarla en un vector
- input.push_back(sentence);
- }
- return input;
- }
- /*
- getPositions() – Function
- Args:
- None
- Return:
- vector<int> positinos: List of the number of phrases that are going to be deleted.
- */
- vector<int> getPositions(){
- vector<int> positions;
- int index;
- cout << "Enter the number of phrase that you'd like to delete. To end enter '0'" << endl;
- while(cin >> index && index != 0){
- positions.push_back(index);
- }
- return positions;
- }
- /*
- getBannedWords() – Function
- Args:
- None
- Return:
- pair<vector<string>, unordered_set<string>> set_list_banned_words:
- First: List of banned words
- Second: Set of banned words
- */
- unordered_set<string> getBannedWords(){
- vector<string> banned_words;
- unordered_set<string> set_banned_words;
- string word;
- cout << "Enter all the words to delete from the file. To finish enter '*End*'" << endl;
- while(cin >> word && word != "*End*"){
- banned_words.push_back(word);
- }
- lowerCases(banned_words);
- for(int banned_iterator = 0; banned_iterator < banned_words.size(); banned_iterator++){
- set_banned_words.insert(banned_words[banned_iterator]);
- }
- return set_banned_words;
- }
- /*
- Solve(vector<string>) – Function
- Args:
- phrases: phrases that were read from the input file
- Return:
- vector<string> clean_phrases: New list of phrases that contain
- */
- vector<string> Solve(vector<string> phrases){
- vector<string> clean_phrases;
- for(int phrase_iterator=0; phrase_iterator<phrases.size(); phrase_iterator++){
- vector<string> phrase; //vector que contenera cada palabra de una oracion
- istringstream iss(phrases[phrase_iterator]);
- do
- {
- string subs;
- iss >> subs;
- phrase.push_back(subs);
- } while (iss); //Loop para meter cada palabra de la oracion dentro de un vector
- /*
- Phrase processing
- Purpose: Loop using a circular shift approach to creat all the new phrases.
- */
- string new_phrase;
- for (int word_iterator=0; word_iterator < phrase.size()-1; word_iterator++){
- new_phrase = "";
- for (int circular_iterator=0; circular_iterator < phrase.size(); circular_iterator++){
- if (circular_iterator == phrase.size()-1){
- new_phrase = new_phrase + phrase[(word_iterator + circular_iterator)%phrase.size()];
- } else if (circular_iterator == 0){
- new_phrase = new_phrase + phrase[(word_iterator + circular_iterator)%phrase.size()] + " ";
- } else {
- new_phrase = new_phrase + phrase[(word_iterator + circular_iterator)%phrase.size()] + " ";
- }
- }
- clean_phrases.push_back(new_phrase);
- }
- phrase.clear();
- }
- return clean_phrases;
- }
- /*
- deletedWords(unordered_set<string>, vector<strign>) – Function
- Args:
- banned_words: set of words that needs to be deleted
- phrases: phrases that were read from the input file
- Return:
- vector<string> clean_phrases: New phrases without the banned_words.
- */
- vector<string> deleteWords(unordered_set<string> banned_words, vector<string> phrases){
- vector<string> clean_phrases;
- vector<string> process_phrase;
- for(int phrases_iterator = 0; phrases_iterator < phrases.size(); phrases_iterator++){
- /*
- iss: String stream
- Purpose: Read word by word from the phrase. Push it, only if the word is not banned
- */
- istringstream iss(phrases[phrases_iterator]);
- do
- {
- string subs;
- iss >> subs;
- if (!banned_words.count(subs)){
- process_phrase.push_back(subs);
- }
- } while (iss);
- /*
- Phrase processing.
- Purpose: Loop through the phrase to creat the new phrase and add it to the final vector
- */
- string new_phrase = "";
- for (int word_iterator=0; word_iterator < process_phrase.size(); word_iterator++){
- new_phrase += process_phrase[word_iterator] + " ";
- }
- clean_phrases.push_back(new_phrase);
- process_phrase.clear();
- }
- return clean_phrases;
- }
- /*
- deletePhrases(vector<string>, vector<int>) – Function
- Args:
- &phrases: Reference of the list of phrases
- positions: List of positions of phrases to delete
- Return:
- None
- */
- void deletePhrases(vector<string> &phrases, vector<int> positions){
- for(int iA = 0; iA < positions.size(); iA++){
- phrases.erase(phrases.begin()+(positions[iA]-1));
- }
- }
- /*
- sortPhrases(vector<string>, int) – Function
- Args:
- &phrases: Reference of the list of phrases to be ordered.
- order: 0 - ascending order | 1 - descending order
- Return:
- None
- */
- void sortPhrases(vector<string> &phrases, int order){
- cout << "Enter sort order\n0 for ascending\n1 for descending" << endl;
- cin >> order;
- if(order == 0){
- sort(phrases.begin(), phrases.end(), ascending());
- } else {
- sort(phrases.begin(), phrases.end(), descending());
- }
- }
- /*
- lowerCases(vector<string>) – Function
- Args:
- &phrases: Reference to the list of phrases read from file.
- Return:
- None
- */
- void lowerCases(vector<string> &phrases){
- for (int phrase_iterator=0; phrase_iterator < phrases.size(); phrase_iterator++){
- transform(phrases[phrase_iterator].begin(), phrases[phrase_iterator].end(), phrases[phrase_iterator].begin(), ::tolower);
- }
- }
- /*
- print(vector<string>) – Function
- Args:
- &answer: Reference to the list of phrases read from file.
- Return:
- None
- */
- void print(vector<string> &answer){
- for (int i=0; i < answer.size(); i++){
- cout << answer[i] << endl;
- }
- }
- int main(){
- int order = 0;
- vector<string> input;
- vector<string> answer;
- vector<string> banned_words;
- vector<int> index_to_delete;
- unordered_set<string> lower_banned_words;
- string NameFile;
- NameFile = getNameFile();
- input = getinput(NameFile);
- lowerCases(input);
- lower_banned_words = getBannedWords();
- input = deleteWords(lower_banned_words, input);
- index_to_delete = getPositions();
- deletePhrases(input, index_to_delete);
- answer = Solve(input);
- index_to_delete = getPositions();
- deletePhrases(answer, index_to_delete);
- sortPhrases(answer, order);
- print(answer);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement