Skip to content

Instantly share code, notes, and snippets.

@harthur
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harthur/8bda4439b76e7ca19587 to your computer and use it in GitHub Desktop.
Save harthur/8bda4439b76e7ca19587 to your computer and use it in GitHub Desktop.
Neural network indentation detection
var brain = require("brain");
// initalize network from previously trained state
var trained = {}; // Too big to display! see https://gist.github.com/harthur/3d5c24a04e509284940d
var network = new brain.NeuralNetwork().fromJSON(trained);
function detectIndent(lines) {
// get widths used in file and what % of file
var percentages = getWidthPercentages(lines);
var results = network.run(percentages);
// from results, pick indent with the highest signal
var max = 0,indent = null;
for (var width in results) {
var prob = results[width];
if (prob > max) {
indent = width;
max = prob;
}
}
return indent;
}
/**
* Get indent widths used, and the % of lines using each.
* Return ex: {2: 0.33, 4: 0.51, 6: 0.13, 8: 0.03, 17: 0.01}
*/
function getWidthPercentages(lines) {
var counts = {}; // # spaces -> # lines with that width
var total = 0; // # of indented lines
lines.forEach(function (text) {
var width = leadingSpaces(text);
if (width > 0) {
counts[width] = (counts[width] || 0) + 1;
total++;
}
});
// turn counts into % of lines
for (var width in counts) {
counts[width] = counts[width] / total;
}
return counts;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment