How to resolve the algorithm I before E except after C step by step in the Ruby programming language
How to resolve the algorithm I before E except after C step by step in the Ruby 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 Ruby programming language
The goal of this code is to test the plausibility of the "I before E except after C" rule in English words. It uses a list of words from the Unix dictionary and counts the occurrences of "ie", "ei", "cie", and "cei" patterns in those words. Then, it checks if the ratio of "ie" to "ei" and "cei" to "cie" occurrences is greater than a specified plausibility ratio, which is 2 in this case.
Here's a detailed explanation of the code:
-
Loading the Word List:
- The code opens a URL
http://wiki.puzzlers.org/pub/wordlists/unixdict.txt
that contains a list of words.
- The code opens a URL
-
Initializing Variables:
plausibility_ratio
is set to 2, which represents the threshold for determining if a rule is plausible.counter
is a Hash that will store the counts of occurrences for different patterns.
-
Processing the Word List:
- The code iterates through each line in the word list using
f.each
. - For each line, it scans for the patterns "ie", "ei", "cie", or "cei" using
line.scan(/ie|ei|cie|cei/)
. - Each match is added to the
counter
Hash, which counts how many times each pattern appears in the word list.
- The code iterates through each line in the word list using
-
Testing the Rules:
rules
is a list of tuples containing the rule description, the expected pattern (e.g., "ie"), and the alternative pattern (e.g., "ei").- The code iterates through each rule in
rules
usingrules.all?
. - For each rule, it:
- Retrieves the expected pattern count (
num_x
) and the alternative pattern count (num_y
) from thecounter
Hash. - Calculates the ratio of
num_x
tonum_y
. - Checks if the ratio is greater than the
plausibility_ratio
. - Prints the rule description and the counts for the patterns.
- Determines if the rule is plausible or implausible based on the ratio.
- Returns true if the rule is plausible, false otherwise.
- Retrieves the expected pattern count (
-
Overall Plausibility:
- The
overall_plausibility
variable is set to the result of therules.all?
loop. It represents whether all the rules were considered plausible.
- The
-
Printing the Overall Result:
- The code prints whether the overall result is plausible or implausible.
Source code in the ruby programming language
require 'open-uri'
plausibility_ratio = 2
counter = Hash.new(0)
path = 'http://wiki.puzzlers.org/pub/wordlists/unixdict.txt'
rules = [['I before E when not preceded by C:', 'ie', 'ei'],
['E before I when preceded by C:', 'cei', 'cie']]
open(path){|f| f.each{|line| line.scan(/ie|ei|cie|cei/){|match| counter[match] += 1 }}}
overall_plausible = rules.all? do |(str, x, y)|
num_x, num_y, ratio = counter[x], counter[y], counter[x] / counter[y].to_f
plausibility = ratio > plausibility_ratio
puts str
puts "#{x}: #{num_x}; #{y}: #{num_y}; Ratio: #{ratio.round(2)}: #{ plausibility ? 'Plausible' : 'Implausible'}"
plausibility
end
puts "Overall: #{overall_plausible ? 'Plausible' : 'Implausible'}."
You may also check:How to resolve the algorithm Soundex step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Factors of an integer step by step in the E programming language
You may also check:How to resolve the algorithm Penney's game step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Literals/Integer step by step in the LiveCode programming language
You may also check:How to resolve the algorithm Anagrams step by step in the SuperCollider programming language