Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Language detection #119325

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

export interface ILanguageDetectionService {
readonly _serviceBrand: undefined;

detectLanguage(contet: string): Promise<string | undefined>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: contet -> content

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { ILanguageDetectionService } from 'vs/workbench/services/languageDetection/common/languageDetection';
// import * as tf from '@tensorflow/tfjs-node';
// import { TFSavedModel } from '@tensorflow/tfjs-node/dist/saved_model';

enum ModelLangToLangId {
bat = 'bat',
cmd = 'bat',
btm = 'bat',
c = 'c',
cs = 'csharp',
cpp = 'cpp',
cc = 'cpp',
coffee = 'coffeescript',
litcoffee = 'coffeescript',
css = 'css',
erl = 'erlang',
hrl = 'erlang',
go = 'go',
hs = 'haskell',
lhs = 'haskell',
html = 'html',
java = 'java',
js = 'javascript',
es6 = 'javascript',
ipynb = 'jupyter',
lua = 'lua',
md = 'markdown',
matlab = 'matlab',
m = 'objective-c',
mm = 'objective-c',
pl = 'perl',
pm = 'perl',
php = 'php',
ps1 = 'powershell',
py = 'python',
r = 'r',
rdata = 'r',
rds = 'r',
rda = 'r',
rb = 'ruby',
rs = 'rust',
scala = 'scala',
sh = 'shellscript',
sql = 'sql',
swift = 'swift',
tex = 'tex',
ts = 'typescript',
tsx = 'typescriptreact'
}

class LanguageDetectionService implements ILanguageDetectionService {
declare readonly _serviceBrand: undefined;
private _model: TFSavedModel | undefined;

constructor() { }

async detectLanguage(content: string): Promise<string | undefined> {
const prediction = await this.predict(content);
return prediction.confidence > 0.8 ? prediction.languageId : undefined;
}

private async getModel(): TFSavedModel {
if (this._model) {
this._model = await tf.node.loadSavedModel('model', ['serve'], 'serving_default');
}

return this._model;
}

private async predict(content: string): Promise<{ languageId: ModelLangToLangId; confidence: number }> {
// call out to the model
const model = await this.getModel();
const predicted = model.predict(tf.tensor([content]));

const langs: Array<keyof typeof ModelLangToLangId> = (predicted as tf.Tensor<tf.Rank>[])[0].dataSync() as any;
const probabilities = (predicted as tf.Tensor<tf.Rank>[])[1].dataSync() as Float32Array;

let maxIndex = 0;
for (let i = 0; i < probabilities.length; i++) {
if (probabilities[i] > probabilities[maxIndex]) {
maxIndex = i;
}
}

return {
languageId: ModelLangToLangId[langs[maxIndex]],
confidence: probabilities[maxIndex]
};
}
}