-
Notifications
You must be signed in to change notification settings - Fork 27.9k
/
languageDetectionSimpleWorker.ts
268 lines (233 loc) · 8.56 KB
/
languageDetectionSimpleWorker.ts
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import type { ModelOperations, ModelResult } from '@vscode/vscode-languagedetection';
import { StopWatch } from 'vs/base/common/stopwatch';
import { IRequestHandler } from 'vs/base/common/worker/simpleWorker';
import { EditorSimpleWorker } from 'vs/editor/common/services/editorSimpleWorker';
import { IEditorWorkerHost } from 'vs/editor/common/services/editorWorkerHost';
type RegexpModel = { detect: (inp: string, langBiases: Record<string, number>) => string | undefined };
/**
* Called on the worker side
* @internal
*/
export function create(host: IEditorWorkerHost): IRequestHandler {
return new LanguageDetectionSimpleWorker(host, null);
}
/**
* @internal
*/
export class LanguageDetectionSimpleWorker extends EditorSimpleWorker {
private static readonly expectedRelativeConfidence = 0.2;
private static readonly positiveConfidenceCorrectionBucket1 = 0.05;
private static readonly positiveConfidenceCorrectionBucket2 = 0.025;
private static readonly negativeConfidenceCorrection = 0.5;
private _regexpModel: RegexpModel | undefined;
private _regexpLoadFailed: boolean = false;
private _modelOperations: ModelOperations | undefined;
private _loadFailed: boolean = false;
public async detectLanguage(uri: string, langBiases: Record<string, number> | undefined, preferHistory: boolean): Promise<string | undefined> {
const languages: string[] = [];
const confidences: number[] = [];
const stopWatch = new StopWatch(true);
const documentTextSample = this.getTextForDetection(uri);
if (!documentTextSample) { return; }
const neuralResolver = async () => {
for await (const language of this.detectLanguagesImpl(documentTextSample)) {
languages.push(language.languageId);
confidences.push(language.confidence);
}
stopWatch.stop();
if (languages.length) {
this._host.fhr('sendTelemetryEvent', [languages, confidences, stopWatch.elapsed()]);
return languages[0];
}
return undefined;
};
const historicalResolver = async () => {
// only detect when we have at least a line of data
if (langBiases) {
if (documentTextSample.length > 20 || documentTextSample.includes('\n')) {
const regexpDetection = await this.runRegexpModel(documentTextSample, langBiases);
if (regexpDetection) {
return regexpDetection;
}
}
}
return undefined;
};
if (preferHistory) {
const history = await historicalResolver();
if (history) { return history; }
const neural = await neuralResolver();
if (neural) { return neural; }
} else {
const neural = await neuralResolver();
if (neural) { return neural; }
const history = await historicalResolver();
if (history) { return history; }
}
return undefined;
}
private getTextForDetection(uri: string): string | undefined {
const editorModel = this._getModel(uri);
if (!editorModel) { return; }
const end = editorModel.positionAt(10000);
const content = editorModel.getValueInRange({
startColumn: 1,
startLineNumber: 1,
endColumn: end.column,
endLineNumber: end.lineNumber
});
return content;
}
private async getRegexpModel(): Promise<RegexpModel | undefined> {
if (this._regexpLoadFailed) {
return;
}
if (this._regexpModel) {
return this._regexpModel;
}
const uri: string = await this._host.fhr('getRegexpModelUri', []);
try {
this._regexpModel = await import(uri) as RegexpModel;
return this._regexpModel;
} catch (e) {
this._regexpLoadFailed = true;
// console.warn('error loading language detection model', e);
return;
}
}
private async runRegexpModel(content: string, langBiases: Record<string, number>): Promise<string | undefined> {
const regexpModel = await this.getRegexpModel();
if (!regexpModel) { return; }
const detected = regexpModel.detect(content, langBiases);
return detected;
}
private async getModelOperations(): Promise<ModelOperations> {
if (this._modelOperations) {
return this._modelOperations;
}
const uri: string = await this._host.fhr('getIndexJsUri', []);
const { ModelOperations } = await import(uri) as typeof import('@vscode/vscode-languagedetection');
this._modelOperations = new ModelOperations({
modelJsonLoaderFunc: async () => {
const response = await fetch(await this._host.fhr('getModelJsonUri', []));
try {
const modelJSON = await response.json();
return modelJSON;
} catch (e) {
const message = `Failed to parse model JSON.`;
throw new Error(message);
}
},
weightsLoaderFunc: async () => {
const response = await fetch(await this._host.fhr('getWeightsUri', []));
const buffer = await response.arrayBuffer();
return buffer;
}
});
return this._modelOperations!;
}
// This adjusts the language confidence scores to be more accurate based on:
// * VS Code's language usage
// * Languages with 'problematic' syntaxes that have caused incorrect language detection
private adjustLanguageConfidence(modelResult: ModelResult): ModelResult {
switch (modelResult.languageId) {
// For the following languages, we increase the confidence because
// these are commonly used languages in VS Code and supported
// by the model.
case 'javascript':
case 'html':
case 'json':
case 'typescript':
case 'css':
case 'python':
case 'xml':
case 'php':
modelResult.confidence += LanguageDetectionSimpleWorker.positiveConfidenceCorrectionBucket1;
break;
// case 'yaml': // YAML has been know to cause incorrect language detection because the language is pretty simple. We don't want to increase the confidence for this.
case 'cpp':
case 'shellscript':
case 'java':
case 'csharp':
case 'c':
modelResult.confidence += LanguageDetectionSimpleWorker.positiveConfidenceCorrectionBucket2;
break;
// For the following languages, we need to be extra confident that the language is correct because
// we've had issues like #131912 that caused incorrect guesses. To enforce this, we subtract the
// negativeConfidenceCorrection from the confidence.
// languages that are provided by default in VS Code
case 'bat':
case 'ini':
case 'makefile':
case 'sql':
// languages that aren't provided by default in VS Code
case 'csv':
case 'toml':
// Other considerations for negativeConfidenceCorrection that
// aren't built in but suported by the model include:
// * Assembly, TeX - These languages didn't have clear language modes in the community
// * Markdown, Dockerfile - These languages are simple but they embed other languages
modelResult.confidence -= LanguageDetectionSimpleWorker.negativeConfidenceCorrection;
break;
default:
break;
}
return modelResult;
}
private async * detectLanguagesImpl(content: string): AsyncGenerator<ModelResult, void, unknown> {
if (this._loadFailed) {
return;
}
let modelOperations: ModelOperations | undefined;
try {
modelOperations = await this.getModelOperations();
} catch (e) {
console.log(e);
this._loadFailed = true;
return;
}
let modelResults: ModelResult[] | undefined;
try {
modelResults = await modelOperations.runModel(content);
} catch (e) {
console.warn(e);
}
if (!modelResults
|| modelResults.length === 0
|| modelResults[0].confidence < LanguageDetectionSimpleWorker.expectedRelativeConfidence) {
return;
}
const firstModelResult = this.adjustLanguageConfidence(modelResults[0]);
if (firstModelResult.confidence < LanguageDetectionSimpleWorker.expectedRelativeConfidence) {
return;
}
const possibleLanguages: ModelResult[] = [firstModelResult];
for (let current of modelResults) {
if (current === firstModelResult) {
continue;
}
current = this.adjustLanguageConfidence(current);
const currentHighest = possibleLanguages[possibleLanguages.length - 1];
if (currentHighest.confidence - current.confidence >= LanguageDetectionSimpleWorker.expectedRelativeConfidence) {
while (possibleLanguages.length) {
yield possibleLanguages.shift()!;
}
if (current.confidence > LanguageDetectionSimpleWorker.expectedRelativeConfidence) {
possibleLanguages.push(current);
continue;
}
return;
} else {
if (current.confidence > LanguageDetectionSimpleWorker.expectedRelativeConfidence) {
possibleLanguages.push(current);
continue;
}
return;
}
}
}
}