Advertisement
Guest User

na minha casa tu senta na pia

a guest
Dec 5th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.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<bits/stdc++.h>
  8.  
  9. using namespace std;
  10. const int alphabet_size = 127;
  11.  
  12. class Node{
  13. public:
  14. //Node *parent;
  15. Node *children[alphabet_size];
  16. bool isLeaf;
  17. int id;
  18. //vector<int> occurencies;
  19. Node(){//constructor
  20. for (int i = 0; i < alphabet_size; i++) {
  21. children[i] = NULL;
  22. }
  23. isLeaf = false;
  24. }
  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. int search(string s, Node *temp);
  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. int Trie::search(string s, Node *temp){
  57. temp = root;
  58. for(int i=0; i<s.size();i++){
  59. int index = s[i];
  60. if(!temp->children[index])
  61. return 0;
  62. temp = temp->children[index];
  63. }
  64. return temp->id;
  65. }
  66.  
  67.  
  68. class movie{
  69. public:
  70. int id;
  71. string title;
  72. vector<string> genres;
  73. float rating; //I intend to update the average incrementally: count ++; rating = ((rating*(count-1)) + newuserrating) /count;
  74. int count;
  75. void init(int entryid, string entrytitle, vector<string> entrygenres){
  76. id = entryid;
  77. title = entrytitle;
  78. genres = entrygenres;
  79. float rating = 0;
  80. int count = 0;
  81. }
  82. };
  83. class hashtable{
  84. public:
  85.  
  86. list<movie> *table;
  87. int size; //ideally choose prime number that is at least 120% the number of entries
  88. int elements_count;
  89. int collisions_count;
  90.  
  91. hashtable(int sz)
  92. {
  93. size = sz;
  94. table = new list<movie>[size];
  95. elements_count = 0;
  96. collisions_count=0;
  97. }
  98.  
  99. void insert(movie entry){
  100. //consists in generating the hash function of the correspondent movie and doing the push_back in table[hash]
  101. int h = hash(entry.id);
  102. if(!table[h].empty())
  103. collisions_count++;
  104. table[h].push_back(entry);
  105. elements_count++;
  106.  
  107. }
  108.  
  109. //using the division method for int entries:
  110. int hash(int id){
  111. return id % size;
  112. }
  113.  
  114. movie search(int entry){
  115. int index = hash(entry);
  116. int cont=0;
  117. movie y;
  118. for(auto x: table[index])
  119. {
  120. cont++;
  121. if(x.id == entry)
  122. {
  123. return x;
  124. }
  125. }
  126. return y;
  127. }
  128.  
  129. void del(int entry){
  130. int index = hash(entry);
  131. list <movie> :: iterator i;
  132. for (i = table[index].begin();i != table[index].end(); i++) {
  133. if (i->id == entry)
  134. break;
  135. }
  136. if (i != table[index].end()){
  137. table[index].erase(i);
  138. elements_count--;
  139. }
  140. }
  141.  
  142. /*
  143. void del(int entry){
  144. int index = hash(entry);
  145. for(auto x: table[index])
  146. {
  147. if(x.id == entry)
  148. {
  149. table[index].erase(x);
  150. elements_count--;
  151. break;
  152. }
  153. }
  154. }
  155. */
  156. void print(){
  157. //just in case
  158. for(int i=0;i<size;i++)
  159. {
  160. for(auto d: table[i])
  161. cout<<d.title<<" ";
  162. cout<<endl;
  163.  
  164. }
  165. }
  166.  
  167. void searchmovie(string title){
  168. cout << title << endl; //TBD, just for testing for now
  169. }
  170.  
  171. void searchuser(string userid){
  172. cout << userid << endl; //TBD, just for testing for now
  173. }
  174.  
  175. void searchgenre(int N, string genre){
  176. cout << N << " " << genre << endl; //TBD, just for testing for now
  177. }
  178.  
  179. void searchtag(vector<string> taglist){
  180. for (auto i: taglist)
  181. cout << i; //TBD, just for testing for now
  182. cout << endl;
  183. }
  184.  
  185. };
  186.  
  187. class data{
  188.  
  189. };
  190. void getmovies(string filename, hashtable* h, Trie* t){
  191. int id;
  192. string title;
  193. string genre;
  194. vector<string> genres;
  195.  
  196. movie entry;
  197. ifstream fin;
  198. string line; //to read each line
  199. fin.open(filename);
  200.  
  201. string skip;
  202. string aux;
  203.  
  204. getline(fin,skip);
  205. while(fin >> id){
  206. getline(fin,skip,'"');
  207. getline(fin,title,'"');
  208. getline(fin,skip,'"');
  209. getline(fin,aux,'"');
  210. stringstream temporary;
  211. temporary << aux;
  212. while(getline(temporary,aux,'|'))
  213. genres.push_back(aux);
  214. getline(fin,skip,'\n');
  215. entry.init(id,title,genres);
  216. h->insert(entry);
  217. t->insert(title,id);
  218. genres.clear();
  219. }
  220. fin.close();
  221. }
  222.  
  223.  
  224.  
  225. int main(void){
  226. hashtable h(27281);
  227. Trie t;
  228. getmovies("movie.csv",&h,&t);
  229. cout << "\nDataset MovieLens 20M - Use 'help' to show commands.\n";
  230. int quit = 0;
  231. string input;
  232. while(!quit){
  233. cout << ">";
  234. getline(cin,input);
  235. istringstream iss(input);
  236. iss >> input;
  237. if (input == "help"){
  238. cout << "movie <title or prefix> - Movies by title or prefix.\n";
  239. cout << "user <userID> - Reviews made by user from given userID.\n";
  240. cout << "top<N> '<genre>' - Top N movies by rating in given genre (only movies with at least 1000 reviews are shown).\n";
  241. cout << "tags <list of tags> - Movies having the given tags (write tags inside apostrophes).\n";
  242. cout << "quit - Quits program.\n";
  243. }
  244. else if (input == "movie"){
  245. Node looked;
  246. movie hashlooked;
  247. string skip;
  248. getline(iss,skip,' ');
  249. getline(iss,input);
  250. cout << "movieid title genres rating count" << endl;
  251. if(t.search(input,&looked)){
  252. cout << looked.id;
  253. hashlooked = h.search(looked.id);
  254.  
  255. //cout << looked.id << " " << hashlooked.title << " ";
  256. int count = 0;
  257. for(auto x: hashlooked.genres){
  258. if(count)
  259. cout << "|";
  260. cout << x;
  261. }
  262. //cout << " " << hashlooked.rating << " " << hashlooked.count;
  263. }
  264. }
  265. else if (input == "user"){
  266. iss >> input;
  267. h.searchuser(input);
  268. }
  269. else if (input.substr(0,3) == "top"){
  270. int N = stoi(input.substr(3,-1));
  271. iss >> input;
  272. if(input[0] != '\'' && input[-1] != '\'')
  273. cout << "Genre not recognised. Remember to write it between apostrophes.\n";
  274. else{
  275. h.searchgenre(N,input.substr(1,input.length()-2));
  276. }
  277. }
  278. else if (input == "tags"){
  279. vector<string> tags;
  280. string skip;
  281. string aux;
  282. getline(iss,skip,'\'');
  283. while(getline(iss,aux,'\''))
  284. tags.push_back(aux);
  285. if(!tags.empty())
  286. h.searchtag(tags);
  287. else
  288. cout << "Tags not recognised. Remember to write them between apostrophes.\n";
  289. }
  290. else if (input == "quit")
  291. quit = 1;
  292. }
  293. return 0;
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement