How to resolve the algorithm I before E except after C step by step in the Common Lisp programming language
How to resolve the algorithm I before E except after C step by step in the Common Lisp 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 Common Lisp programming language
Source code in the common programming language
(defun test-rule (rule-name examples counter-examples)
(let ((plausible (if (> examples (* 2 counter-examples)) 'plausible 'not-plausible)))
(list rule-name plausible examples counter-examples)))
(defun plausibility (result-string file parser)
(let ((cei 0) (cie 0) (ie 0) (ei 0))
(macrolet ((search-count (&rest terms)
(when terms
`(progn
(when (search ,(string-downcase (symbol-name (car terms))) word)
(incf ,(car terms) freq))
(search-count ,@(cdr terms))))))
(with-open-file (stream file :external-format :latin-1)
(loop :for raw-line = (read-line stream nil 'eof)
:until (eq raw-line 'eof)
:for line = (string-trim '(#\Tab #\Space) raw-line)
:for (word freq) = (funcall parser line)
:do (search-count cei cie ie ei))
(print-result result-string cei cie ie ei)))))
(defun print-result (result-string cei cie ie ei)
(let ((results (list (test-rule "I before E when not preceded by C" (- ie cie) (- ei cei))
(test-rule "E before I when preceded by C" cei cie))))
(format t "~a:~%~{~{~2TThe rule \"~a\" is ~S. There were ~a examples and ~a counter-examples.~}~^~%~}~%~%~2TOverall the rule is ~S~%~%"
result-string results (or (find 'not-plausible (mapcar #'cadr results)) 'plausible))))
(defun parse-dict (line) (list line 1))
(defun parse-freq (line)
(list (subseq line 0 (position #\Tab line))
(parse-integer (subseq line (position #\Tab line :from-end t)) :junk-allowed t)))
(plausibility "Dictionary" #p"unixdict.txt" #'parse-dict)
(plausibility "Word frequencies (stretch goal)" #p"1_2_all_freq.txt" #'parse-freq)
You may also check:How to resolve the algorithm Substring step by step in the Clojure programming language
You may also check:How to resolve the algorithm Compare length of two strings step by step in the Go programming language
You may also check:How to resolve the algorithm Animation step by step in the Pascal programming language
You may also check:How to resolve the algorithm Rate counter step by step in the Erlang programming language
You may also check:How to resolve the algorithm Short-circuit evaluation step by step in the Nemerle programming language