I before E except after C: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 36:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V PLAUSIBILITY_RATIO = 2
 
F plausibility_check(comment, x, y)
Line 66:
print(‘Checking plausibility of "I before E except after C":’)
V (cei, cie, not_c_ie, not_c_ei) = simple_stats()
print_result(cei, cie, not_c_ie, not_c_ei)</langsyntaxhighlight>
 
{{out}}
Line 88:
(Indeed, <code>unixdict.txt</code> is 206k.)
 
<langsyntaxhighlight lang="8080asm"> ;;; I before E, except after C
fcb1: equ 5Ch ; FCB 1 (populated by file on command line)
dma: equ 80h ; Standard DMA location
Line 294:
cei: dw 0 ; E before I when preceded by C
xei: dw 0 ; E before I when not preceded by C
curwrd: equ $ ; Current word stored here</langsyntaxhighlight>
 
{{out}}
Line 308:
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}} Uses non-standard procedure to lower available in Algol 68G.
<langsyntaxhighlight lang="algol68"># tests the plausibility of "i before e except after c" using unixdict.txt #
 
# implements the plausibility test specified by the task #
Line 387:
show plausibility( "i before e in general", xie + cie, xei + cei );
show plausibility( "e before i in general", xei + cei, xie + cie )
FI</langsyntaxhighlight>
{{out}}
<pre>
Line 407:
===Vanilla===
 
<langsyntaxhighlight lang="applescript">on ibeeac()
script o
property wordList : words of (read file ((path to desktop as text) & "www.rosettacode.org:unixdict.txt") as «class utf8»)
Line 447:
end ibeeac
 
ibeeac()</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">{|"I before E not after C" is plausible|:true, |"E before I after C" is plausible|:false, |Both are plausible|:false}</langsyntaxhighlight>
 
===AppleScriptObjC===
 
<langsyntaxhighlight lang="applescript">use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
use scripting additions
Line 483:
end ibeeac
 
ibeeac()</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">{|"I before E not after C" is plausible|:true, |"E before I after C" is plausible|:false, |Both are plausible|:false}</langsyntaxhighlight>
 
 
===Functional===
<langsyntaxhighlight lang="applescript">use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
Line 736:
end tell
end if
end zipWith</langsyntaxhighlight>
{{Out}}
<pre>[^c]ie > [^c]ei -> 466 / 217 = 2.15 :: plausible
Line 742:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">WordList := URL_ToVar("http://wiki.puzzlers.org/pub/wordlists/unixdict.txt")
WordList := RegExReplace(WordList, "i)cie", "", cieN)
WordList := RegExReplace(WordList, "i)cei", "", ceiN)
Line 763:
WebRequest.Send()
return, WebRequest.ResponseText
}</langsyntaxhighlight>
{{out}}
<pre>"I before E when not preceded by C" is plausible.
Line 774:
 
=={{header|AWK}}==
<langsyntaxhighlight lang="awk">#!/usr/bin/awk -f
 
/.ei/ {nei+=cnt($3)}
Line 799:
print "E before I when preceded by C: is"v2" plausible";
print "Overall rule is"v" plausible";
}</langsyntaxhighlight>
 
Usage:
Line 821:
=={{header|Batch File}}==
Download first the text file, then put it on the same directory with this sample code:
<langsyntaxhighlight lang="dos">::I before E except after C task from Rosetta Code Wiki
::Batch File Implementation
 
Line 857:
 
pause
exit /b 0</langsyntaxhighlight>
{{Out}}
<pre>Plausibility of "I before E when not preceded by C": TRUE (465 VS 213)
Line 867:
Each word is counted once if word has at least one occurrence of test string (word with 2 or more occurrences only counts once).
The same word may count toward different categories.
<langsyntaxhighlight lang="dos">@echo off
setlocal enableDelayedExpansion
for /f %%A in ('findstr /i "^ie [^c]ie" unixdict.txt ^| find /c /v ""') do set Atrue=%%A
Line 877:
echo I before E when not preceded by C: True=%Atrue% False=%Afalse% : !Answer%Aresult%!
echo E before I when preceded by C: True=%Btrue% False=%Bfalse% : !Answer%Bresult%!
echo I before E, except after C : !Answer%Result%!</langsyntaxhighlight>
{{Out}}
<pre>I before E when not preceded by C: True=465 False=213 : Plausible
Line 886:
Each word frequency is included once if word has at least one occurrence of test string (word with 2 or more occurrences only counts once).
The same word frequency may count toward different categories.
<langsyntaxhighlight lang="dos">@echo off
setlocal enableDelayedExpansion
set /a Atrue=Afalse=Btrue=Bfalse=0
Line 897:
echo I before E when not preceded by C: True=%Atrue% False=%Afalse% : !Answer%Aresult%!
echo E before I when preceded by C: True=%Btrue% False=%Bfalse% : !Answer%Bresult%!
echo I before E, except after C : !Answer%Result%!</langsyntaxhighlight>
{{Out}}
<pre>I before E when not preceded by C: True=8192 False=4826 : Implausible
Line 904:
 
