-
Notifications
You must be signed in to change notification settings - Fork 29.5k
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
Closed
Language detection #119325
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
src/vs/workbench/services/languageDetection/common/languageDetection.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
96 changes: 96 additions & 0 deletions
96
src/vs/workbench/services/languageDetection/node/languageDetectionService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
}; | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo: contet -> content