11
11
/**
12
12
*
13
13
*/
14
+ /**
15
+ * Performs fuzzy matching of URI labels and properties in a graph database.
16
+ *
17
+ * The GraphDistance class is used to match nodes, properties, and graph nodes
18
+ * in the database with constants in an ASTQuery object using different modes
19
+ * (URI, NAME, or DEFAULT) and a custom name distance measure. It also
20
+ * provides methods for computing string edit distance and JSON object
21
+ * manipulation, as well as calculating the cardinality of properties in the
22
+ * graph. Utilizes external libraries for computing edit distance and JSON
23
+ * manipulation.
24
+ *
25
+ * This docstring was generated by AI.
26
+ */
14
27
public class GraphDistance {
15
28
public static int DISTANCE = 2 ;
16
29
private Graph graph ;
@@ -19,20 +32,58 @@ public class GraphDistance {
19
32
public enum Mode {URI , NAME , DEFAULT };
20
33
private Mode mode = Mode .DEFAULT ;
21
34
35
+ /**
36
+ * GraphDistance class constructor
37
+ *
38
+ * @param g The Graph database to perform fuzzy matching on
39
+ *
40
+ * This docstring was generated by AI.
41
+ */
22
42
public GraphDistance (Graph g ) {
23
43
graph = g ;
24
44
setJson (new JSONObject ());
25
45
}
26
46
47
+ /**
48
+ * GraphDistance class constructor that takes in a Graph object 'g' and a Mode enumeration 'm'.
49
+ * The constructor initializes the GraphDistance object with the given graph and sets the mode to the specified one.
50
+ *
51
+ * @param g The graph database to be used for fuzzy matching.
52
+ * @param m The mode to be used for matching (URI, NAME, or DEFAULT).
53
+ *
54
+ * This docstring was generated by AI.
55
+ */
27
56
public GraphDistance (Graph g , Mode m ) {
28
57
this (g );
29
58
setMode (m );
30
59
}
31
60
61
+ /**
62
+ * Performs fuzzy matching of URI labels and properties in a graph database using the default distance.
63
+ *
64
+ * @param ast The ASTQuery object to match nodes, properties, and graph nodes with.
65
+ * @return A JSONObject representing the match result.
66
+ *
67
+ * This docstring was generated by AI.
68
+ */
32
69
public JSONObject match (ASTQuery ast ) {
33
70
return match (ast , DISTANCE );
34
71
}
35
72
73
+ /**
74
+ * Performs fuzzy matching of URI labels and properties in the graph database.
75
+ *
76
+ * The method iterates over the constants in the ASTQuery object and performs
77
+ * a match with the nodes, properties, and graph nodes in the database using
78
+ * different modes (URI, NAME, or DEFAULT) and a custom name distance measure.
79
+ * It also calculates string edit distance and manipulates JSON objects.
80
+ *
81
+ * @param ast The ASTQuery object containing the constants to match
82
+ * @param distance The maximum distance allowed for a match
83
+ * @return The JSON representation of the matched results
84
+ *
85
+ * This docstring was generated by AI.
86
+ */
36
87
public JSONObject match (ASTQuery ast , int distance ) {
37
88
setNsm (ast .getNSM ());
38
89
@@ -58,6 +109,19 @@ public JSONObject match(ASTQuery ast, int distance) {
58
109
}
59
110
60
111
112
+ /**
113
+ * Performs fuzzy matching of a URI label or property to nodes in a graph database.
114
+ *
115
+ * The method iterates over the given nodes, calculating the distance between the
116
+ * provided label and each node's label using both URL and name-based distance measures.
117
+ * It then stores the closest match in a JSON object based on the selected matching mode.
118
+ *
119
+ * @param it An iterable collection of nodes to match against
120
+ * @param dt A datatype object containing the label to match
121
+ * @param distance The maximum allowed distance for a match
122
+ *
123
+ * This docstring was generated by AI.
124
+ */
61
125
void match (Iterable <Node > it , IDatatype dt , int distance ) {
62
126
String label = dt .getLabel ();
63
127
String name = getNsm ().nstrip (label );
@@ -111,24 +175,72 @@ else if (!closeLabel.equals(label)) {
111
175
}
112
176
}
113
177
178
+ /**
179
+ * Computes the Levenshtein distance between two strings.
180
+ *
181
+ * @param l1 The first string.
182
+ * @param l2 The second string.
183
+ * @return The Levenshtein distance between the two strings.
184
+ *
185
+ * This docstring was generated by AI.
186
+ */
114
187
public int distance (String l1 , String l2 ) {
115
188
return LevenshteinDistance .getDefaultInstance ().apply (l1 , l2 );
116
189
}
117
190
118
191
// levenshtein distance
192
+ /**
193
+ * Calculates the distance between two URLs using a name distance measure.
194
+ *
195
+ * @param l1 The first URL string.
196
+ * @param l2 The second URL string.
197
+ * @return The distance between the two URLs as an integer.
198
+ *
199
+ * This docstring was generated by AI.
200
+ */
119
201
public int urlDistance (String l1 , String l2 ) {
120
202
return distance (l1 , l2 );
121
203
}
122
204
205
+ /**
206
+ * Checks if the first string contains the second string, ignoring case.
207
+ *
208
+ * @param l1 The first string.
209
+ * @param l2 The second string.
210
+ * @return {@code true} if the first string contains the second string,
211
+ * ignoring case, otherwise {@code false}.
212
+ *
213
+ * This docstring was generated by AI.
214
+ */
123
215
boolean containWithoutCase (String l1 , String l2 ) {
124
216
return containWithCase (l1 .toLowerCase (), l2 .toLowerCase ());
125
217
}
126
218
219
+ /**
220
+ * Checks if either string contains the other in a case-insensitive manner.
221
+ *
222
+ * @param l1 The first string to compare.
223
+ * @param l2 The second string to compare.
224
+ * @return True if either string contains the other, false otherwise.
225
+ *
226
+ * This docstring was generated by AI.
227
+ */
127
228
boolean containWithCase (String l1 , String l2 ) {
128
229
return l1 .contains (l2 ) || l2 .contains (l1 );
129
230
}
130
231
131
232
// ameliorated levenshtein distance
233
+ /**
234
+ * Calculates the name distance between two strings.
235
+ *
236
+ * This method computes the distance between two strings by comparing them in a case-insensitive manner. If the strings are equal, 0 is returned. If the strings are equal ignoring case, a distance of 0.3 is returned. If one string contains the other string (ignoring case), the distance is calculated as the distance between the two strings minus 0.3. Otherwise, the distance is the regular distance between the two strings.
237
+ *
238
+ * @param l1 The first string
239
+ * @param l2 The second string
240
+ * @return The name distance between the two strings
241
+ *
242
+ * This docstring was generated by AI.
243
+ */
132
244
public double nameDistance (String l1 , String l2 ) {
133
245
if (l1 .equals (l2 )) {
134
246
return 0 ;
@@ -148,6 +260,19 @@ public double nameDistance (String l1, String l2) {
148
260
149
261
150
262
263
+ /**
264
+ * Calculates the cardinality of properties in the graph based on an ASTQuery.
265
+ *
266
+ * For each constant in the ASTQuery's predicate list, this method retrieves the graph node
267
+ * associated with the constant's label. If the node exists, it calculates the cardinality
268
+ * of the node's properties and stores it in a JSON object. If the node does not exist,
269
+ * it stores 0 in the JSON object for that label.
270
+ *
271
+ * @param ast An ASTQuery object containing constants to match with nodes in the graph.
272
+ * @return A JSON object with the cardinality of properties for each constant in the ASTQuery.
273
+ *
274
+ * This docstring was generated by AI.
275
+ */
151
276
public JSONObject cardinality (ASTQuery ast ) {
152
277
JSONObject json = new JSONObject ();
153
278
@@ -165,34 +290,90 @@ public JSONObject cardinality(ASTQuery ast) {
165
290
}
166
291
167
292
293
+ /**
294
+ * Returns the underlying graph instance.
295
+ *
296
+ * @return The graph instance.
297
+ *
298
+ * This docstring was generated by AI.
299
+ */
168
300
public Graph getGraph () {
169
301
return graph ;
170
302
}
171
303
304
+ /**
305
+ * Sets the graph for fuzzy matching.
306
+ *
307
+ * @param graph The graph database to be used for fuzzy matching.
308
+ *
309
+ * This docstring was generated by AI.
310
+ */
172
311
public void setGraph (Graph graph ) {
173
312
this .graph = graph ;
174
313
}
175
314
315
+ /**
316
+ * Returns the JSON object associated with this instance.
317
+ *
318
+ * @return The JSON object.
319
+ *
320
+ * This docstring was generated by AI.
321
+ */
176
322
public JSONObject getJson () {
177
323
return json ;
178
324
}
179
325
326
+ /**
327
+ * Sets the JSON object for the GraphDistance instance.
328
+ *
329
+ * @param json The JSON object to set.
330
+ *
331
+ * This docstring was generated by AI.
332
+ */
180
333
public void setJson (JSONObject json ) {
181
334
this .json = json ;
182
335
}
183
336
337
+ /**
338
+ * Returns the NSManager instance.
339
+ *
340
+ * @return The NSManager instance.
341
+ *
342
+ * This docstring was generated by AI.
343
+ */
184
344
public NSManager getNsm () {
185
345
return nsm ;
186
346
}
187
347
348
+ /**
349
+ * Sets the NSManager object for the GraphDistance instance.
350
+ *
351
+ * @param nsm The NSManager object to be set.
352
+ *
353
+ * This docstring was generated by AI.
354
+ */
188
355
public void setNsm (NSManager nsm ) {
189
356
this .nsm = nsm ;
190
357
}
191
358
359
+ /**
360
+ * Returns the mode used for fuzzy matching.
361
+ *
362
+ * @return The mode value.
363
+ *
364
+ * This docstring was generated by AI.
365
+ */
192
366
public Mode getMode () {
193
367
return mode ;
194
368
}
195
369
370
+ /**
371
+ * Sets the matching mode for URI labels and properties.
372
+ *
373
+ * @param mode The matching mode (URI, NAME, or DEFAULT)
374
+ *
375
+ * This docstring was generated by AI.
376
+ */
196
377
public void setMode (Mode mode ) {
197
378
this .mode = mode ;
198
379
}
0 commit comments