4
\$\begingroup\$

The task is to to compete for the shortest regex (in bytes) in your preferred programming language which can distinguish between English and Spanish with minimum 60% 90% accuracy.

Silvio Mayolo's submission (pinned as Best Answer) has secured his spot as the winner of original contest against any chance of being contested. In order to provide room for further submissions, he has generously allowed the scoring requirement to be pushed to 90% accuracy.

Links to wordlists have been replaced due to concerns voiced in the comments.

The following word lists (based on these) must be used: English, Spanish

The Spanish wordlist is already transliterated into ASCII, and there is no word present in either which is also present in the other.

A naive approach to distinguishing Spanish from English might be to match if the word ends in a vowel:

[aeiou]$ i 9 bytes

Here's a live example, where 6 of 8 words are successfully identified, for 75% accuracy:

const regex = /[aeiou]$/i;

const words = [
  'hello',
  'hola',
  'world',
  'mundo',
  'foo',
  'tonto',
  'bar',
  'barra'
];

words.forEach(word => {
  const match = word.match(regex);
  const langs = ['English', 'Spanish'];
  const lang = langs[+!!match];
  console.log(word, lang);
});

\$\endgroup\$
  • 2
    \$\begingroup\$ Thanks, I like these changes. Could you please also make a link an ASCII-transliterated Spanish word list? I'm not clear though on how our accuracy is judged. Are we given a word from one of the two word lists (20,000 words total) and our regex judges if it's English of Spanish, with us choosing which one is "match" and "not match"? What about if the string happens to appears in both lists? \$\endgroup\$ – xnor Jul 22 at 23:04
  • 4
    \$\begingroup\$ My experience with length-accuracy trade-off scoring formulas is that they're almost always turn out unbalanced or exploitable by answers caring for pretty much just about the one or the other. Moreover, dividing by byte count is usually a bad idea that rewards ultra-short answers when the other factor has a cap. I'd suggest just putting a higher required goal accuracy. \$\endgroup\$ – xnor Jul 23 at 2:22
  • 3
    \$\begingroup\$ Regarding the Sandbox, it's good that you posted there first, but one day is too little time to gather feedback. Three days minimum is usually recommended by default, but with this being not a bog-standard code golf but a challenge with a test battery, I'd suggest a week or two at least. \$\endgroup\$ – xnor Jul 23 at 2:24
  • 2
    \$\begingroup\$ That metric may not quite be enough. A 2-byte answer can beat my answer if it has accuracy of 84% or above (which may be possible with 2 bytes, but I'm not hopeful). A 3-byte or more answer, even with 100% accuracy, can only get a 0.33 score, at most. I like this challenge, but I'm not sure what the right metric for it is, to be honest. \$\endgroup\$ – Silvio Mayolo Jul 23 at 2:24
  • 3
    \$\begingroup\$ I’m voting to close this question because "This contest is frozen until a reasonable scoring system can be decided on." \$\endgroup\$ – pppery Jul 23 at 2:52
21
\$\begingroup\$

Any Language, 0.3677 (60.6064%, 1 byte)

a

No, I'm not joking. The single-character regular expression a successfully identifies Spanish words over English given your input files 60.6064% of the time, which makes it a valid submission.

Here's a complete, runnable Perl script that checks the percentage of this regular expression, assuming you've downloaded english.json and spanish.json into the same folder as the script.

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

my @english;
my @spanish;

my $fh;
open $fh, '<', 'english.json';
while (<$fh>) {
    push @english, $1 if /"(\w+)"/;
}
close $fh;

open $fh, '<', 'spanish.json';
while (<$fh>) {
    push @spanish, $1 if /"(\w+)"/;
}
close $fh;

my $correct = 0;
my $total = 0;

my $re = qr/a/;

for (@english) {
    $total++;
    $correct++ unless /$re/;
}
for (@spanish) {
    $total++;
    $correct++ if /$re/;
}

say "$correct / $total (@{[100*$correct/$total]}%)";
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Good job! That's fascinating that just having the letter a makes a word significantly more likely to be Spanish as opposed to English. Also, maybe I should have consulted a linguist to review the OP before submission :P \$\endgroup\$ – GirkovArpa Jul 23 at 1:31
  • 5
    \$\begingroup\$ Well, can't imagine anybody's going to beat that... \$\endgroup\$ – mypetlion Jul 23 at 1:31
  • \$\begingroup\$ Also amazing how it just barely qualified by 0.6% percentage points. \$\endgroup\$ – GirkovArpa Jul 23 at 1:33
  • 3
    \$\begingroup\$ It's also the only letter that satisfies the criteria. The next best would be o, with 58.69%, then it drops way down to 54.08% with t. On the flipside, n occurs most uniformly, at 50.13% accuracy. \$\endgroup\$ – Silvio Mayolo Jul 23 at 1:40
  • 6
    \$\begingroup\$ I wrote a script to analyze common character sequences in the two files, in the hopes of finding a 3- or 4-character constant regex that would do it. Sure enough, my script came back with a, which made me chuckle. \$\endgroup\$ – Silvio Mayolo Jul 23 at 1:53

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.