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

  1. Loading the Word List:

    • The code opens a URL http://wiki.puzzlers.org/pub/wordlists/unixdict.txt that contains a list of words.
  2. 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.
  3. 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.
  4. 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 using rules.all?.
    • For each rule, it:
      1. Retrieves the expected pattern count (num_x) and the alternative pattern count (num_y) from the counter Hash.
      2. Calculates the ratio of num_x to num_y.
      3. Checks if the ratio is greater than the plausibility_ratio.
      4. Prints the rule description and the counts for the patterns.
      5. Determines if the rule is plausible or implausible based on the ratio.
      6. Returns true if the rule is plausible, false otherwise.
  5. Overall Plausibility:

    • The overall_plausibility variable is set to the result of the rules.all? loop. It represents whether all the rules were considered plausible.
  6. 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