RosettaCodeData/Task/I-before-E-except-after-C/Lua/i-before-e-except-after-c.lua

33 lines
1,009 B
Lua
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
-- Needed to get dictionary file from web server
local http = require("socket.http")
-- Return count of words that contain pattern
function count (pattern, wordList)
local total = 0
for word in wordList:gmatch("%S+") do
if word:match(pattern) then total = total + 1 end
end
return total
end
-- Check plausibility of case given its opposite
function plaus (case, opposite, words)
if count(case, words) > 2 * count(opposite, words) then
print("PLAUSIBLE")
return true
else
print("IMPLAUSIBLE")
return false
end
end
-- Main procedure
2026-02-01 16:33:20 -08:00
local page = http.request("https://web.archive.org/web/20240920144647if_/http://wiki.puzzlers.org/pub/wordlists/unixdict.txt")
2023-07-01 11:58:00 -04:00
io.write("I before E when not preceded by C: ")
local sub1 = plaus("[^c]ie", "cie", page)
io.write("E before I when preceded by C: ")
local sub2 = plaus("cei", "[^c]ei", page)
io.write("Overall the phrase is ")
if not (sub1 and sub2) then io.write("not ") end
print("plausible.")