=={{header|BASIC}}==
<langsyntaxhighlight lang="basic">10 DEFINT A-Z
20 OPEN "I",1,"UNIXDICT.TXT": GOTO 60
30 LINE INPUT #1,W$
Line 920:
150 PRINT "E before I when preceded by C: ";
160 IF 2*CE <= XE THEN PRINT "not ";
170 PRINT "plausible."</langsyntaxhighlight>
{{out}}
<pre>CIE: 24
Line 933:
=={{header|BASIC256}}==
{{trans|BASIC}}
<langsyntaxhighlight lang="freebasic">CI = 0 : XI = 0 : CE = 0 : XE = 0
open 1, "unixdict.txt"
 
Line 958:
if 2 * CE <= XE then print "not ";
print "plausible."
end</langsyntaxhighlight>
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
 
// Read word from selected input
Line 1,014:
writef("E before I when preceded by C: %Splausible.*N",
2*ncei > nxei -> "", "not ")
$)</langsyntaxhighlight>
{{out}}
<pre>CIE: 24
Line 1,028:
This may in turn motivate me to provide a second J solution as a single pass FSM.
Please find the program output hidden at the top of the source as part of the build and example run.
<syntaxhighlight lang="c">
<lang c>
%{
/*
Line 1,061:
return 0;
}
</syntaxhighlight>
</lang>
 
=={{header|C sharp|C#}}==
{{trans|Java}}
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.IO;
Line 1,119:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Plausible count: 384
Line 1,132:
:* (Test used 4.4, so only a limited number of C++11 features were used.)
 
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <fstream>
#include <string>
Line 1,233:
return 0;
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,248:
The output here was generated with the files as of 21st June 2016.
 
<langsyntaxhighlight lang="clojure">
(ns i-before-e.core
(:require [clojure.string :as s])
Line 1,302:
(with-open [rdr (clojure.java.io/reader "http://ucrel.lancs.ac.uk/bncfreq/lists/1_2_all_freq.txt")]
(i-before-e-except-after-c-plausible? "Word frequencies (stretch goal)" (map format-freq-line (drop 1 (line-seq rdr))))))
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,322:
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">report = cluster is new, classify, results
rep = record[cie, xie, cei, xei, words: int]
Line 1,394:
stream$close(fstream)
stream$puts(po, report$results(r))
end start_up </langsyntaxhighlight>
{{out}}
<pre>Amount of words: 25104
Line 1,412:
Now we can do the task:
 
<langsyntaxhighlight lang="coco">ie-npc = ei-npc = ie-pc = ei-pc = 0
for word of dict.toLowerCase!.match /\S+/g
++ie-npc if /(^|[^c])ie/.test word
Line 1,424:
console.log '(1) is%s plausible.', if p1 then '' else ' not'
console.log '(2) is%s plausible.', if p2 then '' else ' not'
console.log 'The whole phrase is%s plausible.', if p1 and p2 then '' else ' not'</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
 
<langsyntaxhighlight lang="lisp">
(defun test-rule (rule-name examples counter-examples)
(let ((plausible (if (> examples (* 2 counter-examples)) 'plausible 'not-plausible)))
Line 1,463:
(plausibility "Dictionary" #p"unixdict.txt" #'parse-dict)
(plausibility "Word frequencies (stretch goal)" #p"1_2_all_freq.txt" #'parse-freq)
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,481:
=={{header|D}}==
The extra work has not been attempted
<langsyntaxhighlight Dlang="d">import std.file;
import std.stdio;
 
Line 1,604:
}
return cnt;
}</langsyntaxhighlight>
 
{{out}}
Line 1,616:
{{libheader| System.IOUtils}}
{{Trans|C sharp}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program I_before_E_except_after_C;
 
Line 1,676:
Writeln('Rule is not plausible.');
 
end.</langsyntaxhighlight>
 
=={{header|Draco}}==
<langsyntaxhighlight lang="draco">\util.g
 
/* variables to hold totals for each possibility */
Line 1,737:
if p then "" else "not " fi,
"plausible.")
corp</langsyntaxhighlight>
{{out}}
<pre>CIE: 24
Line 1,749:
=={{header|Elixir}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="elixir">defmodule RC do
def task(path) do
plausibility_ratio = 2
Line 1,777:
 
path = hd(System.argv)
IO.inspect RC.task(path)</langsyntaxhighlight>
 
{{out}}
Line 1,790:
 
=={{header|Erlang}}==
<langsyntaxhighlight lang="erlang">
-module(cei).
-export([plaus/0,count/3]).
Line 1,815:
nomatch -> count(T,Pattern, Acc)
end.
</syntaxhighlight>
</lang>
{{output}}
<pre>
Line 1,825:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: combinators formatting generalizations io.encodings.utf8
io.files kernel literals math prettyprint regexp sequences ;
IN: rosetta-code.i-before-e
Line 1,847:
 
"I before E when not preceded by C"
"E before I when preceded by C" [ output ] bi@</langsyntaxhighlight>
{{out}}
<pre>
Line 1,859:
=={{header|Fortran}}==
Please find the linux build instructions along with example run in the comments at the beginning of the f90 source. Thank you.
<syntaxhighlight lang="fortran">
<lang FORTRAN>
!-*- mode: compilation; default-directory: "/tmp/" -*-
!Compilation started at Sat May 18 22:19:19
Line 1,931:
end function plausibility
end program cia
</syntaxhighlight>
</lang>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight FreeBASIClang="freebasic">Function getfile(file As String) As String
Dim As Integer F = Freefile
Dim As String text,intext
Line 1,983:
print "So, the idea is not plausible."
 
Sleep</langsyntaxhighlight>
{{out}}
<pre>The number of words in unixdict.txt 25104
Line 1,999:
 
=={{header|FutureBasic}}==
<langsyntaxhighlight lang="futurebasic">include "NSLog.incl"
 
#plist NSAppTransportSecurity @{NSAllowsArbitraryLoads:YES}
Line 2,052:
fn DoIt
 
HandleEvents</langsyntaxhighlight>
 
{{output}}
Line 2,067:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 2,134:
}
return false
}</langsyntaxhighlight>
 
