entity-predictor 1.2.3 → 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 +25 -1
- package/index.d.ts +5 -0
- package/package.json +5 -2
- package/src/predictor.js +57 -0
package/README.md
CHANGED
|
@@ -119,7 +119,31 @@ const predictor = new EntityPredictor(entities, {
|
|
|
119
119
|
});
|
|
120
120
|
```
|
|
121
121
|
|
|
122
|
-
### 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
|
|
123
147
|
|
|
124
148
|
You can add new entities to an existing predictor instance.
|
|
125
149
|
|
package/index.d.ts
CHANGED
|
@@ -24,4 +24,9 @@ export class EntityPredictor {
|
|
|
24
24
|
predictTop(input: string, limit?: number, threshold?: number): PredictionResult[];
|
|
25
25
|
|
|
26
26
|
addEntity(entity: string | EntityOption): void;
|
|
27
|
+
|
|
28
|
+
loadFromRedis(
|
|
29
|
+
redisClient: any,
|
|
30
|
+
options: { key: string; type?: "json" | "set" | "hash" }
|
|
31
|
+
): Promise<void>;
|
|
27
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
|
@@ -143,6 +143,10 @@ export class EntityPredictor {
|
|
|
143
143
|
return [];
|
|
144
144
|
}
|
|
145
145
|
|
|
146
|
+
if (this.searchCandidates.length === 0) {
|
|
147
|
+
return [];
|
|
148
|
+
}
|
|
149
|
+
|
|
146
150
|
const normalizedInput = this.normalize(input);
|
|
147
151
|
const matches = findBestMatch(normalizedInput, this.searchCandidates);
|
|
148
152
|
|
|
@@ -178,4 +182,57 @@ export class EntityPredictor {
|
|
|
178
182
|
if (rating >= 0.6) return "Moderate Confidence";
|
|
179
183
|
return "Low Confidence";
|
|
180
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
|
+
}
|
|
181
238
|
}
|