Guest User

Untitled

a guest
Jan 22nd, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.12 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. #define INPUT_FILE "input.txt"
  7.  
  8. /* The maximum name length and number of people and movies */
  9. #define MAX_NAME_LENGTH 200
  10. #define MAX_PEOPLE 700
  11. #define MAX_MOVIES 200
  12.  
  13. /* The only global variables permitted in this project are below */
  14. char movieData[MAX_MOVIES][MAX_NAME_LENGTH] = {0};
  15. char personData[MAX_PEOPLE][MAX_NAME_LENGTH] = {0};
  16. int  ratingsMatrix[MAX_PEOPLE][MAX_MOVIES] = {0};
  17. int  numberOfMovies = 0;
  18. int  numberOfPeople = 0;
  19.  
  20. void GetData (void);
  21. int  GetPersonId (char *person) ;
  22. int  GetMovieId (char *movie) ;
  23. void AlgorithmOne (int personId) ;
  24. void AlgorithmTwo (int personId);
  25.  
  26. // This program is used to recommened movies for a particular person. The program takes an input from a file which contains names of
  27. // people who have rated particular movies. The program then takes an input of a person and recommends the movies that should be watched
  28. // by that peson along with the rating. This is done through two algorithms, the first algorithm generates a movie which has the highest
  29. // rated average and that has not been seen by the user. The second algorithm generates a person who has the most similar taste and recommends
  30. // their highest ranked movie, which again has not been seen by the user.
  31.  
  32. int main(void)
  33. {
  34.     int  personId;
  35.     char person[MAX_NAME_LENGTH];
  36.  
  37.     GetData();
  38.  
  39.     printf("\nENGGEN 131 2011, Project 2\nCalculate Recommendations for: " );
  40.     scanf("%s", person);
  41.  
  42.     personId = GetPersonId(person);
  43.    
  44.     AlgorithmOne(personId);
  45.  
  46.     AlgorithmTwo(personId);
  47.  
  48.     return 0;
  49. }
  50.  
  51. // The GetData function is used to read the data from the text file.
  52. // Also this function organizes the ratings provided for each movie into a 2D matrix, by calling GetPersonId and GetMovieId functions.
  53.  
  54. void GetData (void)
  55. {
  56.     char person[MAX_NAME_LENGTH];
  57.     char movie[MAX_NAME_LENGTH];
  58.     int  personId;
  59.     int  movieId;
  60.     int  rating;
  61.  
  62.     FILE *fp;
  63.     fp = fopen(INPUT_FILE, "r");
  64.  
  65.     if (fp == NULL){
  66.         printf("Sorry, the file could not be opened");
  67.     } else {
  68.         while (fscanf(fp, "%s", person) != EOF){
  69.             personId = GetPersonId(person);                         // Calls the GetPersonId function and finds the correct personId number
  70.            
  71.             fscanf(fp, "%s", movie);
  72.             movieId = GetMovieId(movie);                            // Calls the GetMovieId function and finds the correct movieId number
  73.            
  74.             fscanf(fp, "%d", &rating);
  75.             ratingsMatrix[personId][movieId] = rating;              // Stores a particular rating given by particular person for a particular movie
  76.         }
  77.     }
  78. }
  79.  
  80. // The GetPersonId function takes the name scanned from the input file and returns a unique id number for that person.
  81. // Also the persons name is stored in a matrix (personData). The function checks for duplicates and ensures that the same person
  82. // is not added twice into the personData matrix. The number of people that have been stored in the matrix is also updated
  83.  
  84. int GetPersonId(char *person)
  85. {
  86.     int i;
  87.     int duplicate = 0;
  88.     int test = 0;
  89.  
  90.     for(i=0; i < numberOfPeople; i++){
  91.  
  92.         duplicate = strcmp(personData[i], person);          // Checks to see if the persons scanned from the file is present in the personData array
  93.  
  94.         if (duplicate == 0){
  95.             test = 1;                                      // If the persons name already exists in the array then the function will return
  96.             return (i);                                    // the personId as the position (i) that the persons name was found at in the array
  97.             break;
  98.         } else
  99.             test = 0;  
  100.     }
  101.     if (!test){
  102.         strcpy(personData[numberOfPeople], person);       // If the persons name is not yet present in the array it is then copied into the personData
  103.         numberOfPeople++;                                 // array, also the number of people present in the array is updated.
  104.     }
  105.  
  106.     return (numberOfPeople - 1);
  107.  
  108. }
  109.  
  110. // The GetMovieId function takes the movie name scanned from the input file and returns a unique id number for that movie.
  111. // Also the movie name is stored in a matrix (movieData). The function checks for duplicates and ensures that the same movie name
  112. // is not added twice into the movieData matrix. The number of movies that have been stored in the matrix is also updated
  113.  
  114. int GetMovieId(char *movie)
  115. {
  116.     int i;
  117.     int duplicate = 0;
  118.     int test = 0;
  119.  
  120.     for(i=0; i < numberOfMovies; i++){
  121.  
  122.         duplicate = strcmp(movieData[i], movie);                 // Checks to see if the movie scanned from the file is present in the movieData array
  123.  
  124.         if (duplicate == 0){
  125.             test = 1;                                            // If the movie name already exists in the array then the function will return
  126.             return (i);                                          // the movieId as the position (i) that the movie name was found at in the array
  127.             break;
  128.         } else
  129.             test = 0;  
  130.     }
  131.     if (!test){
  132.         strcpy(movieData[numberOfMovies], movie);               // If the movie is not yet present in the array it is then copied into the movieData
  133.         numberOfMovies = numberOfMovies + 1;                    // array, also the number of movies present in the array is updated.
  134.     }
  135.  
  136.     return (numberOfMovies - 1);
  137. }
  138.  
  139. // The AlgorithmOne function takes the input of the users Id and calculates the average rating of those movies that have not been seen by the user,
  140. // who which we are recommending a movie. This function will then recommened a movie that has the highest average
  141. // rating and also the name of that movie will be printed to the screen.
  142.  
  143. void AlgorithmOne (int personId)
  144. {
  145.     int i ;
  146.     int j ;
  147.     double avg[MAX_MOVIES] = {0};
  148.     double seen[MAX_MOVIES] = {0};
  149.     double sum[MAX_MOVIES]  = {0};
  150.     double maximumAverage =  0;
  151.     char   *recommendation = {0};
  152.    
  153.  
  154.     for (i=0; i <numberOfMovies; i++){
  155.         seen[i] = 0;
  156.         for (j=0;j<numberOfPeople; j++){
  157.  
  158.             if(j==personId && ratingsMatrix[j][i]!=0){                 // Makes the sum[i] array equal to 0 for the person we are recommending for if
  159.                 sum[i]=0;                                              // they have already seen a movie. This is because we do not want to recommend
  160.                 seen[i]= MAX_PEOPLE;                                   // movies to the user that they have already seen.
  161.                 break;
  162.             }
  163.  
  164.             else if (ratingsMatrix[j][i] !=0 && j != personId){
  165.                 sum[i] = sum[i] + (ratingsMatrix[j][i]);               // Adds the rating given by a particular person to the sum[i] array
  166.                 seen[i] = seen[i]+ 1;                                  // (i corresponds to a particular movie)
  167.             }                                                          // Updates the number of people who have seen a particular movie
  168.         }
  169.         avg[i] = sum[i]/seen[i];                                       // Calculates the average of a movie by dividing all the ratings by the
  170.     }                                                                  // amount of times the movie has been seen.
  171.  
  172.     for (i=0; i <numberOfMovies; i++){        
  173.         if (avg[i] > maximumAverage){
  174.             maximumAverage = avg[i];                                   // Finds the highest average and the movie which corresponds to that particular
  175.             recommendation = movieData[i];                             // average
  176.         }
  177.     }
  178.  
  179.     printf("\nAlgorithm 1:\nRecommended movie: %s\n",  recommendation);  
  180.     printf("Average rating: %f \n\n" , maximumAverage);               // Prints the recommended movie and the average of that particular movie
  181.  
  182. }
  183.  
  184. // The AlgorithmTwo function takes the input of the users Id and calculates who has the most similar taste in movies with the user. This function will
  185. // then recommend the person who has the most similar taste in movies and will recommened the highest rated movie by that person.
  186. // In addition the recommended movie will not of been seen by the user for which we are recommending a movie.
  187.  
  188. void AlgorithmTwo (int personId)
  189. {
  190.     int similarity[MAX_PEOPLE] = {0};
  191.     int personPosition = 0;
  192.     int most_similar[MAX_PEOPLE] = {0};
  193.     int highestRankedMovie = 0;
  194.     int moviePosition = 0;
  195.     int i;
  196.     int j;
  197.     double maximumSimilarity = 0;
  198.  
  199.     for(i=0; i < numberOfPeople; i++){
  200.         for (j =0; j < numberOfMovies; j++){
  201.  
  202.             if (i == personId){
  203.                 similarity[i] = 0;                                  // The simlarity of the user is set to 0
  204.             }
  205.             else{
  206.                 similarity[i] = (ratingsMatrix[personId][j]*ratingsMatrix[i][j]) + similarity[i];   // Calculates the similarity (dot product) between users ratings and the other peoples ratings
  207.             }
  208.         }
  209.     }
  210.  
  211.     for (i=0; i <numberOfPeople; i++){        
  212.         if (similarity[i] > maximumSimilarity){
  213.             maximumSimilarity = similarity[i];                     // Finds the highest similarity and the position of that person is
  214.             personPosition = i;                                    // stored in the variable 'personPosition'
  215.         }
  216.     }
  217.  
  218.     for(i=0; i <numberOfMovies; i++){                                          
  219.  
  220.         if (ratingsMatrix[personId][i] != 0){
  221.             most_similar[i] = 0;                                 // The movies which have been seen by the user results in the array storing a 0
  222.         }
  223.         else{
  224.             most_similar[i] = ratingsMatrix[personPosition][i];  // Copies the most similar persons ratings into another array
  225.         }
  226.     }
  227.  
  228.     for (i=0; i <numberOfMovies; i++){        
  229.         if (most_similar[i] > highestRankedMovie){
  230.             highestRankedMovie = most_similar[i];               // Finds the highest ranked movie by the most similar person, the position which
  231.             moviePosition = i;                                  // corresponds to the highest ranked movie is stored in the 'moviePosition' variable
  232.         }
  233.     }
  234.  
  235.     printf("Algorithm 2:\nRecommended movie: %s\n",movieData[moviePosition]);               // Prints the recommended movie and the most similar
  236.     printf("Similarity rating = ""%f with ""%s\n",maximumSimilarity, personData[personPosition]);   // position along with the similarity rating
  237.  
  238. }
Add Comment
Please, Sign In to add comment