-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautocomplete.js
More file actions
149 lines (132 loc) · 3.58 KB
/
autocomplete.js
File metadata and controls
149 lines (132 loc) · 3.58 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const fs = require("fs");
const path = require("path");
class TrieNode {
constructor() {
this.children = new Map();
this.isWord = false;
}
}
class Trie {
constructor() {
this.root = new TrieNode();
}
insert(word) {
let node = this.root;
for (const char of word) {
if (!node.children.has(char)) {
node.children.set(char, new TrieNode());
}
node = node.children.get(char);
}
node.isWord = true;
}
getWordsFrom(node, prefix, result, maxSuggestions) {
if (result.length >= maxSuggestions) return;
if (node.isWord) result.push(prefix);
for (const [char, child] of node.children) {
if (result.length >= maxSuggestions) break;
this.getWordsFrom(child, prefix + char, result, maxSuggestions);
}
}
autocomplete(prefix, maxSuggestions = 20) {
let node = this.root;
for (const char of prefix) {
if (!node.children.has(char)) return [];
node = node.children.get(char);
}
const result = [];
this.getWordsFrom(node, prefix, result, maxSuggestions);
return result;
}
}
/**
* Moteur d'autocomplétion basé sur la structure Trie
* Intégré dans l'API Algorith
*/
class AutocompleteEngine {
constructor(options = {}) {
const {
language = "fr",
dictionary = null,
maxSuggestions = 20,
} = options;
this.language = language;
this.maxSuggestions = maxSuggestions;
this.trie = new Trie();
this.seen = new Set();
this._loadInitialDictionary(dictionary);
}
_loadInitialDictionary(dictionary) {
if (Array.isArray(dictionary)) {
dictionary.forEach(word => this.addWord(word));
} else if (dictionary || this._getDefaultDictionaryPath(this.language)) {
const dictPath = dictionary || this._getDefaultDictionaryPath(this.language);
try {
if (fs.existsSync(dictPath)) {
const content = fs.readFileSync(dictPath, "utf-8");
let words;
// Support JSON et texte
try {
words = JSON.parse(content);
if (Array.isArray(words)) {
words.forEach(word => this.addWord(word));
}
} catch {
words = content.split("\n")
.map(w => w.trim().toLowerCase())
.filter(w => w.length > 0);
words.forEach(word => this.addWord(word));
}
}
} catch (error) {
// Ignore les erreurs de chargement du dictionnaire
}
}
}
_getDefaultDictionaryPath(lang) {
const langMap = {
fr: path.join(__dirname, "dictionaries", "fr.json"),
en: path.join(__dirname, "dictionaries", "en.json"),
};
return langMap[lang] || null;
}
/**
* Ajoute un mot au dictionnaire
*/
addWord(word) {
if (!word || typeof word !== 'string') return;
word = word.toLowerCase().trim();
if (word.length > 0 && !this.seen.has(word)) {
this.trie.insert(word);
this.seen.add(word);
}
}
/**
* Autocomplète à partir d'un préfixe
*/
autocomplete(prefix) {
if (!prefix || typeof prefix !== 'string') return [];
return this.trie.autocomplete(prefix.toLowerCase(), this.maxSuggestions);
}
/**
* Alias pour la méthode autocomplete (compatibilité)
*/
search(prefix) {
return this.autocomplete(prefix);
}
/**
* Ajoute plusieurs mots d'un coup
*/
addWords(words) {
if (Array.isArray(words)) {
words.forEach(word => this.addWord(word));
}
}
/**
* Retourne le nombre de mots dans le dictionnaire
*/
getWordCount() {
return this.seen.size;
}
}
module.exports = AutocompleteEngine;