11

I am looking for a library that can detect in which programming language a code snippet is written. We can make the assumption that the code snippet is written in one language only.

E.g. for natural languages, I use the Python library guess_language:

>>> from guess_language import guess_language
>>> guess_language("Ces eaux regorgent de renégats et de voleurs.")
'fr'
>>> guess_language("Tienes que seguir tu corazón.")
'es'
>>> guess_language("いいえ!忍者がいます")
'ja'
>>> not guess_language("??")
True

If possible, it should return the probabilities of belonging to each programming language (e.g. for one code snippet given as input, the ouput could be 94% MATLAB, 30% Python, 5% Java, etc). Ideally, callable from Python/Java/C++ (descending order of preference), and free.

9
3

Here's one github/linguist that used by github, a ruby library.

1
  • Could you please give a usage example? Thanks!
    – Nicolas Raoul
    Mar 14 '16 at 6:17
-1

Here's another one blackducksw/ohcount see the ohcount_detect_language function, its for C, also has Ruby and Python port.

1
  • Could you please give a usage example? Thanks!
    – Nicolas Raoul
    Mar 14 '16 at 6:17
-1

Another library isagalaev/highlight.js for JavaScript, see the highlightAuto function in highlight.js source, it gives 1 or 2 possible answer.

1
  • Could you please give a usage example? Thanks!
    – Nicolas Raoul
    Mar 14 '16 at 6:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.