66 lines
1.8 KiB
TypeScript
66 lines
1.8 KiB
TypeScript
import stringSimilarity from 'string-similarity';
|
|
|
|
export class FuzzySearchService {
|
|
_data: any = [];
|
|
_threshold: number;
|
|
|
|
private constructor(data, threshold) {
|
|
this._data = data;
|
|
this._threshold = threshold;
|
|
}
|
|
|
|
public static create(data, threshold = 0.7) {
|
|
return new FuzzySearchService(data, threshold);
|
|
}
|
|
|
|
matchSimilarProperty(property) {
|
|
const groupedData: Map<string, unknown[]> = new Map();
|
|
|
|
this.data.forEach((obj) => {
|
|
let found = false;
|
|
|
|
for (const [key, group] of groupedData.entries()) {
|
|
const representativeObj: any = group[0];
|
|
|
|
if (
|
|
this.isPropertySimilar(representativeObj[property], obj[property])
|
|
) {
|
|
group.push(obj);
|
|
groupedData.set(key, group);
|
|
found = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!found) {
|
|
// Crea una nueva clave en el mapa para un nuevo grupo
|
|
groupedData.set(obj.id, [obj]);
|
|
}
|
|
});
|
|
|
|
return groupedData;
|
|
}
|
|
|
|
matchExactProperty(property) {
|
|
// Implement your exact property matching logic here
|
|
// Return the matched property
|
|
}
|
|
|
|
matchExactProperties(properties) {
|
|
// Implement your exact properties matching logic here
|
|
// Return the matched properties
|
|
}
|
|
|
|
isPropertySimilar(property, otherProperty: string): boolean {
|
|
const similarity = stringSimilarity.compareTwoStrings(property, otherProperty);
|
|
return similarity >= this.threshold;
|
|
}
|
|
|
|
get data() {
|
|
return this._data;
|
|
}
|
|
|
|
get threshold() {
|
|
return this._threshold;
|
|
}
|
|
} |