{{out}}
Line 2,148:
This solution does not attempt the stretch goal.
 
<langsyntaxhighlight Haskelllang="haskell">import Network.HTTP
import Text.Regex.TDFA
import Text.Printf
Line 2,172:
rule2Plausible = numTrueRule2 > (2*numFalseRule2)
printf "Rule 2 is correct for %d\n incorrect for %d\n" numTrueRule2 numFalseRule2
printf "*** Rule 2 is %splausible.\n" (if rule2Plausible then "" else "im")</langsyntaxhighlight>
 
{{out}}
Line 2,193:
same input line should all be tested.
 
<langsyntaxhighlight Uniconlang="unicon">import Utils # To get the FindFirst class
 
procedure main(a)
Line 2,211:
 
if \showCounts then every write(phrase := !phrases,": ",totals[phrase])
end</langsyntaxhighlight>
 
{{out}} of running with <tt>--showcounts</tt> flag:
Line 2,228:
=== stretch goal ===
 
<langsyntaxhighlight Uniconlang="unicon">import Utils # To get the FindFirst class
 
procedure main(a)
Line 2,252:
 
if \showCounts then every write(phrase := !phrases,": ",totals[phrase])
end</langsyntaxhighlight>
 
{{out}}
Line 2,271:
After downloading unixdict to /tmp:
 
<langsyntaxhighlight Jlang="j"> dict=:tolower fread '/tmp/unixdict.txt'</langsyntaxhighlight>
 
Investigating the rules:
 
<langsyntaxhighlight Jlang="j"> +/'cie' E. dict
24
+/'cei' E. dict
Line 2,282:
490
+/'ei' E. dict
230</langsyntaxhighlight>
 
So, based on unixdict.txt, the "I before E" rule seems plausible (490 > 230 by more than a factor of 2), but the exception does not make much sense (we see almost twice as many i before e after a c as we see e before i after a c).
Line 2,292:
After downloading 1_2_all_freq to /tmp, we can read it into J, and break out the first column (as words) and the third column as numbers:
 
<langsyntaxhighlight Jlang="j">allfreq=: |:}.<;._1;._2]1!:1<'/tmp/1_2_all_freq.txt'
 
