How to resolve the algorithm I before E except after C step by step in the Wren 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 Wren 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 Wren programming language

Source code in the wren programming language

import "io" for File
import "./pattern" for Pattern
import "./fmt" for Fmt

var yesNo = Fn.new { |b| (b) ? "yes" : "no" }

var plausRatio = 2

var count1 = 0  // [^c]ie
var count2 = 0  // [^c]ei
var count3 = 0  // cie
var count4 = 0  // cei
var count5 = 0  // ^ie
var count6 = 0  // ^ei

var p1 = Pattern.new("^cie")
var p2 = Pattern.new("^cei")

var words = File.read("unixdict.txt").split("\n").map { |w| w.trim() }.where { |w| w != "" }
System.print("The following words fall into more than one category")
System.print("and so are counted more than once:")
for (word in words) {
    var tc1 = count1 + count2 + count3 + count4 + count5 + count6
    if (p1.isMatch(word)) count1 = count1 + 1
    if (p2.isMatch(word)) count2 = count2 + 1
    if (word.contains("cie")) count3 = count3 + 1
    if (word.contains("cei")) count4 = count4 + 1
    if (word.startsWith("ie")) count5 = count5 + 1
    if (word.startsWith("ei")) count6 = count6 + 1
    var tc2 = count1 + count2 + count3 + count4 + count5 + count6
    if ((tc2 -tc1) > 1) System.print("  " + word)
}

System.print("\nChecking plausability of \"i before e except after c\":")
var nFor  = count1 + count5
var nAgst = count2 + count6
var ratio = nFor / nAgst
var plaus = (ratio > plausRatio)
Fmt.print("  Cases for      : $d", nFor)
Fmt.print("  Cases against  : $d", nAgst)
Fmt.print("  Ratio for/agst : $4.2f", ratio)
Fmt.print("  Plausible      : $s", yesNo.call(plaus))

System.print("\nChecking plausability of \"e before i when preceded by c\":")
var ratio2 = count4 / count3
var plaus2 = (ratio2 > plausRatio)
Fmt.print("  Cases for      : $d", count4)
Fmt.print("  Cases against  : $d", count3)
Fmt.print("  Ratio for/agst : $4.2f", ratio2)
Fmt.print("  Plausible      : $s", yesNo.call(plaus2))

Fmt.print("\nPlausible overall: $s", yesNo.call(plaus && plaus2))


import "io" for File
import "./pattern" for Pattern
import "./fmt" for Fmt

var yesNo = Fn.new { |b| (b) ? "yes" : "no" }

var plausRatio = 2

var count1 = 0  // [^c]ie
var count2 = 0  // [^c]ei
var count3 = 0  // cie
var count4 = 0  // cei
var count5 = 0  // ^ie
var count6 = 0  // ^ei

var p0 = Pattern.new("+1/s")
var p1 = Pattern.new("^cie")
var p2 = Pattern.new("^cei")

var entries = File.read("corpus.txt").split("\n").map { |w| w.trim() }.where { |w| w != "" }
System.print("The following words fall into more than one category")
System.print("and so are counted more than their frequency:")
for (entry in entries.skip(1)) {
    var items = p0.splitAll(entry)
    if (items.count == 3) {
        var word = items[0]  // leave any trailing * in place
        var freq = Num.fromString(items[2])
        var tc1 = count1 + count2 + count3 + count4 + count5 + count6
        if (p1.isMatch(word)) count1 = count1 + freq
        if (p2.isMatch(word)) count2 = count2 + freq
        if (word.contains("cie")) count3 = count3 + freq
        if (word.contains("cei")) count4 = count4 + freq
        if (word.startsWith("ie")) count5 = count5 + freq
        if (word.startsWith("ei")) count6 = count6 + freq
        var tc2 = count1 + count2 + count3 + count4 + count5 + count6
        if ((tc2 -tc1) > freq) System.print("  " + word)
    }
}

System.print("\nChecking plausability of \"i before e except after c\":")
var nFor  = count1 + count5
var nAgst = count2 + count6
var ratio = nFor / nAgst
var plaus = (ratio > plausRatio)
Fmt.print("  Cases for      : $d", nFor)
Fmt.print("  Cases against  : $d", nAgst)
Fmt.print("  Ratio for/agst : $4.2f", ratio)
Fmt.print("  Plausible      : $s", yesNo.call(plaus))

System.print("\nChecking plausability of \"e before i when preceded by c\":")
var ratio2 = count4 / count3
var plaus2 = (ratio2 > plausRatio)
Fmt.print("  Cases for      : $d", count4)
Fmt.print("  Cases against  : $d", count3)
Fmt.print("  Ratio for/agst : $4.2f", ratio2)
Fmt.print("  Plausible      : $s", yesNo.call(plaus2))

Fmt.print("\nPlausible overall: $s", yesNo.call(plaus && plaus2))


  

You may also check:How to resolve the algorithm Knight's tour step by step in the 11l programming language
You may also check:How to resolve the algorithm RIPEMD-160 step by step in the Clojure programming language
You may also check:How to resolve the algorithm Bitmap/Read a PPM file step by step in the D programming language
You may also check:How to resolve the algorithm Tokenize a string step by step in the OCaml programming language
You may also check:How to resolve the algorithm Sorting algorithms/Bead sort step by step in the Haskell programming language