-
Notifications
You must be signed in to change notification settings - Fork 0
/
cossim.c
195 lines (174 loc) · 4.1 KB
/
cossim.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#include <string.h>
#include "cossim.h"
#include <limits.h>
#define MAX_LENGTH 30
//structure
struct Node
{
char *word;
int data1;
int data2;
struct Node *next;
};
struct Table
{
struct Node *head;
struct Node *tail;
};
///utility functions
struct Node *create_node(int count1, int count2, char *string, struct Node *next)
{
struct Node *n = malloc(sizeof(struct Node));
if (n == NULL)
{
printf("memory allocation failed. \n");
EXIT_FAILURE;
}
n->data1 = count1;
n->data2 = count2;
n->word = string;
n->next = next;
return n;
}
struct Table *create_table()
{
struct Table *new = malloc(sizeof(struct Table));
if (new == NULL)
{
printf("memory allocation failed. \n");
EXIT_FAILURE;
}
new->head = create_node(-1, -1, "$", NULL);
new->tail = create_node(-1, -1, "$", NULL);
new->head->next = new->tail;
return new;
}
struct Node *add_to_front(int count1, int count2, char *string, struct Table *table)
{
struct Node *n = create_node(count1, count2, string, table->head->next);
if (n == NULL)
{
return NULL;
}
table->head->next = n;
return n;
}
void lucky_free(struct Table *table)
{
struct Node *n = table->head->next;
struct Node *tmp;
while (n != table->tail)
{
tmp = n;
n = n->next;
free(tmp->word);
free(tmp);
}
free(table->head);
free(table->tail);
free(table);
}
//delete later
void print_table(struct Table *table)
{
struct Node *n = table->head->next;
while (n != table->tail)
{
printf("%s %d %d\n", n->word, n->data1, n->data2);
n = n->next;
}
}
void shuffle(char *string, int whichfile, struct Table *a)
{
bool check = false;
struct Table *table = a;
struct Node *tmp = table->head->next;
//tranverse the table, check if string is already in there
while (tmp != table->tail)
{
if (strcmp(string, tmp->word) == 0)
{
check = true;
if (whichfile == 1)
{
tmp->data1 += 1;
}
else
{
tmp->data2 += 1;
}
}
tmp = tmp->next;
}
if (check == false) //oh it is not in there!!!
{
if (whichfile == 1)
{
add_to_front(1, 0, string, table);
}
else
{
add_to_front(0, 1, string, table);
}
}
}
double calculator(struct Table *a)
{
struct Table *table = a;
struct Node *tmp = table->head->next;
int numerator = 0, collector1 = 0, collector2 = 0;
double denominator, result;
while (tmp != table->tail)
{
numerator += tmp->data1 * tmp->data2;
collector1 += tmp->data1 * tmp->data1;
collector2 += tmp->data2 * tmp->data2;
tmp = tmp->next;
}
denominator = sqrt(collector1) * sqrt(collector2);
result = numerator / denominator;
result = result * 100;
if (result != result) //detect the NaN
{
result = 0;
}
return result;
}
void readin(struct Table *a) //read and store elements into table
{
struct Table *table = a;
char *word1 = malloc(sizeof(char) * MAX_LENGTH); //free
char *word2 = malloc(sizeof(char) * MAX_LENGTH); //free
FILE *fp1 = fopen("result1.txt", "r");
while (fscanf(fp1, "%s", word1) != EOF)
{
shuffle(word1, 1, table);
word1 = malloc(sizeof(char) * MAX_LENGTH);
}
free(word1);
fclose(fp1);
FILE *fp2 = fopen("result2.txt", "r");
while (fscanf(fp2, "%s", word2) != EOF)
{
shuffle(word2, 2, table);
word2 = malloc(sizeof(char) * MAX_LENGTH);
}
free(word2);
fclose(fp2);
}
//main function of this file
void cossim()
{
struct Table *table = create_table();
readin(table);
//print_table(table);
printf("The similarity between these two files is: %.2f%%\n", calculator(table));
if (calculator(table) >= 80.000000) {
printf("Plagiarism detected.\n");
}
lucky_free(table);
}