-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathml-predict.js
More file actions
181 lines (155 loc) · 6.18 KB
/
Copy pathml-predict.js
File metadata and controls
181 lines (155 loc) · 6.18 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
(function (root, factory) {
const api = factory();
if (typeof module !== 'undefined' && module.exports) {
module.exports = api;
}
root.mlPredict = api;
})(typeof globalThis !== 'undefined' ? globalThis : this, function () {
function legacyDetectLanguage(text) {
if (typeof text !== 'string') return 'en';
const cleaned = text.toLowerCase();
const frenchWords = [
'poste', 'offre', 'emploi', 'entreprise', 'salaire', 'travail', 'vous', 'nous', 'votre', 'recrutement',
'candidature', 'missions', 'équipe', 'compétences', 'profil', 'contrat', 'développement', 'gestion', 'client',
'bonjour', 'bonjour', 'cordialement', 'référence', 'candidature', 'recruter'
];
const englishWords = [
'job', 'position', 'company', 'salary', 'team', 'skills', 'candidate', 'application', 'hiring', 'recruitment',
'experience', 'opportunity', 'role', 'please', 'contact', 'work', 'remote', 'full time', 'interview', 'offer'
];
const scoreFR = frenchWords.reduce((count, word) => count + (cleaned.includes(word) ? 1 : 0), 0);
const scoreEN = englishWords.reduce((count, word) => count + (cleaned.includes(word) ? 1 : 0), 0);
if (scoreEN > scoreFR) return 'en';
if (scoreFR > scoreEN) return 'fr';
const hasFrenchPunctuation = /[àâäéèêëïîôöùûüç]/.test(cleaned);
const hasEnglishStopWords = /\b(the|and|for|you|your|with|this|that|have|will|are|from|please|contact|company|position|salary)\b/.test(cleaned);
if (hasEnglishStopWords && !hasFrenchPunctuation) return 'en';
return 'fr';
}
function detectLanguageDetails(text) {
const cleaned = typeof text === 'string' ? text.toLowerCase() : '';
const frenchWords = ['poste', 'emploi', 'entreprise', 'salaire', 'travail', 'vous', 'nous', 'votre', 'recrutement', 'candidature', 'missions', 'profil', 'contrat', 'gestion', 'client', 'bonjour', 'recruter', 'recherchons', 'entretien'];
const englishWords = ['job', 'position', 'company', 'salary', 'team', 'skills', 'candidate', 'application', 'hiring', 'recruitment', 'experience', 'opportunity', 'role', 'please', 'contact', 'work', 'remote', 'interview', 'offer'];
const count = (word) => (cleaned.match(new RegExp(`\\b${word}\\b`, 'giu')) || []).length;
const scoreFR = frenchWords.reduce((total, word) => total + count(word), 0);
const scoreEN = englishWords.reduce((total, word) => total + count(word), 0);
let language;
if (scoreEN > scoreFR) language = 'en';
else if (scoreFR > scoreEN) language = 'fr';
else language = /\b(the|and|for|you|your|with|this|that|have|will|are|from)\b/i.test(cleaned) ? 'en' : 'fr';
const delta = Math.abs(scoreEN - scoreFR);
return { language, scoreFR, scoreEN, confidence: delta >= 2 ? 'high' : delta === 1 ? 'medium' : 'low' };
}
function detectLanguage(text) {
return detectLanguageDetails(text).language;
}
function preprocess(text) {
if (typeof text !== 'string') return '';
return text
.toLowerCase()
.replace(/[^\w\sàâäéèêëïîôöùûüç]/g, ' ')
.replace(/\s+/g, ' ')
.trim();
}
function tokenize(text, stopWords) {
const words = (text.match(/[\p{L}\p{N}_]+/gu) || []);
if (stopWords && stopWords.length > 0) {
return words.filter((m) => !stopWords.includes(m));
}
return words;
}
function genererNgrammes(mots) {
const bigrammes = [];
for (let i = 0; i < mots.length - 1; i++) {
bigrammes.push(mots[i] + ' ' + mots[i + 1]);
}
return [...mots, ...bigrammes];
}
function calculerTFIDF(text, model) {
const propre = preprocess(text);
if (!propre) {
return {};
}
const tokens = tokenize(propre, model.stop_words || []);
const ngrammes = genererNgrammes(tokens);
const compteur = {};
for (const n of ngrammes) {
compteur[n] = (compteur[n] || 0) + 1;
}
const vecteur = {};
for (const [terme, freq] of Object.entries(compteur)) {
const index = model.vocab[terme];
if (index !== undefined) {
vecteur[index] = freq * model.idf[index];
}
}
const norme = Math.sqrt(Object.values(vecteur).reduce((s, v) => s + v * v, 0));
if (norme > 0) {
for (const idx in vecteur) {
vecteur[idx] /= norme;
}
}
return vecteur;
}
function sigmoid(x) {
return 1 / (1 + Math.exp(-x));
}
function predictWithModel(text, model) {
const vecteur = calculerTFIDF(text, model);
let score = model.intercept;
for (const [idx, valeur] of Object.entries(vecteur)) {
score += model.coef[idx] * valeur;
}
return sigmoid(score);
}
let modelFR = null;
let modelEN = null;
async function loadModels() {
if (modelFR && modelEN) return;
if (typeof chrome === 'undefined' || !chrome.runtime || !chrome.runtime.getURL) {
const fs = require('fs');
const path = require('path');
const base = path.resolve(__dirname);
const [fr, en] = await Promise.all([
fs.promises.readFile(path.join(base, 'models', 'model_fr.json'), 'utf8'),
fs.promises.readFile(path.join(base, 'models', 'model_en.json'), 'utf8'),
]);
modelFR = JSON.parse(fr);
modelEN = JSON.parse(en);
return;
}
const [resFR, resEN] = await Promise.all([
fetch(chrome.runtime.getURL('models/model_fr.json')),
fetch(chrome.runtime.getURL('models/model_en.json')),
]);
if (!resFR.ok || !resEN.ok) {
throw new Error('Impossible de charger les modèles ML (fichiers JSON manquants ou corrompus)');
}
modelFR = await resFR.json();
modelEN = await resEN.json();
}
async function predictML(text) {
const normalized = preprocess(text);
if (!normalized) {
return { langue: 'fr', score: 0 };
}
await loadModels();
const languageDetails = detectLanguageDetails(text);
const lang = languageDetails.language;
const model = lang === 'fr' ? modelFR : modelEN;
const proba = predictWithModel(text, model);
return { langue: lang, score: Math.round(proba * 100), languageDetails };
}
return {
detectLanguage,
detectLanguageDetails,
preprocess,
tokenize,
genererNgrammes,
calculerTFIDF,
sigmoid,
predictWithModel,
loadModels,
predictML,
};
});