How to resolve the algorithm I before E except after C step by step in the Clojure programming language
How to resolve the algorithm I before E except after C step by step in the Clojure 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 Clojure programming language
Source code in the clojure programming language
(ns i-before-e.core
(:require [clojure.string :as s])
(:gen-class))
(def patterns {:cie #"cie" :ie #"(?<!c)ie" :cei #"cei" :ei #"(?<!c)ei"})
(defn update-counts
"Given a map of counts of matching patterns and a word, increment any count if the word matches it's pattern."
[counts [word freq]]
(apply hash-map (mapcat (fn [[k v]] [k (if (re-seq (patterns k) word) (+ freq v) v)]) counts)))
(defn count-ie-ei-combinations
"Update counts of all ie and ei combinations"
[words]
(reduce update-counts {:ie 0 :cie 0 :ei 0 :cei 0} words))
(defn apply-freq-1
"Apply a frequency of one to words"
[words]
(map #(vector % 1) words))
(defn- format-plausible
[plausible?]
(if plausible? "plausible" "implausible"))
(defn- apply-rule [desc examples contra]
(let [plausible? (<= (* 2 contra) examples)]
(println (format "The sub rule %s is %s. There are %d examples and %d counter-examples.\n" desc (format-plausible plausible?) examples contra))
plausible?))
(defn i-before-e-except-after-c-plausible?
"Check if i before e after c plausible?"
[description words]
(do
(println description)
(let [counts (count-ie-ei-combinations words)
subrule1 (apply-rule "I before E when not preceeded by C" (:ie counts) (:ei counts))
subrule2 (apply-rule "E before I when preceeded by C" (:cei counts) (:cie counts))
rule (and subrule1 subrule2)]
(println (format "Overall the rule 'I before E except after C' is %s" (format-plausible rule)))
rule)))
(defn format-freq-line [line] (letfn [(format-line [xs] [(first xs) (read-string (last xs))])]
(-> line
s/trim
(s/split #"\s")
format-line)))
(defn -main []
(with-open [rdr (clojure.java.io/reader "http://wiki.puzzlers.org/pub/wordlists/unixdict.txt")]
(i-before-e-except-after-c-plausible? "Check unixdist list" (apply-freq-1 (line-seq rdr))))
(with-open [rdr (clojure.java.io/reader "http://ucrel.lancs.ac.uk/bncfreq/lists/1_2_all_freq.txt")]
(i-before-e-except-after-c-plausible? "Word frequencies (stretch goal)" (map format-freq-line (drop 1 (line-seq rdr))))))
You may also check:How to resolve the algorithm Guess the number step by step in the Bracmat programming language
You may also check:How to resolve the algorithm CSV data manipulation step by step in the PL/I programming language
You may also check:How to resolve the algorithm Function composition step by step in the Ring programming language
You may also check:How to resolve the algorithm Caesar cipher step by step in the Jsish programming language
You may also check:How to resolve the algorithm Currying step by step in the GDScript programming language