I before E except after C: Difference between revisions

→‎{{header|MATLAB}}: Provided a full solution in MATLAB
(→‎{{header|Octave}}: Separated Octave code from MATLAB)
(→‎{{header|MATLAB}}: Provided a full solution in MATLAB)
Line 2,753:
</syntaxhighlight>
 
=={{header|OctaveMATLAB}}==
{{incomplete|MATLAB|Is the original phrase plausible?}}
 
<syntaxhighlight lang="matlab">function i_before_e_except_after_c(f)
function iBeforeE()
check('http://wiki.puzzlers.org/pub/wordlists/unixdict.txt');
fprintf('\n');
check('http://ucrel.lancs.ac.uk/bncfreq/lists/1_2_all_freq.txt');
end
 
function check(URL)
fid = fopen(f,'r');
fprintf('For %s:\n', URL)
nei = 0;
[~, name, ext] = fileparts(URL);
cei = 0;
niefn = 0[name ext];
if exist(fn,'file')
lines = readlines(fn, 'EmptyLineRule', 'skip');
else
fprintf('Reading data from %s\n', URL)
lines = readlines(URL, 'EmptyLineRule', 'skip');
% Save the file for later
writelines(lines,fn);
end
includesFrequencyData = length(split(lines(1))) > 1;
ie = 0;
cie = 0;
ei = 0;
while ~feof(fid)
cei = 0;
c = strsplit(strtrim(fgetl(fid)),char([9,32]));
for i = 1:size(lines,1)
if length(c) > 2,
if includesFrequencyData
n = str2num(c{3});
fields = split(strtrim(lines(i)));
else
if length(fields) ~= 3 || i == 1
n = 1;
continue;
end;
end
if strfind(c{1},'ei')>1, nei=nei+n; end;
word = fields(1);
if strfind(c{1},'cei'), cei=cei+n; end;
frequency = str2double(fields(3));
if strfind(c{1},'ie')>1, nie=nie+n; end;
else
if strfind(c{1},'cie'), cie=cie+n; end;
word = lines(i);
end;
frequency = 1;
fclose(fid);
end
ie = ie + length(strfind(word,'ie')) * frequency;
ei = ei + length(strfind(word,'ei')) * frequency;
cie = cie + length(strfind(word,'cie')) * frequency;
cei = cei + length(strfind(word,'cei')) * frequency;
end
rule1 = "I before E when not preceded by C";
p1 = reportPlausibility(rule1, ie-cie, ei-cei );
rule2 = "E before I when preceded by C";
p2 = reportPlausibility(rule2, cei, cie );
combinedRule = "I before E, except after C";
fprintf('Hence the combined rule \"%s\" is ', combinedRule);
if ~(p1 && p2)
fprintf('NOT ');
end
fprintf('PLAUSIBLE.\n');
end
 
function plausible = reportPlausibility(claim, positive, negative)
printf('cie: %i\nnie: %i\ncei: %i\nnei: %i\n',cie,nie-cie,cei,nei-cei);
vplausible = ''true;
fprintf('\"%s\" is ', claim);
if (nie < 3 * cie)
if positive <= 2*negative
v=' not';
plausible = false;
fprintf('NOT ')
end
fprintf('PLAUSIBLE,\n since the ratio of positive to negative examples is %d/%d = %0.2f.\n', positive, negative, positive/negative )
printf('I before E when not preceded by C: is%s plausible\n',v);
v = '';
if (nei > 3 * cei)
v=' not';
end
printf('E before I when preceded by C: is%s plausible\n',v);
</syntaxhighlight>
 
<pre>
<pre>octave:23> i_before_e_except_after_c 1_2_all_freq.txt
>> iBeforeE
cie: 994
For http://wiki.puzzlers.org/pub/wordlists/unixdict.txt:
nie: 8133
"I before E when not preceded by C" is PLAUSIBLE,
cei: 327
since the ratio of positive to negative examples is 466/217 = 2.15.
nei: 4274
I"E before EI when not preceded by C:" is plausibleNOT PLAUSIBLE,
since the ratio of positive to negative examples is 13/24 = 0.54.
E before I when preceded by C: is not plausible
Hence the combined rule "I before E, except after C" is NOT PLAUSIBLE.
octave:24> i_before_e_except_after_c unixdict.txt
 
cie: 24
For http://ucrel.lancs.ac.uk/bncfreq/lists/1_2_all_freq.txt:
nie: 464
"I before E when not preceded by C" is NOT PLAUSIBLE,
cei: 13
since the ratio of positive to negative examples is 8207/4826 = 1.70.
nei: 191
I"E before EI when not preceded by C:" is plausibleNOT PLAUSIBLE,
since the ratio of positive to negative examples is 327/994 = 0.33.
E before I when preceded by C: is not plausible</pre>
Hence the combined rule "I before E, except after C" is NOT PLAUSIBLE.
</pre>
 
=={{header|Modula-2}}==