Skip to content

Instantly share code, notes, and snippets.

@jdkato
Created September 3, 2016 22:37
Show Gist options
  • Save jdkato/edf5f899943c725adae8bd8f50dfee7d to your computer and use it in GitHub Desktop.
Save jdkato/edf5f899943c725adae8bd8f50dfee7d to your computer and use it in GitHub Desktop.
var walk = require('walk')
var fs = require('fs')
var detectLang = require('lang-detector')
var supported = [
'javascript', 'c', 'c++', 'python', 'java', 'html', 'css', 'ruby', 'go', 'php'
]
var ext2Lang = {
'csharp': 'C#', 'gcc': 'C', 'gpp': 'C++', 'ghc': 'Haskell', 'jruby': 'Ruby',
'python3': 'Python', 'hack': 'PHP', 'yarv': 'Ruby', 'C-sharp': 'C#'
}
var count = 0.0
var correct = 0
// Or path to benchmark data (https://github.com/nbraud/benchmarksgame/tree/master/bench)
var walker = walk.walk('./test', { followLinks: false })
walker.on('file', function (root, stat, next) {
var path = root + '/' + stat.name
var lang = path.split('.').slice(-1)[0]
if (lang in ext2Lang) {
lang = ext2Lang[lang]
}
if (supported.indexOf(lang.toLowerCase()) > 0) {
fs.readFile(path, {
encoding: 'utf8'
}, function (err, code) {
if (err) {
throw err
}
count += 1
var out = detectLang(code)
if (out.toLowerCase() === lang.toLowerCase()) {
correct += 1
} else {
console.log(out, lang, path)
}
})
}
next()
})
walker.on('end', function () {
console.log(count, correct)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment