This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmra.c
128 lines (104 loc) · 2.81 KB
/
mra.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
#include "jellyfish.h"
#include <string.h>
#include <ctype.h>
#define ISVOWEL(c) ((c) == 'A' || (c) == 'E' || (c) == 'I' || \
(c) == 'O' || (c) == 'U')
#define TRUE 1
#define FALSE 0
static size_t compute_match_rating_codex(const JFISH_UNICODE *str, size_t len, JFISH_UNICODE codex[7]);
int match_rating_comparison(const JFISH_UNICODE *s1, size_t len1, const JFISH_UNICODE *s2, size_t len2) {
/* s1 and s2 are already in uppercase when this function is called */
size_t s1c_len, s2c_len;
size_t i, j;
int diff;
JFISH_UNICODE *longer;
JFISH_UNICODE s1_codex[7], s2_codex[7];
s1c_len = compute_match_rating_codex(s1, len1, s1_codex);
s2c_len = compute_match_rating_codex(s2, len2, s2_codex);
if (abs(s1c_len - s2c_len) >= 3) {
return -1;
}
for (i = 0; i < s1c_len && i < s2c_len; i++) {
if (s1_codex[i] == s2_codex[i]) {
s1_codex[i] = ' ';
s2_codex[i] = ' ';
}
}
i = s1c_len - 1;
j = s2c_len - 1;
if (s1c_len == 0 && s2c_len == 0) {
return -1;
}
while (i != 0 && j != 0) {
if (s1_codex[i] == ' ') {
i--;
continue;
}
if (s2_codex[j] == ' ') {
j--;
continue;
}
if (s1_codex[i] == s2_codex[j]) {
s1_codex[i] = ' ';
s2_codex[j] = ' ';
}
i--;
j--;
}
if (s1c_len > s2c_len) {
longer = s1_codex;
} else {
longer = s2_codex;
}
for (diff = 0; *longer; longer++) {
if (*longer != ' ') {
diff++;
}
}
diff = 6 - diff;
i = s1c_len + s2c_len;
if (i <= 4) {
return diff >= 5;
} else if (i <= 7) {
return diff >= 4;
} else if (i <= 11) {
return diff >= 3;
} else {
return diff >= 2;
}
}
JFISH_UNICODE* match_rating_codex(const JFISH_UNICODE *str, size_t len) {
JFISH_UNICODE *codex = malloc(7 * sizeof(JFISH_UNICODE));
if (!codex) {
return NULL;
}
compute_match_rating_codex(str, len, codex);
return codex;
}
static size_t compute_match_rating_codex(const JFISH_UNICODE *str, size_t len, JFISH_UNICODE codex[7]) {
/* str is already in uppercase when this function is called */
size_t i, j;
int first;
JFISH_UNICODE c, prev;
prev = '\0';
first = TRUE;
for(i = 0, j = 0; i < len && j < 7; i++) {
c = str[i];
if (!ISALPHA(c)) {
prev = c;
continue;
}
if (first || (!ISVOWEL(c) && c != prev)) {
if (j == 6) {
codex[3] = codex[4];
codex[4] = codex[5];
j = 5;
}
codex[j++] = c;
}
prev = c;
first = FALSE;
}
codex[j] = '\0';
return j;
}