Advertisement
Guest User

Untitled

a guest
Apr 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <algorithm>
  5. #include <iterator>
  6. #include <vector>
  7. #include <fstream>
  8. #include <unordered_set>
  9.  
  10. using namespace std;
  11.  
  12. string getNameFile();
  13. vector<string> getinput(string sFileName);
  14. vector<int> getPositions();
  15. unordered_set<string> getBannedWords();
  16. vector<string> Solve(vector<string> phrases);
  17. vector<string> deleteWords(unordered_set<string> banned_words, vector<string> phrases);
  18. void deletePhrases(vector<string> &phrases, vector<int> positions);
  19. void sortPhrases(vector<string> &phrases, int order);
  20. void lowerCases(vector<string> &phrases);
  21. void print(vector<string> &answer);
  22.  
  23.  
  24. /*
  25.  Structs purpose:
  26.  Used for sorting any type of data, ascending or descending.
  27.  */
  28. struct ascending
  29. {
  30.     template<class T>
  31.     bool operator()(T const &a, T const &b) const { return a > b; }
  32. };
  33. struct descending
  34. {
  35.     template<class T>
  36.     bool operator()(T const &a, T const &b) const { return a < b; }
  37. };
  38.  
  39.  
  40. /*
  41.  getNameFile() – Function
  42.  
  43.  Args:
  44.  None
  45.  
  46.  Return:
  47.  string sFileName: Name of the file to be open.
  48.  */
  49. string getNameFile(){
  50.     string sFileName;
  51.     cout << "Place name of the file to process: ";
  52.     cin >> sFileName;
  53.     return sFileName;
  54. }
  55.  
  56. /*
  57.  getInput(string) – Function
  58.  
  59.  Args:
  60.  sFileName: Name of the file to be open and read.
  61.  
  62.  Return:
  63.  vector<string> input: List of phrases that were read from the file.
  64.  */
  65. vector<string> getinput(string sFileName){
  66.     vector<string> input;
  67.     string sentence;
  68.     ifstream file(sFileName);
  69.     while(getline(file, sentence)){ //Agarrar cada oracion y guardarla en un vector
  70.         input.push_back(sentence);
  71.     }
  72.     return input;
  73. }
  74.  
  75. /*
  76.  getPositions() – Function
  77.  
  78.  Args:
  79.  None
  80.  
  81.  Return:
  82.  vector<int> positinos: List of the number of phrases that are going to be deleted.
  83.  */
  84. vector<int> getPositions(){
  85.     vector<int> positions;
  86.     int index;
  87.     cout << "Enter the number of phrase that you'd like to delete. To end enter '0'" << endl;
  88.     while(cin >> index && index != 0){
  89.         positions.push_back(index);
  90.     }
  91.     return positions;
  92. }
  93.  
  94. /*
  95.  getBannedWords() – Function
  96.  
  97.  Args:
  98.  None
  99.  
  100.  Return:
  101.  pair<vector<string>, unordered_set<string>> set_list_banned_words:
  102.  First: List of banned words
  103.  Second: Set of banned words
  104.  */
  105. unordered_set<string> getBannedWords(){
  106.     vector<string> banned_words;
  107.     unordered_set<string> set_banned_words;
  108.     string word;
  109.     cout << "Enter all the words to delete from the file. To finish enter '*End*'" << endl;
  110.     while(cin >> word && word != "*End*"){
  111.         banned_words.push_back(word);
  112.     }
  113.     lowerCases(banned_words);
  114.     for(int banned_iterator = 0; banned_iterator < banned_words.size(); banned_iterator++){
  115.         set_banned_words.insert(banned_words[banned_iterator]);
  116.     }
  117.     return set_banned_words;
  118. }
  119.  
  120. /*
  121.  Solve(vector<string>) – Function
  122.  
  123.  Args:
  124.  phrases: phrases that were read from the input file
  125.  
  126.  Return:
  127.  vector<string> clean_phrases: New list of phrases that contain
  128.  */
  129. vector<string> Solve(vector<string> phrases){
  130.     vector<string> clean_phrases;
  131.    
  132.     for(int phrase_iterator=0; phrase_iterator<phrases.size(); phrase_iterator++){
  133.         vector<string> phrase; //vector que contenera cada palabra de una oracion
  134.         istringstream iss(phrases[phrase_iterator]);
  135.         do
  136.         {
  137.             string subs;
  138.             iss >> subs;
  139.             phrase.push_back(subs);
  140.         } while (iss); //Loop para meter cada palabra de la oracion dentro de un vector
  141.        
  142.        
  143.         /*
  144.          Phrase processing
  145.          Purpose: Loop using a circular shift approach to creat all the new phrases.
  146.          */
  147.         string new_phrase;
  148.         for (int word_iterator=0; word_iterator < phrase.size()-1; word_iterator++){
  149.             new_phrase = "";
  150.             for (int circular_iterator=0; circular_iterator < phrase.size(); circular_iterator++){
  151.                 if (circular_iterator == phrase.size()-1){
  152.                     new_phrase = new_phrase + phrase[(word_iterator + circular_iterator)%phrase.size()];
  153.                 } else if (circular_iterator == 0){
  154.                     new_phrase = new_phrase + phrase[(word_iterator + circular_iterator)%phrase.size()] + " ";
  155.                 } else {
  156.                     new_phrase = new_phrase + phrase[(word_iterator + circular_iterator)%phrase.size()] + " ";
  157.                 }
  158.             }
  159.             clean_phrases.push_back(new_phrase);
  160.         }
  161.         phrase.clear();
  162.     }
  163.     return clean_phrases;
  164. }
  165.  
  166. /*
  167.  deletedWords(unordered_set<string>, vector<strign>) – Function
  168.  
  169.  Args:
  170.  banned_words: set of words that needs to be deleted
  171.  phrases: phrases that were read from the input file
  172.  
  173.  Return:
  174.  vector<string> clean_phrases: New phrases without the banned_words.
  175.  */
  176. vector<string> deleteWords(unordered_set<string> banned_words, vector<string> phrases){
  177.     vector<string> clean_phrases;
  178.     vector<string> process_phrase;
  179.     for(int phrases_iterator = 0; phrases_iterator < phrases.size(); phrases_iterator++){
  180.        
  181.         /*
  182.          iss: String stream
  183.          Purpose: Read word by word from the phrase. Push it, only if the word is not banned
  184.          */
  185.         istringstream iss(phrases[phrases_iterator]);
  186.         do
  187.         {
  188.             string subs;
  189.             iss >> subs;
  190.             if (!banned_words.count(subs)){
  191.                 process_phrase.push_back(subs);
  192.             }
  193.         } while (iss);
  194.        
  195.         /*
  196.          Phrase processing.
  197.          Purpose: Loop through the phrase to creat the new phrase and add it to the final vector
  198.          */
  199.         string new_phrase = "";
  200.         for (int word_iterator=0; word_iterator < process_phrase.size(); word_iterator++){
  201.             new_phrase += process_phrase[word_iterator] + " ";
  202.         }
  203.         clean_phrases.push_back(new_phrase);
  204.         process_phrase.clear();
  205.     }
  206.     return clean_phrases;
  207. }
  208.  
  209. /*
  210.  deletePhrases(vector<string>, vector<int>) – Function
  211.  
  212.  Args:
  213.  &phrases: Reference of the list of phrases
  214.  positions: List of positions of phrases to delete
  215.  
  216.  Return:
  217.  None
  218.  */
  219. void deletePhrases(vector<string> &phrases, vector<int> positions){
  220.     for(int iA = 0; iA < positions.size(); iA++){
  221.         phrases.erase(phrases.begin()+(positions[iA]-1));
  222.     }
  223. }
  224.  
  225. /*
  226.  sortPhrases(vector<string>, int) – Function
  227.  
  228.  Args:
  229.  &phrases: Reference of the list of phrases to be ordered.
  230.  order: 0 - ascending order | 1 - descending order
  231.  
  232.  Return:
  233.  None
  234.  */
  235. void sortPhrases(vector<string> &phrases, int order){
  236.     cout << "Enter sort order\n0 for ascending\n1 for descending" << endl;
  237.     cin >> order;
  238.     if(order == 0){
  239.         sort(phrases.begin(), phrases.end(), ascending());
  240.     } else {
  241.         sort(phrases.begin(), phrases.end(), descending());
  242.     }
  243. }
  244.  
  245. /*
  246.  lowerCases(vector<string>) – Function
  247.  
  248.  Args:
  249.  &phrases: Reference to the list of phrases read from file.
  250.  
  251.  Return:
  252.  None
  253.  */
  254. void lowerCases(vector<string> &phrases){
  255.     for (int phrase_iterator=0; phrase_iterator < phrases.size(); phrase_iterator++){
  256.         transform(phrases[phrase_iterator].begin(), phrases[phrase_iterator].end(), phrases[phrase_iterator].begin(), ::tolower);
  257.     }
  258. }
  259.  
  260. /*
  261.  print(vector<string>) – Function
  262.  
  263.  Args:
  264.  &answer: Reference to the list of phrases read from file.
  265.  
  266.  Return:
  267.  None
  268.  */
  269. void print(vector<string> &answer){
  270.     for (int i=0; i < answer.size(); i++){
  271.         cout << answer[i] << endl;
  272.     }
  273. }
  274.  
  275. int main(){
  276.     int order = 0;
  277.     vector<string> input;
  278.     vector<string> answer;
  279.     vector<string> banned_words;
  280.     vector<int> index_to_delete;
  281.     unordered_set<string> lower_banned_words;
  282.     string NameFile;
  283.    
  284.     NameFile = getNameFile();
  285.     input = getinput(NameFile);
  286.     lowerCases(input);
  287.     lower_banned_words = getBannedWords();
  288.     input = deleteWords(lower_banned_words, input);
  289.     index_to_delete = getPositions();
  290.     deletePhrases(input, index_to_delete);
  291.  
  292.     answer = Solve(input);
  293.    
  294.     index_to_delete = getPositions();
  295.     deletePhrases(answer, index_to_delete);
  296.     sortPhrases(answer, order);
  297.    
  298.     print(answer);
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement