I before E except after C: Difference between revisions

m
(→‎{{header|Vlang}}: Rename "Vlang" in "V (Vlang)")
m (→‎{{header|Wren}}: Minor tidy)
(9 intermediate revisions by 7 users not shown)
Line 740:
<pre>[^c]ie > [^c]ei -> 466 / 217 = 2.15 :: plausible
cei > cie -> 13 / 24 = 0.54 :: unsupported</pre>
 
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">rule1: {"I before E when not preceded by C"}
rule2: {"E before I when preceded by C"}
phrase: {"I before E except after C"}
 
plausibility: #[
false: "not plausible",
true: "plausible"
]
 
checkPlausible: function [rule, count1, count2][
result: count1 > 2 * count2
print ["The rule" rule "is" plausibility\[result] ":"]
print ["\tthere were" count1 "examples and" count2 "counter-examples."]
return result
]
 
words: read.lines relative "unixdict.txt"
 
[nie,cie,nei,cei]: 0
 
loop words 'word [
if contains? word "ie" ->
inc (contains? word "cie")? -> 'cie -> 'nie
if contains? word "ei" ->
inc (contains? word "cei")? -> 'cei -> 'nei
]
 
p1: checkPlausible rule1 nie nei
p2: checkPlausible rule2 cei cie
 
print ["\nSo the phrase" phrase "is" (to :string plausibility\[and? p1 p2]) ++ "."]</syntaxhighlight>
 
{{out}}
 
<pre>The rule "I before E when not preceded by C" is plausible :
there were 465 examples and 213 counter-examples.
The rule "E before I when preceded by C" is not plausible :
there were 13 examples and 24 counter-examples.
 
So the phrase "I before E except after C" is not plausible.</pre>
 
=={{header|AutoHotkey}}==
Line 959 ⟶ 1,001:
print "plausible."
end</syntaxhighlight>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> F%=OPENIN"unixdict.txt"
IF F% == 0 ERROR 100, "unixdict not found!"
 
CI=0 : XI=0 : CE=0 : XE=0
WHILE NOT EOF#F%
Line$=GET$#F%
P%=INSTR(Line$, "ie")
WHILE P%
IF MID$(Line$, P% - 1, 1) == "c" CI+=1 ELSE XI+=1
P%=INSTR(Line$, "ie", P% + 1)
ENDWHILE
P%=INSTR(Line$, "ei")
WHILE P%
IF MID$(Line$, P% - 1, 1) == "c" CE+=1 ELSE XE+=1
P%=INSTR(Line$, "ei", P% + 1)
ENDWHILE
ENDWHILE
CLOSE#F%
 
PRINT "Instances of 'ie', proceeded by a 'c' = ";CI
PRINT "Instances of 'ie', NOT proceeded by a 'c' = ";XI
P1%=XI * 2 > CI
PRINT "Therefore 'I before E when not preceded by C' is" FNTest(P1%)
PRINT
 
PRINT "Instances of 'ei', proceeded by a 'c' = ";CE
PRINT "Instances of 'ei', NOT proceeded by a 'c' = ";XE
P2%=CE * 2 > XE
PRINT "Therefore 'E before I when preceded by C' is" FNTest(P2%)
PRINT
 
IF P1% AND P2% PRINT "B"; ELSE PRINT "Not b";
PRINT "oth sub-phrases are plausible, therefore the phrase " +\
\ "'I before E, except after C' can be said to be" FNTest(P1% AND P2%) "!"
END
 
DEF FNTest(plausible%)=MID$(" not plausible", 1 - 4 * plausible%)</syntaxhighlight>
{{out}}
<pre>Instances of 'ie', proceeded by a 'c' = 24
Instances of 'ie', NOT proceeded by a 'c' = 466
Therefore 'I before E when not preceded by C' is plausible
 
Instances of 'ei', proceeded by a 'c' = 13
Instances of 'ei', NOT proceeded by a 'c' = 217
Therefore 'E before I when preceded by C' is not plausible
 
Not both sub-phrases are plausible, therefore the phrase 'I before E, except after C' can be said to be not plausible!</pre>
 
=={{header|BCPL}}==
Line 2,318 ⟶ 2,412:
1.68255</syntaxhighlight>
 
Note that we might also want to consider non-adjacent matches (the regular expression 'i.*e' instead of 'ie' or perhaps 'c.*ie' or 'c.*i.*e' instead of 'cie') - this would be straightforward to check, but this would bulk up the page. (And, to be meaningful, we'd want a more constrained wildcard than <code>.*</code> -- at the very least we would not want to span words.)
 
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
</syntaxhighlight>
<syntaxhighlight lang="java">
public static void main(String[] args) throws URISyntaxException, IOException {
count();
System.out.printf("%-10s %,d%n", "total", total);
System.out.printf("%-10s %,d%n", "'cei'", cei);
System.out.printf("%-10s %,d%n", "'cie'", cie);
System.out.printf("%,d > (%,d * 2) = %b%n", cei, cie, cei > (cie * 2));
System.out.printf("%,d > (%,d * 2) = %b", cie, cei, cie > (cei * 2));
}
 
static int total = 0;
static int cei = 0;
static int cie = 0;
 
static void count() throws URISyntaxException, IOException {
URL url = new URI("http://wiki.puzzlers.org/pub/wordlists/unixdict.txt").toURL();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()))) {
String line;
while ((line = reader.readLine()) != null) {
if (line.matches(".*?(?:[^c]ie|cei).*")) {
cei++;
} else if (line.matches(".*?(?:[^c]ei|cie).*")) {
cie++;
}
total++;
}
}
}
</syntaxhighlight>
<pre>
total 25,104
'cei' 477
'cie' 215
477 > (215 * 2) = true
215 > (477 * 2) = false
</pre>
<br />
An alternate demonstration<br>
Download and save wordlist to unixdict.txt.
 
Line 2,711 ⟶ 2,851:
</syntaxhighlight>
 
=={{header|MATLAB}} / {{header|Octave}}==
{{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);
fn = [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;
cei = 0;
for i = 1:size(lines,1)
nie = 0;
if includesFrequencyData
cie = 0;
fields = split(strtrim(lines(i)));
while ~feof(fid)
if length(fields) ~= 3 || i == 1
c = strsplit(strtrim(fgetl(fid)),char([9,32]));
continue;
if length(c) > 2,
end
n = str2num(c{3});
word = fields(1);
else
frequency = str2double(fields(3));
n = 1;
else
end;
word = lines(i);
if strfind(c{1},'ei')>1, nei=nei+n; end;
frequency = 1;
if strfind(c{1},'cei'), cei=cei+n; end;
end
if strfind(c{1},'ie')>1, nie=nie+n; end;
if ie = ie + length(strfind(c{1}word,'cieie'), ) cie=cie+n;* endfrequency;
ei = ei + length(strfind(word,'ei')) * frequency;
end;
cie = cie + length(strfind(word,'cie')) * frequency;
fclose(fid);
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}}==
Line 2,988 ⟶ 3,159:
(To be plausible, one word count must exceed another by 2 times)
</pre>
 
=={{header|Octave}}==
{{incomplete|MATLAB|Is the original phrase plausible?}}
 
<syntaxhighlight lang="matlab">function i_before_e_except_after_c(f)
 
fid = fopen(f,'r');
nei = 0;
cei = 0;
nie = 0;
cie = 0;
while ~feof(fid)
c = strsplit(strtrim(fgetl(fid)),char([9,32]));
if length(c) > 2,
n = str2num(c{3});
else
n = 1;
end;
if strfind(c{1},'ei')>1, nei=nei+n; end;
if strfind(c{1},'cei'), cei=cei+n; end;
if strfind(c{1},'ie')>1, nie=nie+n; end;
if strfind(c{1},'cie'), cie=cie+n; end;
end;
fclose(fid);
 
printf('cie: %i\nnie: %i\ncei: %i\nnei: %i\n',cie,nie-cie,cei,nei-cei);
v = '';
if (nie < 3 * cie)
v=' not';
end
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>octave:23> i_before_e_except_after_c 1_2_all_freq.txt
cie: 994
nie: 8133
cei: 327
nei: 4274
I before E when not preceded by C: is plausible
E before I when preceded by C: is not plausible
octave:24> i_before_e_except_after_c unixdict.txt
cie: 24
nie: 464
cei: 13
nei: 191
I before E when not preceded by C: is plausible
E before I when preceded by C: is not plausible</pre>
 
=={{header|Perl}}==
Line 4,212 ⟶ 4,435:
(To be plausible, one word count must exceed another by 2 times)
</pre>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program i_before_e_except_after_c;
init cie := 0, xie := 0, cei := 0, xei := 0;
 
dict := open("unixdict.txt", "r");
loop doing word := getline(dict); while word /= om do
classify(word);
end loop;
close(dict);
 
p :=
plausible("I before E when not preceded by C", xie, cie) and
plausible("E before I when preceded by C", cei, xei);
print;
print("I before E, except after C:" + (if p then "" else " not" end)
+ " plausible.");
 
proc classify(word);
if "ie" in word then
if "cie" in word then cie +:= 1;
else xie +:= 1;
end if;
elseif "ei" in word then
if "cei" in word then cei +:= 1;
else xei +:= 1;
end if;
end if;
end proc;
 
proc plausible(clause, feature, opposite);
p := 2 * feature > opposite;
print(clause + ":" + (if p then "" else " not" end) + " plausible.");
return p;
end proc;
end program;</syntaxhighlight>
{{out}}
<pre>I before E when not preceded by C: plausible.
E before I when preceded by C: not plausible.
 
I before E, except after C: not plausible.</pre>
 
=={{header|Swift}}==
Line 4,275 ⟶ 4,539:
E before I when preceded by C is not plausable
I before E except after C is not plausible</pre>
 
 
=={{header|True BASIC}}==
Line 4,455 ⟶ 4,718:
Do While Read (a)
w = Tok(0)
If FUNC(_Search(w, Dup("cei"))) > -1 Then x = x + 1
If FUNC(_Search(w, Dup("cie"))) > -1 Then y = y + 1
If FUNC(_Search(w, Dup("ie"))) > -1 Then p = p + 1
If FUNC(_Search(w, Dup("ei"))) > -1 Then q = q + 1
Loop
 
Line 4,817 ⟶ 5,080:
 
Also there are seven words which fall into two categories and which have therefore been double-counted.
<syntaxhighlight lang="ecmascriptwren">import "io" for File
import "./pattern" for Pattern
import "./fmt" for Fmt
 
var yesNo = Fn.new { |b| (b) ? "yes" : "no" }
Line 4,899 ⟶ 5,162:
And the code and results for the 'stretch goal' which has just the one double-counted word:
 
<syntaxhighlight lang="ecmascriptwren">import "io" for File
import "./pattern" for Pattern
import "./fmt" for Fmt
 
var yesNo = Fn.new { |b| (b) ? "yes" : "no" }
Line 4,978 ⟶ 5,241:
Plausible overall: no
</pre>
 
 
=={{header|Yabasic}}==
9,476

edits