Introduction
This site is rapidly building up a huge dataset of code snippets, so let's do something with it!
Here's a data file. It contains 9,066 unique language+snippet pairs for 113 languages, all taken from this site. The format is tab-separated (language-TAB-snippet), with all newlines in the snippets replaced with <LF>
, and all tabs replaced with 4 spaces. There are at least 5 snippets for each language.
[update: I've made a minor change to the data file to merge some Python & RegExp versions I missed before - the link above has been updated]
Challenge
Write a program or function which takes a code snippet and outputs the language it is written in (see below for details). The total size of your source + any data you require must be 300 bytes or less, and your program must output the correct language when given its own source code. Highest accuracy (most correct answers on the dataset above) wins.
Rules
- The total size of your source code, resources, and any required compilation / runtime flags must not exceed 300 bytes.
- Your answer will be tested against the dataset above; it will be given one of the "Snippet" values as input and its output will be compared against the "correct" output according to the dataset. This will be repeated for all entries in the dataset and the final number of correct answers is your score.
- You can choose the input encoding - I'll assume UTF-8, so if you need another encoding specify it in your answer.
- You don't need to use the
<LF>
replacement for newlines; if your entry expects to receive newlines as literal newlines (char 10), specify it in your answer. - Your entry must output the language it thinks the input snippet is written in. To avoid the need to compress lots of language strings, I'll allow mappings (If you want to output 3 for "Java", that's fine); just note the mappings in your answer.
- You can only have 1 output mapping for each language (i.e. if 3 means "Java", you can't also have 4 meaning "Java").
- When given its own source code, your program must produce the correct answer (must output the language it is written in).
- You don't need to support all languages in the dataset, and you can support extra languages if you want to (e.g. if your entry isn't in one of the languages in the dataset).
- Your program must be deterministic (providing the same input twice must produce the same output).
Tie-Breaking
- Ties will be decided by reducing the dataset until one entry wins. The dataset will be reduced by removing all snippets for the most popular language (i.e. ties are broken by accuracy on rarer languages). For example, if A and B score 70% on the full dataset, all Python snippets will be removed. If A and B now both score 60%, CJam will be removed. If A now scores 50% but B scores 55%, B is the winner.
- If 100% accuracy is achieved, ties will be decided using a second (blind) dataset containing more samples for the same languages.
Example 1
The Python script:
print("python")
This script successfully produces "python" when given its own source code, so it is valid. On the dataset, it scores 1008/9066 = 11.1%
Example 2
The JavaScript function:
function f(s){return /function/.test(s)?1:2}
With the mappings 1 → javascript, 2 → python. Again it successfully produces 1 ("javascript") for its own source, and on the dataset it scores 1092/9066 = 12.0%
Where did the data come from?
I created an SEDE query to pull samples from [code-golf] challenges on this site. From the resulting 10,000 answers, I used a hacked-together python script to find the code & language name for each, then filtered out any language with less than 5 examples. The data isn't 100% clean (I know there are some non-code snippets it pulled), but should be good enough.
Inspired by this challenge from earlier in the year: Who said that? 2016 Presidential election
Also partly related to What's the Language?