words=: >0 { allfreq
freqs=: 0 {.@".&>2 { allfreq</langsyntaxhighlight>
 
With these definitions, we can define a prevalence verb which will tell us how often a particular substring is appears in use:
 
<langsyntaxhighlight Jlang="j">prevalence=:verb define
(y +./@E."1 words) +/ .* freqs
)</langsyntaxhighlight>
 
Investigating our original proposed rules:
 
<langsyntaxhighlight Jlang="j"> 'ie' %&prevalence 'ei'
1.76868</langsyntaxhighlight>
 
A generic "i before e" rule is not looking quite as good now - words that have i before e are used less than twice as much as words which use e before i.
 
<langsyntaxhighlight Jlang="j"> 'cei' %&prevalence 'cie'
0.328974</langsyntaxhighlight>
 
An "except after c" variant is looking awful now - words that use the cie sequence are three times as likely as words that use the cei sequence. So, of course, if we modified our original rule with this exception it would weaken the original rule:
 
<langsyntaxhighlight Jlang="j"> ('ie' -&prevalence 'cie') % ('ei' -&prevalence 'cei')
1.68255</langsyntaxhighlight>
 
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.
Line 2,323:
Download and save wordlist to unixdict.txt.
 
<langsyntaxhighlight lang="java">
import java.io.BufferedReader;
import java.io.FileReader;
Line 2,382:
}
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,393:
 
WARNING: The problem statement is misleading as the rule only applies to syllables that rhyme with "see".
<langsyntaxhighlight lang="jq">def plausibility_ratio: 2;
 
# scan/2 produces a stream of matches but the first match of a segment (e.g. cie)
Line 2,427:
as ratio = \($x)/\($y) ~ \($ratio * 100 |round)%" ;
 
"Using the problematic criterion specified in the task requirements:", assess</langsyntaxhighlight>
{{out}}
Using http://www.puzzlers.org/pub/wordlists/unixdict.txt as of June 2015:
<langsyntaxhighlight lang="sh">$ jq -s -R -r -f I_before_E_except_after_C.jq unixdict.txt
Using the problematic criterion specified in the task requirements:
-- the rule "E before I when preceded by C" is implausible
as ratio = 13/24 ~ 54%
-- the rule "I before E when not preceded by C" is plausible
as ratio = 464/217 ~ 214%</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia"># v0.0.6
 
open("unixdict.txt") do txtfile
Line 2,465:
print("Plausibility of \"E before I when preceded by C\":")
println(rule2 > 2 * notrule2 ? "PLAUSIBLE" : "UNPLAUSIBLE")
end</langsyntaxhighlight>
 
{{out}}
Line 2,472:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.6
 
import java.net.URL
Line 2,532:
println()
printResults("British National Corpus", counts2)
}</langsyntaxhighlight>
 
{{out}}
Line 2,564:
 
=={{header|Lasso}}==
<langsyntaxhighlight lang="lasso">
local(cie,cei,ie,ei) = (:0,0,0,0)
 
Line 2,598:
)
stdoutnl(`Overall the rule is ` + (#ie_plausible and #cei_plausible ? `` | `NOT-`) + `PLAUSIBLE`)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,607:
 
=={{header|Lua}}==
<langsyntaxhighlight Lualang="lua">-- Needed to get dictionary file from web server
local http = require("socket.http")
 
Line 2,638:
io.write("Overall the phrase is ")
if not (sub1 and sub2) then io.write("not ") end
print("plausible.")</langsyntaxhighlight>
{{out}}
<pre>I before E when not preceded by C: PLAUSIBLE
Line 2,645:
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">words:= HTTP:-Get("http://wiki.puzzlers.org/pub/wordlists/unixdict.txt"):
lst := StringTools:-Split(words[2],"\n"):
xie, cie, cei, xei := 0, 0, 0, 0:
Line 2,667:
printf("The first phrase is %s with supporting features %d, anti features %d\n", piecewise(p1, "plausible", "not plausible"), xie, xei);
printf("The seond phrase is %s with supporting features %d, anti features %d\n", piecewise(p2, "plausible", "not plausible"), cei, cie);
printf("The overall phrase is %s\n", piecewise(p1 and p2, "plausible", "not plausible")):</langsyntaxhighlight>
{{Out|Output}}
<pre>The first phrase is plausible with supporting features 465 and anti features 213
Line 2,674:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight lang="mathematica">wordlist =
Import["http://wiki.puzzlers.org/pub/wordlists/unixdict.txt",
"Words"];
Line 2,700:
ToString[N[cei/cie]]]
Print["Overall the rule is " <>
If[test1 && test2, "PLAUSIBLE", "NOT PLAUSIBLE" ]]</langsyntaxhighlight>
 
{{out}}
<langsyntaxhighlight lang="mathematica">The number of words in unixdict.txt = 25104
The rule "I before E when not preceded by C" is PLAUSIBLE
There were 465 examples and 213 counter examples, for a ratio of 2.1831
Line 2,709:
There were 13 examples and 24 counter examples, for a ratio of 0.541667
Overall the rule is NOT PLAUSIBLE
</syntaxhighlight>
</lang>
 
=={{header|MATLAB}} / {{header|Octave}}==
{{incomplete|MATLAB|Is the original phrase plausible?}}
 
<langsyntaxhighlight MATLABlang="matlab">function i_before_e_except_after_c(f)
 
fid = fopen(f,'r');
Line 2,746:
end
printf('E before I when preceded by C: is%s plausible\n',v);
</syntaxhighlight>
</lang>
 
<pre>octave:23> i_before_e_except_after_c 1_2_all_freq.txt
Line 2,764:
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE IEC;
IMPORT SeqIO;
IMPORT Texts;
Line 2,859:
WriteString("plausible.");
WriteLn;
END IEC.</langsyntaxhighlight>
{{out}}
<pre>Amount of words: 50209
Line 2,872:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import httpclient, strutils, strformat
 
const
Line 2,905:
let p1 = plausibility(Rule1, nie, nei)
let p2 = plausibility(Rule2, cei, cie)
echo &"So the phrase {Phrase} is {PlausibilityText[p1 and p2]}."</langsyntaxhighlight>
 
{{out}}
Line 2,914:
=={{header|Objeck}}==
{{trans|Seed7}}
<langsyntaxhighlight lang="objeck">
use HTTP;
use Collection;
Line 2,976:
}
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,990:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
use warnings;
use strict;
Line 3,023:
$result += result($support, $against);
 
