I before E except after C

From Rosetta Code
Revision as of 08:46, 3 January 2013 by rosettacode>Paddy3118 (New draft task and Python solution.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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 [1], 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.

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 = urllib.request.urlopen(

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

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

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)