Giter VIP home page Giter VIP logo

vscode-languagedetection's Introduction

vscode-languagedetection

An npm package that uses machine learning to detect source code languages. Powered by @yoeo's guesslang model!

Usage

First install it in your project:

npm install --save @vscode/vscode-languagedetection
# or using yarn
yarn add @vscode/vscode-languagedetection

Then instantiate a ModuleOperations and run the run the model on a string of code:

import { ModelOperations } from "@vscode/vscode-languagedetection";

const modulOperations = new ModelOperations();

const result = await modulOperations.runModel(`
function makeThing(): Thing {
    let size = 0;
    return {
        get size(): number {
        return size;
        },
        set size(value: string | number | boolean) {
        let num = Number(value);
        // Don't allow NaN and stuff.
        if (!Number.isFinite(num)) {
            size = 0;
            return;
        }
        size = num;
        },
    };
}
`);

which will give you the following in order of confidence:

[
  { languageId: 'ts', confidence: 0.48307517170906067 },
  { languageId: 'rs', confidence: 0.10045434534549713 },
  { languageId: 'js', confidence: 0.07833506911993027 },
  { languageId: 'c', confidence: 0.045049071311950684 },
  { languageId: 'lua', confidence: 0.044198162853717804 },
  { languageId: 'cpp', confidence: 0.03847603127360344 },
  { languageId: 'cs', confidence: 0.03298814222216606 },
  { languageId: 'mm', confidence: 0.02999635599553585 },
  { languageId: 'html', confidence: 0.01874217577278614 },
  { languageId: 'sql', confidence: 0.01811739057302475 },
  { languageId: 'swift', confidence: 0.01418407540768385 },
  { languageId: 'pl', confidence: 0.014126052148640156 },
  { languageId: 'md', confidence: 0.01112559624016285 },
  { languageId: 'java', confidence: 0.009976979345083237 },
  { languageId: 'ps1', confidence: 0.009242385625839233 },
  { languageId: 'php', confidence: 0.008150739595293999 },
  { languageId: 'go', confidence: 0.0069260732270777225 },
  { languageId: 'tex', confidence: 0.006594990845769644 },
  { languageId: 'scala', confidence: 0.00619362760335207 },
  { languageId: 'py', confidence: 0.004240741487592459 },
  { languageId: 'r', confidence: 0.0033439004328101873 },
  { languageId: 'matlab', confidence: 0.0030552551615983248 },
  { languageId: 'css', confidence: 0.0026798006147146225 },
  { languageId: 'sh', confidence: 0.0023688252549618483 },
  { languageId: 'ipynb', confidence: 0.002114647999405861 },
  { languageId: 'bat', confidence: 0.0018151027616113424 },
  { languageId: 'hs', confidence: 0.001677449094131589 },
  { languageId: 'erl', confidence: 0.0014191442169249058 },
  { languageId: 'coffee', confidence: 0.000696933304425329 },
  { languageId: 'rb', confidence: 0.0006357143283821642 }
]

Advanced options

By default, this library will work in Node.js. It uses the fs and path modules provided by Node.js to read in the model.json file and the weights file, group1-shard1of1.bin, that are contained in this repo.

You can overwrite that behavior using the first two optional parameters of ModelOperations:

modelJSONFunc?: () => Promise<any> // This must return a JSON.parse() object
weightsFunc?: () => Promise<ArrayBuffer>

These allow you to overwrite the model loading behavior of this package if you happen to be in a non-traditional environment. For an example of this, check out how VS Code is doing it.

The third parameter is the options bag that has:

  • minContentSize?: number - The minimum number of characters in a file to be considered for language detection. Defaults to 20.
  • maxContentSize?: number - The maximum number of characters that will be used in a file to be considered for language detection. Defaults to 100000.

Local development

To build from source, follow these steps:

  1. Clone the repository
  2. Run npm install
  3. Run npm run watch

To run the tests, simply run npm test.

To build a production package:

  1. Run npm run build
  2. Run npm pack

To publish this package, run npm publish.

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

Trademarks

This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft's Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies.

vscode-languagedetection's People

Contributors

joaomoreno avatar lszomoru avatar microsoft-github-operations[bot] avatar microsoftopensource avatar rzhao271 avatar tylerleonhardt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vscode-languagedetection's Issues

Paste JSON been detected as YAML

I paste the following JSON below:

{
    "java.home": "/usr/lib/jvm/java-11-openjdk-amd64",
    "java.debug.settings.hotCodeReplace": "auto",
    "java.configuration.runtimes": [
        {
            "name": "JavaSE-1.8",
            "path": "/usr/lib/jvm/java-8-openjdk-amd64/",
            "sources": "/usr/lib/jvm/java-8-openjdk-amd64/src.zip",
        },
        {
            "name": "JavaSE-11",
            "path": "/usr/lib/jvm/java-11-openjdk-amd64",
            "sources": "/usr/lib/jvm/java-11-openjdk-amd64/lib/src.zip",
            "javadoc": "https://docs.oracle.com/en/java/javase/11/docs/api",
            "default": true
        }
    ],
    "maven.terminal.useJavaHome": true,
    "editor.foldingImportsByDefault": true
}

The language detection think it's an YAML.

image

Auto detection for custom languages

I have an extension which provides some language support (folding and highlighting) for file trees. When opening an untitled file and pasting in a tree, the extension will try to set the language to tree if it has some specific characters (ex. ├──, └──). However, once language detection kicks in it automatically changes it to ini.

I was testing the language detection code and can clearly see that for a tree file it somehow detects ini with 100% confidence. So there are 2 questions:

  1. Is it a bug that this is being detected as an ini file?
  2. Is there any process for hooking into/add languages to the custom detection API?

I tested the language detection code with the below

const vsl = require("@vscode/vscode-languagedetection");
const modulOperations = new vsl.ModelOperations();
(async () => {
    const result = await modulOperations.runModel(`
    ├── functions
    ├── src
    │    ├── articles
    │    ├── components
    │    ├── constants
    │    ├── contexts
    │    ├── data
    │    ├── hooks
    │    ├── i18n
    │    │    └── locales
    │    ├── modals
    │    │    └── sections
    │    ├── pages
    │    │    ├── app
    │    │    └── r
    │    ├── styles
    │    ├── templates
    │    └── utils
    └── static
        └── images
            ├── screenshots
            └── templates
    `);
    console.log(result);
})();

Output:

image

Java and Groovy are so much alike

I paste a Java code, but VSCode recognize as a Groovy language. I think these two languages are almost the same, but how can I configure prefer Java over Groovy?

package com.example.demo4;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Demo4Application {

	public static void main(String[] args) {
		SpringApplication.run(Demo4Application.class, args);
	}

}

Tflite model

Maybe you should try tfjs-tflite which has better performance

Error accessing source map from npm package

When running vscode web (serverless) I get the following warning about a source map not found:
GET /static/build/node_modules/@vscode/vscode-languagedetection/dist/lib/index.js.map 404 0.946 ms - 9

Either add the map file to the npm package, or, better, disable source maps when building for production to avoid the reference

To reproduce:

  • git clone https://github.com/microsoft/vscode-test-web.git
  • npm i && npm run sample, open dev tools network tab, in VS code web open some files

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.