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.

  • 1
    How will the snippets be presented to the library? You can use e.g. github.com/github/linguist if the snippets are in files. – Neil Slater Jul 22 '14 at 7:17
  • 1
    Depending on the size of the snippet this can be almost impossible to do reliably, you could probably do as well with a few regex rules that look for defining characteristics. – Steve Barnes Jul 22 '14 at 20:54
  • 1
    Similar question on Stack Overflow – Gilles Jul 23 '14 at 7:24
  • 1
    @FranckDernoncourt Do you care in which language the actual library is written? – RockPaperLizard Nov 10 '14 at 11:45
  • 1
    @RockPaperLizard Not really, just if possible callable from Python/Java/C++ (descending order of preference). – Franck Dernoncourt Nov 10 '14 at 14:37
1

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

  • 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.

  • 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.

  • 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.