in reply to heuristic to detect (perl) code
use 5.010; use strict; use warnings; use File::Slurp qw(slurp); my $text = slurp(__FILE__); my $length = length $text; my $perlish = ($text =~ y(@$%;{}[]<>=~)//); my $metric = $perlish / $length; say "Metric is $metric"; if ($metric > 0.10) { say "Looks like code"; } elsif ($metric < 0.03) { say "Looks like text"; } else { say "Debatable"; }
I've only tried this on a few sample inputs, but it hasn't failed once. It correctly detects itself.
As you can see, it's a very simple metric, so should be trivial to port to Javascript or anything else.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: heuristic to detect (perl) code
by LanX (Saint) on Jan 19, 2013 at 12:03 UTC | |
by 7stud (Deacon) on Jan 20, 2013 at 09:22 UTC | |
by Anonymous Monk on Jan 20, 2013 at 10:25 UTC | |
by LanX (Saint) on Jan 20, 2013 at 10:46 UTC |