I before E except after C: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|REXX}}: added comments in the section header about assumptions made about the (default) dictionary. -- ~~~~)
m (→‎{{header|REXX}}: removed some dead code. -- ~~~~)
Line 81: Line 81:
if ie\==0 & ei\==0 then #.eiie=#.eiie+1 /*word contains both IE & EI*/
if ie\==0 & ei\==0 then #.eiie=#.eiie+1 /*word contains both IE & EI*/


if ie\==0 then do; #.iek=#.iek+1; if ie==1 then ie=999 /*word: IExxx*/
if ie\==0 then do; if ie==1 then ie=999 /*word starts with: IExxx */
if substr(u,ie-1, 1)=='C' then #.iec=#.iec+1
if substr(u,ie-1, 1)=='C' then #.iec=#.iec+1
else #.iex=#.iex+1
else #.iex=#.iex+1
end
end
if ei\==0 then do; #.eik=#.eik+1; if ei==1 then ei=999 /*word: EIxxx*/
if ei\==0 then do; if ei==1 then ei=999 /*word starts with: EIxxx */
if substr(u,ei-1, 1)=='C' then #.eic=#.eic+1
if substr(u,ei-1, 1)=='C' then #.eic=#.eic+1
else #.eix=#.eix+1
else #.eix=#.eix+1
end
end
end /*r*/
end /*r*/

Revision as of 20:10, 3 January 2013

I before E except after C is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

The phrase "I before E, except after C" is a widely known mnemonic which is supposed to help when spelling English words.

Task Description

Using the word list from http://www.puzzlers.org/pub/wordlists/unixdict.txt, check if the two sub-clauses of the phrase are plausible individually:

  1. "I before E when not preceded by C"
  2. "E before I when preceded by C"

If both sub-phrases are plausible then the original phrase can be said to be plausible.
Something is plausible if the number of words having the feature is more than two times the number of words having the opposite feature (where feature is 'ie' or 'ei' preceded or not by 'c' as appropriate).

Show your output here as well as your program.

Cf

Python

<lang python>import urllib.request import re

PLAUSIBILITY_RATIO = 2

def plausibility_check(comment, x, y):

   print('\n  Checking plausibility of: %s' % comment)
   if x > PLAUSIBILITY_RATIO * y:
       print('    PLAUSIBLE. As we have counts of %i vs %i words, a ratio of %4.1f times'
             % (x, y, x / y))
   else:
       if x > y:
           print('    IMPLAUSIBLE. As although we have counts of %i vs %i words, a ratio of %4.1f times does not make it plausible'
                 % (x, y, x / y))
       else:
           print('    IMPLAUSIBLE, probably contra-indicated. As we have counts of %i vs %i words, a ratio of %4.1f times'
                 % (x, y, x / y))
   return x > PLAUSIBILITY_RATIO * y

words = set(urllib.request.urlopen(

   'http://www.puzzlers.org/pub/wordlists/unixdict.txt'
   ).read().decode().lower().split())

cie = sum('cie' in word for word in words) cei = sum('cei' in word for word in words) not_c_ie = sum(bool(re.search(r'(^ie|[^c]ie)', word)) for word in words) not_c_ei = sum(bool(re.search(r'(^ei|[^c]ei)', word)) for word in words)

print('Checking plausibility of "I before E except after C":') if ( plausibility_check('I before E when not preceded by C', not_c_ie, not_c_ei)

    and plausibility_check('E before I when preceded by C', cei, cie) ):
   print('\nOVERALL IT IS PLAUSIBLE!')

else:

   print('\nOVERALL IT IS IMPLAUSIBLE!')

print('\n(To be plausible, one word count must exceed another by %i times)' % PLAUSIBILITY_RATIO)</lang>

Output:
Checking plausibility of "I before E except after C":

  Checking plausibility of: I before E when not preceded by C
    PLAUSIBLE. As we have counts of 465 vs 213 words, a ratio of  2.2 times

  Checking plausibility of: E before I when preceded by C
    IMPLAUSIBLE, probably contra-indicated. As we have counts of 13 vs 24 words, a ratio of  0.5 times

OVERALL IT IS IMPLAUSIBLE!

(To be plausible, one word count must exceed another by 2 times)

REXX

The following assumptions were made about the (default) dictionary:

  • assumed there could be leading and/or trailing blanks or tabs
  • assumed the dictionary words are in mixed case.
  • assumed there could be blank lines

<lang rexx>/*REXX pgm shows plausibility of I before E when not preceded by C, and*/ /*────────────────────────────── E before I when preceded by C. */

  1. .=0 /*zero out various word counters.*/

parse arg iFID .; if iFID== then iFID='UNIXDICT.TXT' /*use default?*/

 do r=1  while lines(ifid)\==0;    _=linein(iFID)  /*get a single line.*/
 u=translate(space(_,0))              /*elide superfluous blanks & tabs*/
 if u==            then iterate     /*if a blank line, then ignore it*/
 #.words=#.words+1                    /*keep a running count of #words.*/
 ei=pos('EI',u);     ie=pos('IE',u)   /*see if EI or IE is in this word*/
 if ie\==0 & ei\==0  then #.eiie=#.eiie+1  /*word contains both IE & EI*/
 if ie\==0 then do;  if ie==1  then ie=999 /*word starts with:  IExxx  */
                if substr(u,ie-1, 1)=='C'  then #.iec=#.iec+1
                                           else #.iex=#.iex+1
                end
 if ei\==0 then do;  if ei==1  then ei=999 /*word starts with:  EIxxx  */
                if substr(u,ei-1, 1)=='C'  then #.eic=#.eic+1
                                           else #.eix=#.eix+1
                end
 end   /*r*/

L=length(#.words) /*use this to align output. */ say 'words in the ' ifid ' dictionary: ' #.words say 'words with "IE" and "EI" (in same word): ' right(#.eiie,L) say 'words with "IE" and preceded by "C": ' right(#.iec ,L) say 'words with "IE" and not preceded by "C": ' right(#.iex ,L) say 'words with "EI" and preceded by "C": ' right(#.eic ,L) say 'words with "EI" and not preceded by "C": ' right(#.eix ,L) say; mantra='The spelling mantra ' p1=#.iex/#.eix; phrase='"I before E when not preceded by C"' say mantra phrase ' is ' word("im", 1+(p1>2))'plausible.' p2=#.iec/#.eic; phrase='"E before I when preceded by C"' say mantra phrase ' is ' word("im", 1+(p2>2))'plausible.' po=p1>2 & p2>2; say 'Overall, it is' word("im",1+po)'plausible.'

                                      /*stick a fork in it, we're done.*/

</lang> output when using the default dictionary

words in the   UNIXDICT.TXT  dictionary:  25104
words with "IE" and "EI" (in same word):      4
words with "IE" and     preceded by "C":     24
words with "IE" and not preceded by "C":    465
words with "EI" and     preceded by "C":     13
words with "EI" and not preceded by "C":    213

The spelling mantra   "I before E when not preceded by C"  is  plausible.
The spelling mantra   "E before I when preceded by C"  is  implausible.
Overall, it is implausible.