Show HN: Detecting the Programming Language of Code Using ML and Neural Networks
danielheres.space
danielheres.space
int main(int* x):
return *x + ((int)x);
This program is classified as being C (84% confidence), but if I change 'main' to 'elephant' it drops to 49% C, 41% Python. If I change the name to 'snake' it suddenly becomes Python code with 80% confidence. I guess Python programmers like really like talking about animals? :)This would be more interesting if it were able to accurately classify programs by what family of syntax they're most similar to (Lisp, ML, C, etc). Otherwise, at first glance this seems like another needless application of something complicated like neural networks, since languages already have a formal grammar describing their structure and reserved tokens/symbols that you could take advantage of.
I agree a lot would be possible using formal grammar indeed but it would be probably a lot of work to maintain all the parsers. Also some languages share a lot of the same syntax, so in some cases this may lead to ambiguity. Maybe you could use a combination of both approaches in that case.
That was the goal of the example, because it tests whether you're overfitting to the training data. The point of machine learning is to use the training data to help you make smart decisions in scenarios that you haven't seen before. In this case, I expected it always say it is more C than Python.
I wouldn't go so far as saying it's 'invalid'. It's a perfectly reasonable new language that the classifier hasn't encountered, yet whose style is heavily rooted in languages the classifier has been trained on.
If we require that all programs we feed it are a valid program in one of a handful of languages it expects, we could've just run a parser for each language on the given input and call it a day. Trying to classify a weird language like this is where the machine learning aspect of the work really shines.
#define def int
def main(def x) {
return x;
}The data would then really only be needed to guess the frequencies.
I am also not sure whether word bigrams would help a lot, but character level n-grams would be totally off my radar.
Any opinions on that?
#include <iostream>
class Logger {
public:
void log(std::string a) {
std::cout << a << std::endl;
}
};
int main() {
Logger console = Logger();
for (int i=0;i<10;i++) {
console.log("test");
console.log("test");
console.log("test");
console.log("test");
console.log("test");
console.log("test");
console.log("test");
console.log("test");
}
} x = [1,2,3]
x << 4
> haskell: 0.94, ruby: 0.03, swift: 0.01 x = [1,2,3]
x << 4
puts x
> ruby: 0.89, haskell: 0.10, swift: 0.00 for (int i = 0; i ? N; ++i) {
a[i]->val = i;
}
Is clearly either C or C++, but it's not valid C or C++ because "i ? N" is not a valid boolean expression in the for loop. I actually think your techniques would work on syntactically invalid code, but I'm curious if you tried. let x = {}
doesn't even register as JS.