How to resolve the algorithm I before E except after C step by step in the Raku programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm I before E except after C step by step in the Raku programming language

Table of Contents

Problem Statement

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

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

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).

As a stretch goal use the entries from the table of Word Frequencies in Written and Spoken English: based on the British National Corpus, (selecting those rows with three space or tab separated words only), to see if the phrase is plausible when word frequencies are taken into account.

Show your output here as well as your program.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm I before E except after C step by step in the Raku programming language

Source code in the raku programming language

grammar CollectWords {
    token TOP {
        [^^  $$ \n?]+
    }

    token word {
        [  |  | \N ]+
    }

    token with_c {
        c 
    }

    token no_c {
        
    }

    token ie_part {
        ie | ei | eie # a couple words in the list have "eie"
    }
}

class CollectWords::Actions {
    method TOP($/) {
        make $».ast.flat.Bag;
    }

    method word($/) {
        if $ + $ {
            make flat $».ast, $».ast;
        } else {
            make ();
        }
    }

    method with_c($/) {
        make "c" X~ $.ast;
    }

    method no_c($/) {
        make "!c" X~ $.ast;
    }

    method ie_part($/) {
        if ~$/ eq 'eie' {
            make ('ei', 'ie');
        } else {
            make ~$/;
        }
    }
}

sub plausible($good, $bad, $msg) {
    if $good > 2*$bad {
        say "$msg: PLAUSIBLE ($good  vs. $bad ✘)";
        return True;
    } else {
        say "$msg: NOT PLAUSIBLE ($good  vs. $bad ✘)";
        return False;
    }
}

my $results = CollectWords.parsefile("unixdict.txt", :actions(CollectWords::Actions)).ast;

my $phrasetest = [&] plausible($results, $results, "I before E when not preceded by C"),
                     plausible($results, $results, "E before I when preceded by C");

say "I before E except after C: ", $phrasetest ?? "PLAUSIBLE" !! "NOT PLAUSIBLE";


grammar CollectWords {
    token TOP {
        ^^ \t Word \t PoS \t Freq $$ \n
        [^^  $$ \n?]+
    }

    token word {
        \t+
        [  |  | \T ]+ \t+
        \T+ \t+ # PoS doesn't matter to us, so ignore it
        $=[<.digit>+] \h*
    }

    token with_c {
        c 
    }

    token no_c {
        
    }

    token ie_part {
        ie | ei
    }
}

class CollectWords::Actions {
    method TOP($/) {
        make $».ast.flat.Bag;
    }

    method word($/) {
        if $ + $ {
            make flat $».ast xx +$, $».ast xx +$;
        } else {
            make ();
        }
    }

    method with_c($/) {
        make "c" ~ $;
    }

    method no_c($/) {
        make "!c" ~ $;
    }
}

sub plausible($good, $bad, $msg) {
    if $good > 2*$bad {
        say "$msg: PLAUSIBLE ($good  vs. $bad ✘)";
        return True;
    } else {
        say "$msg: NOT PLAUSIBLE ($good  vs. $bad ✘)";
        return False;
    }
}

# can't use .parsefile like before due to the non-Unicode £ in this file.
my $file = slurp("1_2_all_freq.txt", :enc);
my $results = CollectWords.parse($file, :actions(CollectWords::Actions)).ast;

my $phrasetest = [&] plausible($results, $results, "I before E when not preceded by C"),
                     plausible($results, $results, "E before I when preceded by C");

say "I before E except after C: ", $phrasetest ?? "PLAUSIBLE" !! "NOT PLAUSIBLE";


  

You may also check:How to resolve the algorithm Record sound step by step in the C++ programming language
You may also check:How to resolve the algorithm Determine if a string is numeric step by step in the min programming language
You may also check:How to resolve the algorithm Empty string step by step in the Asymptote programming language
You may also check:How to resolve the algorithm File modification time step by step in the HicEst programming language
You may also check:How to resolve the algorithm Dragon curve step by step in the Racket programming language