entity-predictor 1.2.2 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +28 -3
- package/index.d.ts +6 -0
- package/package.json +5 -2
- package/src/predictor.js +59 -0
package/README.md
CHANGED
|
@@ -63,7 +63,8 @@ Output:
|
|
|
63
63
|
{
|
|
64
64
|
entity: "STATE BANK OF INDIA",
|
|
65
65
|
confidence: 1,
|
|
66
|
-
confidenceLevel: "Trustable"
|
|
66
|
+
confidenceLevel: "Trustable",
|
|
67
|
+
input: "sbi"
|
|
67
68
|
}
|
|
68
69
|
*/
|
|
69
70
|
```
|
|
@@ -118,7 +119,31 @@ const predictor = new EntityPredictor(entities, {
|
|
|
118
119
|
});
|
|
119
120
|
```
|
|
120
121
|
|
|
121
|
-
### 6.
|
|
122
|
+
### 6. Redis Datasets Support
|
|
123
|
+
|
|
124
|
+
Load entities directly from a Redis source (requires your own redis client).
|
|
125
|
+
|
|
126
|
+
```javascript
|
|
127
|
+
import Redis from "ioredis"; // or any redis client
|
|
128
|
+
import { EntityPredictor } from "entity-predictor";
|
|
129
|
+
|
|
130
|
+
const redis = new Redis();
|
|
131
|
+
const predictor = new EntityPredictor(); // Start empty or with some local entities
|
|
132
|
+
|
|
133
|
+
// Load from a Redis String (JSON)
|
|
134
|
+
// Key content: '["Apple", {"name": "Google", "aliases": ["Alphabet"]}]'
|
|
135
|
+
await predictor.loadFromRedis(redis, { key: "my_entities", type: "json" });
|
|
136
|
+
|
|
137
|
+
// Load from a Redis Set
|
|
138
|
+
// Key content: SMEMBERS -> ["Tesla", "SpaceX"]
|
|
139
|
+
await predictor.loadFromRedis(redis, { key: "my_set_key", type: "set" });
|
|
140
|
+
|
|
141
|
+
// Load from a Redis Hash
|
|
142
|
+
// Key content: HGETALL -> { "Amazon": '["AWS"]', "Netflix": "FLIX" }
|
|
143
|
+
await predictor.loadFromRedis(redis, { key: "my_hash_key", type: "hash" });
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### 7. Add Entities Dynamically
|
|
122
147
|
|
|
123
148
|
You can add new entities to an existing predictor instance.
|
|
124
149
|
|
|
@@ -141,7 +166,7 @@ predictor.addEntity("PUNJAB NATIONAL BANK", ["PNB"]);
|
|
|
141
166
|
|
|
142
167
|
- `input`: String to search for.
|
|
143
168
|
- `threshold`: (Optional) Minimum confidence score (default `0.6`).
|
|
144
|
-
- **Returns**: Best match object, `{ entity: "UNKNOWN", ... }` if no match found, or `null` if input is invalid.
|
|
169
|
+
- **Returns**: Best match object `{ entity: string, confidence: number, input: string, ... }`, `{ entity: "UNKNOWN", input: string, ... }` if no match found, or `null` if input is invalid.
|
|
145
170
|
|
|
146
171
|
### `predictTop(input, limit, threshold)`
|
|
147
172
|
|
package/index.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ export interface PredictionResult {
|
|
|
13
13
|
entity: string;
|
|
14
14
|
confidence: number;
|
|
15
15
|
confidenceLevel: "Trustable" | "High Confidence" | "Moderate Confidence" | "Low Confidence";
|
|
16
|
+
input: string;
|
|
16
17
|
}
|
|
17
18
|
|
|
18
19
|
export class EntityPredictor {
|
|
@@ -23,4 +24,9 @@ export class EntityPredictor {
|
|
|
23
24
|
predictTop(input: string, limit?: number, threshold?: number): PredictionResult[];
|
|
24
25
|
|
|
25
26
|
addEntity(entity: string | EntityOption): void;
|
|
27
|
+
|
|
28
|
+
loadFromRedis(
|
|
29
|
+
redisClient: any,
|
|
30
|
+
options: { key: string; type?: "json" | "set" | "hash" }
|
|
31
|
+
): Promise<void>;
|
|
26
32
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "entity-predictor",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Lightweight, Zero Dependency Node.js library for entity name prediction and normalization.",
|
|
5
5
|
"types": "index.d.ts",
|
|
6
6
|
"type": "module",
|
|
@@ -23,5 +23,8 @@
|
|
|
23
23
|
"index.d.ts",
|
|
24
24
|
"package.json"
|
|
25
25
|
],
|
|
26
|
-
"license": "MIT"
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"ioredis": "^5.8.2"
|
|
29
|
+
}
|
|
27
30
|
}
|
package/src/predictor.js
CHANGED
|
@@ -127,6 +127,7 @@ export class EntityPredictor {
|
|
|
127
127
|
entity: "UNKNOWN",
|
|
128
128
|
confidence: 0,
|
|
129
129
|
confidenceLevel: "Low Confidence",
|
|
130
|
+
input,
|
|
130
131
|
};
|
|
131
132
|
}
|
|
132
133
|
|
|
@@ -142,6 +143,10 @@ export class EntityPredictor {
|
|
|
142
143
|
return [];
|
|
143
144
|
}
|
|
144
145
|
|
|
146
|
+
if (this.searchCandidates.length === 0) {
|
|
147
|
+
return [];
|
|
148
|
+
}
|
|
149
|
+
|
|
145
150
|
const normalizedInput = this.normalize(input);
|
|
146
151
|
const matches = findBestMatch(normalizedInput, this.searchCandidates);
|
|
147
152
|
|
|
@@ -151,6 +156,7 @@ export class EntityPredictor {
|
|
|
151
156
|
entity: this.candidateToEntity[index],
|
|
152
157
|
confidence: Number(rating?.rating?.toFixed(2)),
|
|
153
158
|
confidenceLevel: this._getConfidenceLevel(rating?.rating),
|
|
159
|
+
input,
|
|
154
160
|
}))
|
|
155
161
|
.filter((m) => m.confidence >= threshold)
|
|
156
162
|
.sort((a, b) => b.confidence - a.confidence);
|
|
@@ -176,4 +182,57 @@ export class EntityPredictor {
|
|
|
176
182
|
if (rating >= 0.6) return "Moderate Confidence";
|
|
177
183
|
return "Low Confidence";
|
|
178
184
|
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Loads entities from a Redis source.
|
|
188
|
+
* @param {Object} redisClient - A Redis client instance (e.g., ioredis or node-redis).
|
|
189
|
+
* @param {Object} options - Loading options.
|
|
190
|
+
* @param {string} options.key - The Redis key to fetch.
|
|
191
|
+
* @param {'json'|'set'|'hash'} [options.type='json'] - The type of data structure in Redis.
|
|
192
|
+
* @returns {Promise<void>}
|
|
193
|
+
*/
|
|
194
|
+
async loadFromRedis(redisClient, { key, type = "json" }) {
|
|
195
|
+
if (!redisClient || typeof redisClient.get !== "function") {
|
|
196
|
+
throw new TypeError("Invalid Redis client provided.");
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if (type === "json") {
|
|
200
|
+
const data = await redisClient.get(key);
|
|
201
|
+
if (data) {
|
|
202
|
+
const parsed = JSON.parse(data);
|
|
203
|
+
if (Array.isArray(parsed)) {
|
|
204
|
+
parsed.forEach((item) => this.addEntity(item, true));
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
} else if (type === "set") {
|
|
208
|
+
// Assumes simple strings in set
|
|
209
|
+
if (typeof redisClient.smembers !== "function") {
|
|
210
|
+
throw new TypeError("Client does not support smembers.");
|
|
211
|
+
}
|
|
212
|
+
const members = await redisClient.smembers(key);
|
|
213
|
+
if (Array.isArray(members)) {
|
|
214
|
+
members.forEach((member) => this.addEntity(member, true));
|
|
215
|
+
}
|
|
216
|
+
} else if (type === "hash") {
|
|
217
|
+
// Key = Entity Name, Value = JSON string of aliases OR simple string
|
|
218
|
+
if (typeof redisClient.hgetall !== "function") {
|
|
219
|
+
throw new TypeError("Client does not support hgetall.");
|
|
220
|
+
}
|
|
221
|
+
const hash = await redisClient.hgetall(key);
|
|
222
|
+
if (hash) {
|
|
223
|
+
Object.entries(hash).forEach(([name, val]) => {
|
|
224
|
+
let aliases = [];
|
|
225
|
+
try {
|
|
226
|
+
const parsed = JSON.parse(val);
|
|
227
|
+
if (Array.isArray(parsed)) aliases = parsed;
|
|
228
|
+
else if (typeof parsed === "string") aliases = [parsed];
|
|
229
|
+
} catch (e) {
|
|
230
|
+
// value might be a simple comma-separated string or just raw string
|
|
231
|
+
aliases = [val];
|
|
232
|
+
}
|
|
233
|
+
this.addEntity({ name, aliases }, true);
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
179
238
|
}
|