A digraph is a pair of characters used in the orthography of a language to write a single phoneme (a distinct sound). In English, the correct spelling of a word may greatly differ from its pronunciation. If one is unsure whether a word is spelled with the digraph ei or ei, the mnemonic rule of thumb

I before E, except after C

suggests that the correct order is ie, unless the digraph is preceded by the letter c. In that case, it must be spelled as ei. Because of its simplicity, the rhyme is widely known in the Anglo-Saxon world. Edward Carney even calls it

this supreme, and for many people solitary, spelling rule

coffee mug
Coffee cup with exceptions to the rule "I before E except after C".

However, there are many common exceptions to "I before E …":

albeit, beige, counterfeit, deity, either, feisty, height, kaleidoscope, leisure, neighbor, obeisance, protein, reveille, seize, veil, weird, zeitgeist

"… except after C":

ancient, concierge, efficient, financier, glacier, juicier, democracies, species

such that Will Rogers once said:

Nothing you can't spell will ever work.

Assignment

An English word is a sequence of one or more letters, hyphens (-) and quotation marks ('). You may assume that there's always at least one character between two occurrences of the digraphs ei and ie in an English word. English words such as boogieing, sortieing or Weierstrass will therefore never occur in this assignment.

The English words in an English sentence are the longest possible sequences of one or more letters, hyphens (-) and quotation marks ('). As such, eighty-eight is a single word, as is O'Reilly.

Your task:

Example

>>> fill('h??roglyphic', False)
'hieroglyphic'
>>> fill('inconc??vable', False)
'inconceivable'
>>> fill('fr??ndl??st', False)
'friendliest'
>>> fill('programmer', False)
'programmer'
>>> fill('alb??t', True)
'albeit'
>>> fill('z??tg??st', True)
'zeitgeist'
>>> fill('conc??rge', True)
'concierge'
>>> fill('glac??r', True)
'glacier'

>>> mask('hieroglyphic')
'h??roglyphic'
>>> mask('inconceivable')
'inconc??vable'
>>> mask('friendliest')
'fr??ndl??st'
>>> mask('programmer')
'programmer'
>>> mask('albeit')
'alb??t'
>>> mask('zeitgeist')
'z??tg??st'
>>> mask('concierge')
'conc??rge'
>>> mask('glacier')
'glac??r'

>>> isexception('hierarchy')
False
>>> isexception('ancient')
True
>>> isexception('ceiling')
False
>>> isexception('fahrenheit')
True
>>> isexception('daily')
False

>>> exceptions('I believe in the power of education.')
0
>>> exceptions('The ancient recipe requires a dash of weird ingredients.')
2
>>> exceptions('The society was fraught with a weird sense of hierarchy.')
2
>>> exceptions('The efficient machine processed the data with great speed.')
1
>>> exceptions('I before E except when your feisty foreign neighbor Keith leisurely receives eight counterfeit beige sleighs from weirdly caffeinated atheist weightlifters')
13

Resources

Epilogue