Jump to content

I before E except after C: Difference between revisions

Line 587:
(To be plausible, one word count must exceed another by 2 times)
</pre>
 
=={{header|Perl}}==
<lang perl>#!/usr/bin/perl
use warnings;
use strict;
 
sub result {
my ($support, $against) = @_;
my $ratio = sprintf '%.2f', $support / $against;
my $result = $ratio >= 2;
print "$support / $against = $ratio. ", 'NOT ' x !$result, "PLAUSIBLE\n";
return $result;
}
 
my @keys = qw(ei cei ie cie);
my %count;
 
while (<>) {
for my $k (@keys) {
$count{$k}++ if -1 != index $_, $k;
}
}
 
my ($support, $against, $result);
 
print 'I before E when not preceded by C: ';
$support = $count{ie} - $count{cie};
$against = $count{ei} - $count{cei};
$result += result($support, $against);
 
print 'E before I when preceded by C: ';
$support = $count{cei};
$against = $count{cie};
$result += result($support, $against);
 
print 'Overall: ', 'NOT ' x ($result < 2), "PLAUSIBLE.\n";</lang>
 
Output:
 
<pre>I before E when not preceded by C: 465 / 213 = 2.18. PLAUSIBLE
E before I when preceded by C: 13 / 24 = 0.54. NOT PLAUSIBLE
Overall: NOT PLAUSIBLE.</pre>
 
===Perl: Stretch Goal===
Just replace the while loop with the following one:
<lang perl>while (<>) {
my @columns = split;
next if 3 < @columns;
my ($word, $freq) = @columns[0, 2];
for my $k (@keys) {
$count{$k} += $freq if -1 != index $word, $k;
}
}</lang>
Output:
<pre>I before E when not preceded by C: 8148 / 4826 = 1.69. NOT PLAUSIBLE
E before I when preceded by C: 327 / 994 = 0.33. NOT PLAUSIBLE
Overall: NOT PLAUSIBLE.</pre>
 
=={{header|Python}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.