Advertisement
ricco_soares

trab final ver2

Dec 13th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.13 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. #include<sstream>
  4. #include<list>
  5. #include<vector>
  6. #include<cmath>
  7. #include<utility>
  8. //#include<bits/stdc++.h>
  9.  
  10. using namespace std;
  11. const int alphabet_size = 127;
  12.  
  13. class Node{
  14.     public:
  15.         //Node *parent;
  16.         Node *children[alphabet_size];
  17.         bool isLeaf;
  18.         int id;
  19.         //vector<int> occurencies;
  20.         Node(){//constructor
  21.             for (int i = 0; i < alphabet_size; i++) {
  22.                     children[i] = NULL;
  23.                 }
  24.             isLeaf = false;
  25.         }
  26. };
  27.  
  28. class Trie{
  29.     public:
  30.         Node *root;
  31.        
  32.         Trie(){//constructor
  33.         //  root= (Node *) calloc(1, sizeof(Node));
  34.             root= new Node;
  35.             //*root= new Node;
  36.         }
  37.  
  38.         //void insert(string s,int index);
  39.         void insert(string s, int id);
  40.         Node* search(string s); //searches for string and returns the Node if it's found
  41. };
  42.  
  43. void Trie::insert(string s, int id){
  44.     //int index =  
  45.     Node *temp = root;
  46.     for(int i=0; i<s.size();i++){
  47.         int index = s[i];
  48.         if(!temp->children[index])
  49.             temp->children[index]= new Node;
  50.         temp = temp->children[index];
  51.     }
  52.     temp->isLeaf=true;
  53.     temp->id = id;
  54. }
  55.  
  56. Node* Trie::search(string s){
  57.     Node *temp = root;
  58.     for(int i=0; i<s.size();i++){
  59.         int index = s[i];
  60.         if(!temp->children[index]){
  61.             return nullptr;             //next letter is not in trie, returns null
  62.         }
  63.         temp = temp->children[index];
  64.     }
  65.     if(temp != NULL)
  66.         return temp;    //returns node since got to the end of word and is still in trie
  67.     else{
  68.         return nullptr;
  69.     }
  70. }
  71.  
  72.  
  73. class movie{
  74.     public:
  75.         int id;
  76.         string title;
  77.         vector<string> genres;
  78.         float rating;   //I intend to update the average incrementally: count ++; rating = ((rating*(count-1)) + newuserrating) /count;
  79.         int count;
  80.        
  81.         void init(int entryid, string entrytitle, vector<string> entrygenres){
  82.         id = entryid;
  83.         title = entrytitle;
  84.         genres = entrygenres;
  85.         rating = 0;
  86.         count = 0;
  87.     }
  88. };
  89.  
  90. class user{
  91.     public:
  92.         int id;
  93.         vector<pair<int, float>> v;
  94.        
  95.         //movie id, user rating  
  96.  
  97.  
  98. };
  99.  
  100. class hashtableu{
  101.     public:
  102.    
  103.     user *table;
  104.     int size;  
  105.     int elements_count;
  106.     int collisions_count;  
  107.    
  108.     hashtableu(int sz)
  109.     {
  110.         this->size = sz;
  111.         table = new user[size];
  112.         elements_count = 0;
  113.         collisions_count=0;
  114.     }
  115.  
  116.     void insert(int userid, int movieid, float rating){
  117.         int index;
  118.         index = hash(userid);
  119.  
  120.         //user u;
  121.         //if(u.v.empty())
  122.         //  u.id = userid;
  123.         //pair<string,float> mr;
  124.         //mr = makepair(filmtitle,rating);
  125.         //u.push_back(mr);
  126.        
  127.         //table[index].push_back(u);
  128.         table[index].v.push_back(make_pair(movieid, rating));  
  129.  
  130.  
  131.     }
  132.        
  133.  
  134.     int hash(int id){
  135.             return id % size;
  136.     }
  137.    
  138. };
  139.  
  140.  
  141. class hashtablem{//hash table só que de filme kk
  142.     public:
  143.    
  144.         vector<movie> *table;
  145.         int size;       //ideally choose prime number that is at least 120% the number of entries
  146.         int elements_count;
  147.         int collisions_count;  
  148.    
  149.     hashtablem(int sz)
  150.     {
  151.         size = sz;
  152.         table = new vector<movie>[size];
  153.         elements_count = 0;
  154.         collisions_count=0;
  155.     }
  156.  
  157.     void insert(movie entry){
  158.     //consists in generating the hash function of the correspondent movie and doing the push_back in table[hash]  
  159.         int h = hash(entry.id);
  160.         if(!table[h].empty())
  161.             collisions_count++;
  162.         table[h].push_back(entry);
  163.         elements_count++;
  164.    
  165.     }
  166.    
  167.     //using the division method for int entries:
  168.     int hash(int id){
  169.         return id % size;
  170.     }
  171.    
  172.     movie search(int entry){
  173.     int index = hash(entry);
  174.     movie y;
  175.     y.id = -1;
  176.     for(auto x: table[index])
  177.         {
  178.             if(x.id == entry)
  179.             {
  180.                 return x;
  181.             }
  182.         }
  183.         return y;   // :]
  184.     }
  185.    
  186.     void del(int entry){
  187.         int index = hash(entry);
  188.         vector <movie> :: iterator i;
  189.         for (i = table[index].begin();i != table[index].end(); i++) {
  190.             if (i->id == entry)
  191.                 break;
  192.         }
  193.         if (i != table[index].end()){
  194.             table[index].erase(i);  
  195.             elements_count--;  
  196.         }  
  197.     }  
  198.    
  199.     void print(){
  200.         //just in case
  201.         for(int i=0;i<size;i++)
  202.         {
  203.             for(auto d: table[i])
  204.                 cout<<d.title<<" ";
  205.             cout<<endl;
  206.        
  207.         }
  208.     }
  209.    
  210.    
  211. };
  212.  
  213.  
  214. void getmovies(string filename, hashtablem* h, Trie* t){
  215.     int id;
  216.     string title;
  217.     string genre;
  218.     vector<string> genres;
  219.    
  220.     movie entry;
  221.     ifstream fin;
  222.     string line;  //to read each line
  223.     fin.open(filename);
  224.  
  225.     string skip;    //used to receive unwanted data
  226.     string aux;
  227.  
  228.     getline(fin,skip);
  229.     while(fin >> id){   //first data, receives id, and stop if that's
  230.         getline(fin,skip,'"');  //skips past first "
  231.         getline(fin,title,'"'); // reads title
  232.         getline(fin,skip,'"');  //skips past "
  233.         getline(fin,aux,'"');   //gets whole genres string
  234.         stringstream temporary; //ss to parse genres string
  235.         temporary << aux;
  236.         while(getline(temporary,aux,'|'))   //parsing genres between |s
  237.             genres.push_back(aux);
  238.         getline(fin,skip,'\n'); //you know the drill
  239.         entry.init(id,title,genres);    //fills movie object
  240.         h->insert(entry);   //inserts it in hashtablem
  241.         t->insert(title,id);//insert read data in trie
  242.         genres.clear();
  243.     }
  244.     fin.close();
  245. }
  246.  
  247. void attrating(hashtablem* hmovies, float newrating, int movieid){//function called every time a movie rating is readed to assign the value of it's rating.
  248.     int index, cont=0, aux=0, c;
  249.     index = hmovies->hash(movieid);
  250.     for(auto x: hmovies->table[index])
  251.     {
  252.         if(x.id == movieid)
  253.         {
  254.         hmovies->table[index][cont].count++;
  255.         c= hmovies->table[index][cont].count;
  256.         aux = hmovies->table[index][cont].rating;
  257.         hmovies->table[index][cont].rating= (aux*(c-1)+newrating)/c;  //rating = ((rating*(count-1)) + newuserrating) /count;
  258.            
  259.         }
  260.         cont++;
  261.     }
  262.        
  263.  
  264. }
  265.  
  266.  
  267. void getrating(string filename, hashtableu* husers, hashtablem* hmovies){
  268.     int userid, movieid;
  269.     float rating;
  270.     movie m;
  271.     string aux;
  272.     char ignore;
  273.     ifstream fin;
  274.     fin.open(filename);
  275.     getline(fin, aux);
  276.     while(fin >> userid){
  277.         fin>>ignore;//ignore the commas
  278.         fin >> movieid;
  279.         fin>>ignore;
  280.        
  281.         //m = hmovies->search(movieid);
  282.        
  283.        
  284.         fin>>rating;
  285.         attrating(hmovies, rating, movieid);
  286.         husers->insert(userid, movieid, rating);       
  287.            
  288.         getline(fin, aux);
  289.     }
  290.    
  291.     fin.close();
  292.  
  293. }
  294.  
  295. void prefixsearch(Node* root, hashtablem h){
  296.         if(root->isLeaf){   //stop condition, prints stuff on screen
  297.             movie movieinfo = h.search(root->id);
  298.  
  299.             cout << root->id << "   :   " << movieinfo.title << "   :   ";
  300.             int count = 0;
  301.             for(auto x: movieinfo.genres){
  302.                 if(count)
  303.                     cout << "|";
  304.                 cout << x;
  305.                 count++;
  306.             }
  307.             cout << "   :   " << movieinfo.rating << "   :   " << movieinfo.count << endl;
  308.         }
  309.         else{           //else tries on children (just like ww2 german experiments)
  310.             for(int i = 0; i < alphabet_size; i++)
  311.                 if(root->children[i])
  312.                     prefixsearch(root->children[i], h);
  313.         }
  314. }
  315.  
  316.  
  317. int main(void){
  318.     hashtablem h(5501); //movies
  319.     hashtableu h1(27281);   //users
  320.     Trie t;
  321.     movie m;
  322.     getmovies("movie.csv",&h,&t);
  323.     getrating("minirating.csv",&h1, &h);
  324.     cout << "\nDataset MovieLens 20M - Use 'help' to show commands.\n";
  325.     int quit = 0;
  326.     string input;
  327.     while(!quit){
  328.         cout << ">";
  329.         getline(cin,input);
  330.         istringstream iss(input);
  331.         iss >> input;
  332.         if (input == "help"){
  333.             cout << "movie <title or prefix> - Movies by title or prefix.\n";
  334.             cout << "user <userID>           - Reviews made by user from given userID.\n";
  335.             cout << "top<N> '<genre>'        - Top N movies by rating in given genre (only movies with at least 1000 reviews are shown).\n";
  336.             cout << "tags <list of tags>     - Movies having the given tags (write tags inside apostrophes).\n";
  337.             cout << "quit                    - Quits program.\n";
  338.         }
  339.         else if (input == "movie"){
  340.             string skip;
  341.             getline(iss,skip,' ');  //skip space
  342.             getline(iss,input); //gets full movie name written
  343.             Node *query = t.search(input);  //searches for stuff written
  344.             if(query != nullptr){   //if it found something
  345.                 cout << "movieid:title:genres:rating:count" << endl << endl;
  346.                 if(query->isLeaf){
  347.                    
  348.                     movie movieinfo = h.search(query->id);
  349.  
  350.                     cout << query->id << "   :   " << movieinfo.title << "   :   ";
  351.                     int count = 0;
  352.                     for(auto x: movieinfo.genres){
  353.                         if(count)
  354.                             cout << "|";
  355.                         cout << x;
  356.                         count++;
  357.                     }
  358.                     cout << "   :   " << movieinfo.rating << "   :   " << movieinfo.count << endl;
  359.                 }
  360.                 else
  361.                     prefixsearch(query,h);
  362.             }
  363.             else
  364.                 cout << "No results were found." << endl;
  365.         }
  366.         else if (input == "user"){
  367.             iss >> input;
  368.             iss >> input;
  369.             int N = stoi(input);    //gets stuff after 'top' from string into int
  370.             int index = h1.hash(N);
  371.             int auxr=0;
  372.             for(int i=0; i<h1.table[index].v.size();i++){
  373.                 auxr = h1.table[index].v[i].first;
  374.                 m = h.search(auxr);
  375.    
  376.                 cout<<"user rating: "<<h1.table[index].v[i].second;
  377.                 cout<<"  movie title: "<<m.title;
  378.                 cout<<"  global rating: "<<m.rating;
  379.                 cout<<"  global count: "<<m.count<<endl;
  380.  
  381.  
  382.  
  383.  
  384.             }
  385.         }
  386.         else if (input.substr(0,3) == "top"){
  387.             int N = stoi(input.substr(3,-1));   //gets stuff after 'top' from string into int
  388.             iss >> input;
  389.             if(input[0] != '\'' && input[-1] != '\'')   //checks for apostrophes
  390.                 cout << "Genre not recognised. Remember to write it between apostrophes.\n";
  391.             else{
  392.                 //h.searchgenre(N,input.substr(1,input.length()-2)); //searches removing them
  393.             }
  394.         }
  395.         else if (input == "tags"){
  396.             vector<string> tags;
  397.             string skip;
  398.             string aux;
  399.             getline(iss,skip,'\'');    
  400.             while(getline(iss,aux,'\''))
  401.                 tags.push_back(aux);        //separates between apostrophes and removes them
  402.             if(!tags.empty())
  403.                 cout<<"oi"<<endl;
  404.             else
  405.                 cout << "Tags not recognised. Remember to write them between apostrophes.\n";
  406.         }
  407.         else if (input == "quit")
  408.             quit = 1;
  409.     }
  410.     return 0;
  411. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement