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);
- 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;
- }
- 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);
- fscanf(fp, "%s", movie);
- movieId = GetMovieId(movie);
- fscanf(fp, "%d", &rating);
- ratingsMatrix[personId][movieId] = rating;
- }
- }
- }
- int GetPersonId(char *person)
- {
- int i;
- int duplicate = 0;
- int test = 0;
- for(i=0; i < numberOfPeople; i++){
- duplicate = strcmp(personData[i], person);
- if (duplicate == 0){
- test = 1;
- return (i);
- break;
- } else
- test = 0;
- }
- if (!test){
- strcpy(personData[numberOfPeople], person);
- numberOfPeople++;
- }
- return (numberOfPeople - 1);
- }
- int GetMovieId(char *movie)
- {
- int i;
- int duplicate = 0;
- int test = 0;
- for(i=0; i < numberOfMovies; i++){
- duplicate = strcmp(movieData[i], movie);
- if (duplicate == 0){
- test = 1;
- return (i);
- break;
- } else
- test = 0;
- }
- if (!test){
- strcpy(movieData[numberOfMovies], movie);
- numberOfMovies = numberOfMovies + 1;
- }
- return (numberOfMovies - 1);
- }
- void AlgorithmOne (int personId)
- {
- double avg[MAX_MOVIES] = {0};
- double seen[MAX_MOVIES] = {0};
- double sum[MAX_MOVIES] = {0};
- double maximumAverage = 0;
- char *recommendation = {0};
- int i ;
- int j ;
- for (i=0; i <numberOfMovies; i++){
- seen[i] = 0;
- for (j=0;j<numberOfPeople; j++){
- if(j==personId && ratingsMatrix[j][i]!=0){
- sum[i]=0;
- seen[i]= MAX_PEOPLE;
- break;
- }
- else if (ratingsMatrix[j][i] !=0 && j != personId){
- sum[i] = sum[i] + (ratingsMatrix[j][i]);
- seen[i] = seen[i]+ 1;
- }
- }
- avg[i] = sum[i]/seen[i];
- }
- for (i=0; i <numberOfMovies; i++){
- if (avg[i] > maximumAverage){
- maximumAverage = avg[i];
- recommendation = movieData[i];
- }
- }
- printf("\nAlgorithm 1:\nRecommended movie: %s\n", recommendation);
- printf("Average rating: %f \n\n" , maximumAverage);
- }
- void AlgorithmTwo (int personId)
- {
- int similarity[MAX_PEOPLE] = {0};
- double maximumSimilarity = 0;
- int personPosition = 0;
- int most_similar[MAX_PEOPLE] = {0};
- int highestRankedMovie = 0;
- int moviePosition = 0;
- int i;
- int j;
- for(i=0; i < numberOfPeople; i++){
- for (j =0; j < numberOfMovies; j++){
- if (i == personId){
- similarity[i] = 0;
- }
- else{
- similarity[i] = (ratingsMatrix[personId][j]*ratingsMatrix[i][j]) + similarity[i];
- }
- }
- }
- for (i=0; i <numberOfPeople; i++){
- if (similarity[i] > maximumSimilarity){
- maximumSimilarity = similarity[i];
- personPosition = i;
- }
- }
- for(i=0; i <numberOfMovies; i++){
- if (ratingsMatrix[personId][i] != 0){
- most_similar[i] = 0;
- }
- else{
- most_similar[i] = ratingsMatrix[personPosition][i];
- }
- }
- for (i=0; i <numberOfMovies; i++){
- if (most_similar[i] > highestRankedMovie){
- highestRankedMovie = most_similar[i];
- moviePosition = i;
- }
- }
- printf("Algorithm 2:\nRecommended movie: %s\n",movieData[moviePosition]);
- printf("Similarity rating = ""%f with ""%s\n",maximumSimilarity, personData[personPosition]);
- }
Add Comment
Please, Sign In to add comment