print 'Overall: ', 'NOT ' x ($result < 2), "PLAUSIBLE.\n";</langsyntaxhighlight>
 
{{out}}
Line 3,032:
===Perl: Stretch Goal===
Just replace the while loop with the following one:
<langsyntaxhighlight lang="perl">while (<>) {
my @columns = split;
next if 3 < @columns;
Line 3,039:
$count{$k} += $freq if -1 != index $word, $k;
}
}</langsyntaxhighlight>
{{out}}
<pre>I before E when not preceded by C: 8148 / 4826 = 1.69. NOT PLAUSIBLE
Line 3,047:
=={{header|Phix}}==
Kept dirt simple, difficult to imagine any other approach being faster than this.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">-- demo\rosetta\IbeforeE.exw</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Line 3,085:
<span style="color: #000000;">show_plausibility</span><span style="color: #0000FF;">(</span> <span style="color: #008000;">"i before e in general"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">xie</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">cie</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">xei</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">cei</span> <span style="color: #0000FF;">);</span>
<span style="color: #000000;">show_plausibility</span><span style="color: #0000FF;">(</span> <span style="color: #008000;">"e before i in general"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">xei</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">cei</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">xie</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">cie</span> <span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
Although the output matches, I decided to use different metrics from ALGOL 68 for the middle two conclusions.<br>
Line 3,101:
 
=={{header|Picat}}==
<langsyntaxhighlight Picatlang="picat">main =>
Words = read_file_lines("unixdict.txt"),
IEWords = [Word : Word in Words, find(Word,"ie",_,_)],
Line 3,131:
False := [Word|False]
end
end.</langsyntaxhighlight>
 
{{out}}
Line 3,141:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de ibEeaC (File . Prg)
(let
(Cie (let N 0 (in File (while (from "cie") (run Prg))))
Line 3,171:
 
(ibEeaC "1_2_all_freq.txt"
(inc 'N (format (stem (line) "\t"))) )</langsyntaxhighlight>
Output:
<pre>cie: 24
Line 3,190:
 
=={{header|PL/I}}==
<langsyntaxhighlight lang="pli">iBeforeE: procedure options(main);
declare dict file;
open file(dict) title('unixdict.txt');
Line 3,238:
if ^(ieNotC & eiC) then put list('not');
put list('plausible.');
end iBeforeE;</langsyntaxhighlight>
{{out}}
<pre>CIE: 24
Line 3,249:
 
=={{header|PowerShell}}==
<langsyntaxhighlight Powershelllang="powershell">$Web = New-Object -TypeName Net.Webclient
$Words = $web.DownloadString('http://wiki.puzzlers.org/pub/wordlists/unixdict.txt')
Line 3,277:
if ($Clause1 -and $Clause2)
{$MainClause = $True}
"The plausibility of the phrase 'I before E except after C' is $MainClause"</langsyntaxhighlight>
{{out}}
<pre>
Line 3,286:
 
==={{header|Alternative Implementation}}===
<langsyntaxhighlight Powershelllang="powershell">$Web = New-Object -TypeName Net.Webclient
$Words = $web.DownloadString('http://wiki.puzzlers.org/pub/wordlists/unixdict.txt')
Line 3,314:
if ($Clause1 -and $Clause2)
{$MainClause = $True}
"The plausibility of the phrase 'I before E except after C' is $MainClause"</langsyntaxhighlight>
{{out}}
<pre>
Line 3,323:
==={{header|Alternative Implementation 2}}===
A single pass through the wordlist using the regex engine.
<langsyntaxhighlight Powershelllang="powershell">$webResult = Invoke-WebRequest -Uri http://wiki.puzzlers.org/pub/wordlists/unixdict.txt -UseBasicParsing
 
$cie, $cei, $_ie, $_ei = 0, 0, 0, 0
Line 3,336:
"I before E when not preceded by C is plausible: $($_ie -gt $_ei)"
"E before I when preceded by C is plausible: $($cei -gt $cie)"
"I before E, except after C is plausible: $(($_ie -gt $_ei) -and ($cei -gt $cie))"</langsyntaxhighlight>
{{out}}
<pre>
Line 3,345:
 
=={{header|PureBasic}}==
<langsyntaxhighlight lang="purebasic">If ReadFile(1,GetPathPart(ProgramFilename())+"wordlist(en).txt")
While Not Eof(1)
wl$+ReadString(1)+";"
Line 3,367:
Print("Overall the rule is : ")
If cei>cie And ie>ei : PrintN("PLAUSIBLE") : Else : PrintN("NOT PLAUSIBLE") : EndIf
Input()</langsyntaxhighlight>
{{out}}
<pre>
Line 3,383:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">import urllib.request
import re
 
Line 3,419:
 
print('Checking plausibility of "I before E except after C":')
print_result(*simple_stats())</langsyntaxhighlight>
 
{{out}}
Line 3,435:
===Python: Stretch Goal===
Add the following to the bottom of the previous program:
<langsyntaxhighlight lang="python">def stretch_stats(url='http://ucrel.lancs.ac.uk/bncfreq/lists/1_2_all_freq.txt'):
freq = [line.strip().lower().split()
for line in urllib.request.urlopen(url)
Line 3,450:
print('\n\nChecking plausibility of "I before E except after C"')
print('And taking account of word frequencies in British English:')
print_result(*stretch_stats())</langsyntaxhighlight>
 
{{out|Produces this extra output}}
Line 3,468:
=={{header|QBasic}}==
{{trans|BASIC}}
<langsyntaxhighlight QBasiclang="qbasic">DEFINT A-Z
DIM W AS STRING
CLS
Line 3,489:
PRINT "E before I when preceded by C: ";
IF 2 * CE <= XE THEN PRINT "not ";
PRINT "plausible."</langsyntaxhighlight>
 
=={{header|R}}==
<langsyntaxhighlight lang="rsplus">words = tolower(readLines("http://wiki.puzzlers.org/pub/wordlists/unixdict.txt"))
ie.npc = sum(grepl("(?<!c)ie", words, perl = T))
ei.npc = sum(grepl("(?<!c)ei", words, perl = T))
Line 3,503:
message("(1) is ", (if (p1) "" else "not "), "plausible.")
message("(2) is ", (if (p2) "" else "not "), "plausible.")
message("The whole phrase is ", (if (p1 && p2) "" else "not "), "plausible.")</langsyntaxhighlight>
 
{{out}}
Line 3,511:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
 
(define (get-tallies filename line-parser . patterns)
Line 3,542:
 
(plausibility "Dictionary" "unixdict.txt" (λ (line) (list line 1))) (newline)
(plausibility "Word frequencies (stretch goal)" "1_2_all_freq.txt" parse-frequency-data)</langsyntaxhighlight>
 
{{out}}
Line 3,562:
(formerly Perl 6)
This solution uses grammars and actions to parse the given file, the <tt>Bag</tt> for tallying up occurrences of each possible thing we're looking for ("ie", "ei", "cie", and "cei"), and junctions to determine the plausibility of a phrase from the subphrases. Note that a version of rakudo newer than the January 2014 compiler or Star releases is needed, as this code relies on a recent bugfix to the <tt>make</tt> function.
<syntaxhighlight lang="raku" perl6line>grammar CollectWords {
token TOP {
[^^ <word> $$ \n?]+
Line 3,629:
plausible($results<cei>, $results<cie>, "E before I when preceded by C");
 
say "I before E except after C: ", $phrasetest ?? "PLAUSIBLE" !! "NOT PLAUSIBLE";</langsyntaxhighlight>
 
{{out}}
Line 3,652:
 
This solution requires just a few modifications to the grammar and actions from the non-stretch goal.
<syntaxhighlight lang="raku" perl6line>grammar CollectWords {
token TOP {
^^ \t Word \t PoS \t Freq $$ \n
Line 3,717:
plausible($results<cei>, $results<cie>, "E before I when preceded by C");
 
say "I before E except after C: ", $phrasetest ?? "PLAUSIBLE" !! "NOT PLAUSIBLE";</langsyntaxhighlight>
 
{{out}}
Line 3,727:
The script processes both the task and the stretch goal.
In the stretch goal, "rows with three space or tab separated words only" (7574 out of 7726) are processed, excluding all expressions like "out of".
<langsyntaxhighlight Redlang="red">Red ["i before e except after c"]
 
testlist: function [wordlist /wfreq] [
Line 3,759:
keep seq/1 keep to-integer seq/3
]]]
testlist/wfreq bnclist</langsyntaxhighlight>
 
{{out}}
Line 3,784:
 
===unweighted version===
<langsyntaxhighlight lang="rexx">/*REXX program shows plausibility of "I before E" when not preceded by C, and */
/*───────────────────────────────────── "E before I" when preceded by C. */
parse arg iFID . /*obtain optional argument from the CL.*/
Line 3,819:
else #.x.z=#.x.z + 1
s=_ + 1 /*handle the cases of multiple finds. */
end /*forever*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default dictionary:}}
<pre>
Line 3,855:
 
A cursory look at the file seems to indicate that the use of the tilde and/or asterisk doesn't affect the rules for the mantra phrases.
<langsyntaxhighlight lang="rexx">/*REXX program shows plausibility of "I before E" when not preceded by C, and */
/*───────────────────────────────────── "E before I" when preceded by C, using a */
/*───────────────────────────────────── weighted frequency for each word. */
Line 3,915:
if substr(u, _ - 1 + (_==1)*999, 1)=='C' then #.x.c=#.x.c + one
else #.x.z=#.x.z + one
s=_ + 1 /*handle the cases of multiple finds. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default dictionary and default word frequency list:}}
<pre>
Line 3,936:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : I before E except after C
 
Line 3,976:
end
return sum
</syntaxhighlight>
</lang>
Output:
<pre>
Line 3,990:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require 'open-uri'
 
plausibility_ratio = 2
Line 4,009:
 
puts "Overall: #{overall_plausible ? 'Plausible' : 'Implausible'}."
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 4,020:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">use std::default::Default;
use std::ops::AddAssign;
 
Line 4,099:
);
}
</syntaxhighlight>
</lang>
<pre>
Counting Feature {
Line 4,113:
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">object I_before_E_except_after_C extends App {
val testIE1 = "(^|[^c])ie".r // i before e when not preceded by c
val testIE2 = "cie".r // i before e when preceded by c
Line 4,135:
println("E before I when preceded by C: "+plausibility(countsCEI))
println("Overall: "+plausibility(plausible(countsIE) && plausible(countsCEI)))
}</langsyntaxhighlight>
{{out}}
<pre>I before E when not preceded by C: plausible
Line 4,142:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "gethttp.s7i";
include "float.s7i";
Line 4,200:
writeln("(To be plausible, one word count must exceed another by " <& PLAUSIBILITY_RATIO <& " times)");
end if;
end func;</langsyntaxhighlight>
 
{{out}}
Line 4,215:
=={{header|Swift}}==
Using [https://github.com/johnno1962/SwiftRegex/blob/master/SwiftRegex.swift SwiftRegex] for easy regex in strings.
<langsyntaxhighlight Swiftlang="swift">import Foundation
 
let request = NSURLRequest(URL: NSURL(string: "http://wiki.puzzlers.org/pub/wordlists/unixdict.txt")!)
Line 4,269:
}
 
CFRunLoopRun()</langsyntaxhighlight>
{{out}}
<pre>
Line 4,279:
=={{header|True BASIC}}==
{{trans|BASIC}}
<langsyntaxhighlight lang="qbasic">DEF EOF(f)
IF END #f THEN LET EOF = -1 ELSE LET EOF = 0
END DEF
Line 4,307:
IF 2*ce <= xe THEN PRINT "not ";
PRINT "plausible."
END</langsyntaxhighlight>
 
=={{header|Tcl}}==
{{trans|Python}}<!-- very approximately, mainly for the messages -->
<langsyntaxhighlight lang="tcl">package require http
 
variable PLAUSIBILITY_RATIO 2.0
Line 4,352:
}
puts "\n(To be plausible, one word count must exceed another by\
$PLAUSIBILITY_RATIO times)"</langsyntaxhighlight>
{{out}}
<!-- note that checking the pronunciation of the words indicates a key guard on the real rule that isn't normally stated -->
Line 4,370:
 
=={{header|TUSCRIPT}}==
<langsyntaxhighlight lang="tuscript">
$$ MODE TUSCRIPT,{}
words=REQUEST("http://wiki.puzzlers.org/pub/wordlists/unixdict.txt")
Line 4,433:
 
TRAcE *check1,check2,checkall
</syntaxhighlight>
</lang>
Output:
<pre>
Line 4,449:
=={{header|uBasic/4tH}}==
{{trans|PowerShell}}
<syntaxhighlight lang="text">If Set(a, Open ("unixdict.txt", "r")) < 0 Then Print "Cannot open \qunixdict.txt\q" : End
 
x = Set (y, Set (p, Set (q, 0)))
Line 4,479:
If Comp(Clip(Chop(a@,c@),Len(a@)-c@-Len(b@)),b@)=0 Then Unloop : Return (c@)
Next
Return (-1)</langsyntaxhighlight>
{{Out}}
<pre>The plausibility of 'I before E when not preceded by C' is True
Line 4,488:
 
=={{header|UNIX Shell}}==
<langsyntaxhighlight lang="bash">#!/bin/sh
 
matched() {
Line 4,515:
echo "Overall, the rule is not plausible"
fi
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 4,525:
=={{header|VBScript}}==
The sample text was downloaded and saved in the same folder as the script.
<syntaxhighlight lang="vb">
<lang vb>
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set srcFile = objFSO.OpenTextFile(objFSO.GetParentFolderName(WScript.ScriptFullName) &_
Line 4,574:
srcFile.Close
Set objFSO = Nothing
</syntaxhighlight>
</lang>
 
{{Out}}
Line 4,591:
Regex implementation does not technically conform to specification because it counts the number of occurrences of "ie" and "ei" instead of the number of words.
 
<langsyntaxhighlight lang="vbnet">Option Compare Binary
Option Explicit On
Option Infer On
Line 4,726:
End Sub
End Module
</syntaxhighlight>
</lang>
 
{{out|case=Loop implementation}}
Line 4,777:
 
Also there are seven words which fall into two categories and which have therefore been double-counted.
<langsyntaxhighlight lang="ecmascript">import "io" for File
import "/pattern" for Pattern
import "/fmt" for Fmt
Line 4,828:
Fmt.print(" Plausible : $s", yesNo.call(plaus2))
 
Fmt.print("\nPlausible overall: $s", yesNo.call(plaus && plaus2))</langsyntaxhighlight>
 
{{out}}
Line 4,859:
And the code and results for the 'stretch goal' which has just the one double-counted word:
 
<langsyntaxhighlight lang="ecmascript">import "io" for File
import "/pattern" for Pattern
import "/fmt" for Fmt
Line 4,916:
Fmt.print(" Plausible : $s", yesNo.call(plaus2))
 
Fmt.print("\nPlausible overall: $s", yesNo.call(plaus && plaus2))</langsyntaxhighlight>
 
{{out}}
Line 4,942:
=={{header|Yabasic}}==
{{trans|BASIC}}
<langsyntaxhighlight lang="freebasic">open "unixdict.txt" for reading as #1
 
repeat
Line 4,965:
if 2 * CE <= XE then print "not "; : fi
print "plausible."
end</langsyntaxhighlight>
 
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn wcnt(wordList,altrs,aAdjust,bltrs,bAdjust,text){
a:=wordList.reduce('wrap(cnt,word){ cnt+word.holds(altrs) },0) - aAdjust;
b:=wordList.reduce('wrap(cnt,word){ cnt+word.holds(bltrs) },0) - bAdjust;
Line 4,977:
return(a,b,ratio);
}
wordList:=File("unixdict.txt").read();</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">a,b,r1:=wcnt(wordList,"cei",0,"cie",0,"E before I when preceded by C");
_,_,r2:=wcnt(wordList,"ie",b,"ei",a, "I before E when not preceded by C");
"Overall the rule is %splausible".fmt((r1<2 or r2<2) and "im" or "").println();</langsyntaxhighlight>
{{out}}
<pre>
Line 4,990:
</pre>
Stretch
<langsyntaxhighlight lang="zkl">fcn wc2(wordList,altrs,aAdjust,bltrs,bAdjust,text){
a,b:=wordList.reduce('wrap(cnts,line){
// don't care if line is "Word PoS Freq" or "as yet Adv 14"
Line 5,004:
return(a,b,ratio);
}
wordList:=File("1_2_all_freq.txt").read();</langsyntaxhighlight>
{{out}}
<pre>
10,339

edits