This repository has been archived by the owner on Jul 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmost-similar.c
130 lines (117 loc) · 3.35 KB
/
most-similar.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
#define BUFSIZE 512
#include <stdio.h>
#include <strings.h>
#include <stdlib.h>
#include <unistd.h>
/*
* This implementation allows the costs to be weighted:
*
* - w (as in "sWap")
* - s (as in "Substitution")
* - a (for insertion, AKA "Add")
* - d (as in "Deletion")
*
*/
double calc_similarity(const char *string1, const char *string2)
{
int len1 = strlen(string1),
len2 = strlen(string2);
double w = 0.75, s = 1, a = 1, d = 1,
*row0 = (double *)&(double[BUFSIZE]){},
*row1 = (double *)&(double[BUFSIZE]){},
*row2 = (double *)&(double[BUFSIZE]){};
for (int j = 0; j <= len2; ++j) {
row1[j] = j * a;
}
for (int i = 0; i < len1; ++i) {
row2[0] = (i + 1) * d;
for (int j = 0; j < len2; ++j) {
/* substitution */
row2[j + 1] = row1[j] + s * (string1[i] != string2[j]);
/* swap */
if (i > 0 && j > 0 &&
string1[i - 1] == string2[j] &&
string1[i] == string2[j - 1] &&
row2[j + 1] > row0[j - 1] + w) {
row2[j + 1] = row0[j - 1] + w;
}
/* deletion */
if (row2[j + 1] > row1[j + 1] + d) {
row2[j + 1] = row1[j + 1] + d;
}
/* insertion */
if (row2[j + 1] > row2[j] + a) {
row2[j + 1] = row2[j] + a;
}
}
double *dummy = row0;
row0 = row1;
row1 = row2;
row2 = dummy;
}
return 1.0 - row1[len2] / (len1 > len2 ? len1 : len2);
}
int readline(char *buffer)
{
int length;
fgets(buffer, BUFSIZE, stdin);
if (ferror(stdin) || feof(stdin)) {
return -1;
}
length = strlen(buffer);
if (length && buffer[length - 1] == '\n') {
buffer[--length] = '\0';
}
return length;
}
int main(int argc, char *argv[])
{
int opt, min_length = 1, length = 0;
double min_similarlity = 0.0, similarlity = 0.0;
char input[BUFSIZE] = {'\0'}, comparison[BUFSIZE] = {'\0'}, similitude[BUFSIZE] = {'\0'};
while ((opt = getopt(argc, argv, "i:l:s:h")) != -1) {
switch (opt) {
case 'i':
strncpy(input, optarg, BUFSIZE - 1);
break;
case 'l':
min_length = atoi(optarg);
break;
case 's':
min_similarlity = atof(optarg);
break;
case 'h':
default:
fprintf(stderr, "Usage: %s -i input < comparisons (Splitted by \"\\n\")\n", argv[0]);
fprintf(stderr, " [-l min_length=1] [-s min_similarlity=0.0]\n");
return opt != 'h';
}
}
if (!*input) {
fputs("No input specified\n", stderr);
return 1;
}
if (*input == '_') {
return 1;
}
similarlity = min_similarlity;
while ((length = readline(comparison)) > -1) {
if (length < min_length || length < 1 || *comparison == '_') {
continue;
}
double s = calc_similarity(input, comparison);
if (s < similarlity) {
continue;
}
similarlity = s;
strncpy(similitude, comparison, BUFSIZE - 1);
if (s == 1.0) {
break;
}
}
if (*similitude == '\0') {
return 1;
}
puts(similitude);
return 0;
}