I before E except after C: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|REXX}}: added code to circumvent the possibility of dividing by zero. -- ~~~~)
(→‎{{header|REXX}}: added code to support possibility of finding more than one string within a single word. -- ~~~~)
Line 66: Line 66:
=={{header|REXX}}==
=={{header|REXX}}==
The following assumptions were made about the (default) dictionary:
The following assumptions were made about the (default) dictionary:
* assumed there could be leading and/or trailing blanks or tabs
* there could be leading and/or trailing blanks or tabs
* assumed the dictionary words are in mixed case.
* the dictionary words are in mixed case.
* assumed there could be blank lines
* there could be blank lines
* there may be more than one occurrence of a target string within a word [einsteinium]
<lang rexx>/*REXX pgm shows plausibility of I before E when not preceded by C, and*/
<lang rexx>/*REXX pgm shows plausibility of I before E when not preceded by C, and*/
/*────────────────────────────── E before I when preceded by C. */
/*────────────────────────────── E before I when preceded by C. */
Line 78: Line 79:
if u=='' then iterate /*if a blank line, then ignore it*/
if u=='' then iterate /*if a blank line, then ignore it*/
#.words=#.words+1 /*keep a running count of #words.*/
#.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 pos('EI',u)\==0 & pos('IE',u)\==0 then #.both=#.both+1 /*has both.*/
call find 'ie'
if ie\==0 & ei\==0 then #.eiie=#.eiie+1 /*word contains both IE & EI*/
call find 'ei'
if ie\==0 then if substr(u,ie-1+(ie==1)*999, 1)=='C' then #.iec=#.iec+1
else #.iex=#.iex+1
if ei\==0 then if substr(u,ei-1+(ei==1)*999, 1)=='C' then #.eic=#.eic+1
else #.eix=#.eix+1
end /*r*/
end /*r*/


L=length(#.words) /*use this to align output. */
L=length(#.words) /*use this to align the output #s*/
say 'words in the ' ifid ' dictionary: ' #.words
say 'words in the ' ifid ' dictionary: ' #.words
say 'words with "IE" and "EI" (in same word): ' right(#.eiie,L)
say 'words with "IE" and "EI" (in same word): ' right(#.both,L)
say 'words with "IE" and preceded by "C": ' right(#.iec ,L)
say 'words with "IE" and preceded by "C": ' right(#.ie.c ,L)
say 'words with "IE" and not preceded by "C": ' right(#.iex ,L)
say 'words with "IE" and not preceded by "C": ' right(#.ie.z ,L)
say 'words with "EI" and preceded by "C": ' right(#.eic ,L)
say 'words with "EI" and preceded by "C": ' right(#.ei.c ,L)
say 'words with "EI" and not preceded by "C": ' right(#.eix ,L)
say 'words with "EI" and not preceded by "C": ' right(#.ei.z ,L)
say; mantra='The spelling mantra '
say; mantra='The spelling mantra '
p1=#.iex/max(1,#.eix); phrase='"I before E when not preceded by C"'
p1=#.ie.z/max(1,#.ei.z); phrase='"I before E when not preceded by C"'
say mantra phrase ' is ' word("im", 1+(p1>2))'plausible.'
say mantra phrase ' is ' word("im", 1+(p1>2))'plausible.'
p2=#.iec/max(1,#.eic); phrase='"E before I when preceded by C"'
p2=#.ie.c/max(1,#.ei.c); phrase='"E before I when preceded by C"'
say mantra phrase ' is ' word("im", 1+(p2>2))'plausible.'
say mantra phrase ' is ' word("im", 1+(p2>2))'plausible.'
po=p1>2 & p2>2; say 'Overall, it is' word("im",1+po)'plausible.'
po=p1>2 & p2>2; say 'Overall, it is' word("im",1+po)'plausible.'
/*stick a fork in it, we're done.*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────FIND subroutine─────────────────────*/
</lang>
find: arg x; s=1; do forever; _=pos(x,u,s); if _==0 then leave
if substr(u,_-1+(_==1)*999,1)=='C' then #.x.c=#.x.c+1
else #.x.z=#.x.z+1
s=_+1 /*handle case of multiple finds. */
end /*forever*/
return</lang>
'''output''' when using the default dictionary
'''output''' when using the default dictionary
<pre style="overflow:scroll">
<pre style="overflow:scroll">

Revision as of 21:02, 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:

  • there could be leading and/or trailing blanks or tabs
  • the dictionary words are in mixed case.
  • there could be blank lines
  • there may be more than one occurrence of a target string within a word [einsteinium]

<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.*/
 if pos('EI',u)\==0 & pos('IE',u)\==0 then #.both=#.both+1  /*has both.*/
 call find 'ie'
 call find 'ei'
 end   /*r*/

L=length(#.words) /*use this to align the output #s*/ say 'words in the ' ifid ' dictionary: ' #.words say 'words with "IE" and "EI" (in same word): ' right(#.both,L) say 'words with "IE" and preceded by "C": ' right(#.ie.c ,L) say 'words with "IE" and not preceded by "C": ' right(#.ie.z ,L) say 'words with "EI" and preceded by "C": ' right(#.ei.c ,L) say 'words with "EI" and not preceded by "C": ' right(#.ei.z ,L) say; mantra='The spelling mantra ' p1=#.ie.z/max(1,#.ei.z); phrase='"I before E when not preceded by C"' say mantra phrase ' is ' word("im", 1+(p1>2))'plausible.' p2=#.ie.c/max(1,#.ei.c); 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.' exit /*stick a fork in it, we're done.*/ /*──────────────────────────────────FIND subroutine─────────────────────*/ find: arg x; s=1; do forever; _=pos(x,u,s); if _==0 then leave

                   if substr(u,_-1+(_==1)*999,1)=='C'  then #.x.c=#.x.c+1
                                                       else #.x.z=#.x.z+1
                   s=_+1              /*handle case of multiple finds. */
                   end   /*forever*/

return</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.