Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <string.h>
- #define INPUT_FILE "input.txt"
- /* The maximum name length and number of people and movies */
- #define MAX_NAME_LENGTH 200
- #define MAX_PEOPLE 700
- #define MAX_MOVIES 200
- /* The only global variables permitted in this project are below */
- char movieData[MAX_MOVIES][MAX_NAME_LENGTH] = {0};
- char personData[MAX_PEOPLE][MAX_NAME_LENGTH] = {0};
- int ratingsMatrix[MAX_PEOPLE][MAX_MOVIES] = {0};
- int numberOfMovies = 0;
- int numberOfPeople = 0;
- void GetData (void);
- int GetPersonId (char *person) ;
- int GetMovieId (char *movie) ;
- void AlgorithmOne (int personId) ;
- void AlgorithmTwo (int personId);
- // This program is used to recommened movies for a particular person. The program takes an input from a file which contains names of
- // people who have rated particular movies. The program then takes an input of a person and recommends the movies that should be watched
- // by that peson along with the rating. This is done through two algorithms, the first algorithm generates a movie which has the highest
- // 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
- // their highest ranked movie, which again has not been seen by the user.
- int main(void)
- {
- int personId;
- char person[MAX_NAME_LENGTH];
- GetData();
- printf("\nENGGEN 131 2011, Project 2\nCalculate Recommendations for: " );
- scanf("%s", person);
- personId = GetPersonId(person);
- AlgorithmOne(personId);
- AlgorithmTwo(personId);
- return 0;
- }
- // The GetData function is used to read the data from the text file.
- // Also this function organizes the ratings provided for each movie into a 2D matrix, by calling GetPersonId and GetMovieId functions.
- void GetData (void)
- {
- char person[MAX_NAME_LENGTH];
- char movie[MAX_NAME_LENGTH];
- int personId;
- int movieId;
- int rating;
- FILE *fp;
- fp = fopen(INPUT_FILE, "r");
- if (fp == NULL){
- printf("Sorry, the file could not be opened");
- } else {
- while (fscanf(fp, "%s", person) != EOF){
- personId = GetPersonId(person); // Calls the GetPersonId function and finds the correct personId number
- fscanf(fp, "%s", movie);
- movieId = GetMovieId(movie); // Calls the GetMovieId function and finds the correct movieId number
- fscanf(fp, "%d", &rating);
- ratingsMatrix[personId][movieId] = rating; // Stores a particular rating given by particular person for a particular movie
- }
- }
- }
- // The GetPersonId function takes the name scanned from the input file and returns a unique id number for that person.
- // Also the persons name is stored in a matrix (personData). The function checks for duplicates and ensures that the same person
- // is not added twice into the personData matrix. The number of people that have been stored in the matrix is also updated
- int GetPersonId(char *person)
- {
- int i;
- int duplicate = 0;
- int test = 0;
- for(i=0; i < numberOfPeople; i++){
- duplicate = strcmp(personData[i], person); // Checks to see if the persons scanned from the file is present in the personData array
- if (duplicate == 0){
- test = 1; // If the persons name already exists in the array then the function will return
- return (i); // the personId as the position (i) that the persons name was found at in the array
- break;
- } else
- test = 0;
- }
- if (!test){
- strcpy(personData[numberOfPeople], person); // If the persons name is not yet present in the array it is then copied into the personData
- numberOfPeople++; // array, also the number of people present in the array is updated.
- }
- return (numberOfPeople - 1);
- }
- // The GetMovieId function takes the movie name scanned from the input file and returns a unique id number for that movie.
- // Also the movie name is stored in a matrix (movieData). The function checks for duplicates and ensures that the same movie name
- // is not added twice into the movieData matrix. The number of movies that have been stored in the matrix is also updated
- int GetMovieId(char *movie)
- {
- int i;
- int duplicate = 0;
- int test = 0;
- for(i=0; i < numberOfMovies; i++){
- duplicate = strcmp(movieData[i], movie); // Checks to see if the movie scanned from the file is present in the movieData array
- if (duplicate == 0){
- test = 1; // If the movie name already exists in the array then the function will return
- return (i); // the movieId as the position (i) that the movie name was found at in the array
- break;
- } else
- test = 0;
- }
- if (!test){
- strcpy(movieData[numberOfMovies], movie); // If the movie is not yet present in the array it is then copied into the movieData
- numberOfMovies = numberOfMovies + 1; // array, also the number of movies present in the array is updated.
- }
- return (numberOfMovies - 1);
- }
- // 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,
- // who which we are recommending a movie. This function will then recommened a movie that has the highest average
- // rating and also the name of that movie will be printed to the screen.
- void AlgorithmOne (int personId)
- {
- int i ;
- int j ;
- double avg[MAX_MOVIES] = {0};
- double seen[MAX_MOVIES] = {0};
- double sum[MAX_MOVIES] = {0};
- double maximumAverage = 0;
- char *recommendation = {0};
- for (i=0; i <numberOfMovies; i++){
- seen[i] = 0;
- for (j=0;j<numberOfPeople; j++){
- if(j==personId && ratingsMatrix[j][i]!=0){ // Makes the sum[i] array equal to 0 for the person we are recommending for if
- sum[i]=0; // they have already seen a movie. This is because we do not want to recommend
- seen[i]= MAX_PEOPLE; // movies to the user that they have already seen.
- break;
- }
- else if (ratingsMatrix[j][i] !=0 && j != personId){
- sum[i] = sum[i] + (ratingsMatrix[j][i]); // Adds the rating given by a particular person to the sum[i] array
- seen[i] = seen[i]+ 1; // (i corresponds to a particular movie)
- } // Updates the number of people who have seen a particular movie
- }
- avg[i] = sum[i]/seen[i]; // Calculates the average of a movie by dividing all the ratings by the
- } // amount of times the movie has been seen.
- for (i=0; i <numberOfMovies; i++){
- if (avg[i] > maximumAverage){
- maximumAverage = avg[i]; // Finds the highest average and the movie which corresponds to that particular
- recommendation = movieData[i]; // average
- }
- }
- printf("\nAlgorithm 1:\nRecommended movie: %s\n", recommendation);
- printf("Average rating: %f \n\n" , maximumAverage); // Prints the recommended movie and the average of that particular movie
- }
- // 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
- // then recommend the person who has the most similar taste in movies and will recommened the highest rated movie by that person.
- // In addition the recommended movie will not of been seen by the user for which we are recommending a movie.
- void AlgorithmTwo (int personId)
- {
- int similarity[MAX_PEOPLE] = {0};
- int personPosition = 0;
- int most_similar[MAX_PEOPLE] = {0};
- int highestRankedMovie = 0;
- int moviePosition = 0;
- int i;
- int j;
- double maximumSimilarity = 0;
- for(i=0; i < numberOfPeople; i++){
- for (j =0; j < numberOfMovies; j++){
- if (i == personId){
- similarity[i] = 0; // The simlarity of the user is set to 0
- }
- else{
- similarity[i] = (ratingsMatrix[personId][j]*ratingsMatrix[i][j]) + similarity[i]; // Calculates the similarity (dot product) between users ratings and the other peoples ratings
- }
- }
- }
- for (i=0; i <numberOfPeople; i++){
- if (similarity[i] > maximumSimilarity){
- maximumSimilarity = similarity[i]; // Finds the highest similarity and the position of that person is
- personPosition = i; // stored in the variable 'personPosition'
- }
- }
- for(i=0; i <numberOfMovies; i++){
- if (ratingsMatrix[personId][i] != 0){
- most_similar[i] = 0; // The movies which have been seen by the user results in the array storing a 0
- }
- else{
- most_similar[i] = ratingsMatrix[personPosition][i]; // Copies the most similar persons ratings into another array
- }
- }
- for (i=0; i <numberOfMovies; i++){
- if (most_similar[i] > highestRankedMovie){
- highestRankedMovie = most_similar[i]; // Finds the highest ranked movie by the most similar person, the position which
- moviePosition = i; // corresponds to the highest ranked movie is stored in the 'moviePosition' variable
- }
- }
- printf("Algorithm 2:\nRecommended movie: %s\n",movieData[moviePosition]); // Prints the recommended movie and the most similar
- printf("Similarity rating = ""%f with ""%s\n",maximumSimilarity, personData[personPosition]); // position along with the similarity rating
- }
Add Comment
Please, Sign In to add comment