itte/examples/gordon/soupe.lua

172 lines
3.6 KiB
Lua
Raw Normal View History

2022-08-31 04:23:19 +00:00
local util = require("itteutil")
local soupe = {
max_ingred = 7,
nouns_file = "nouns.txt",
}
soupe.servings = {
bowl = {
"bronze",
"copper",
"ceramic",
"crystal",
"earthenware",
"golden",
"stone",
"glass",
"plastic",
"silver",
"styrofoam",
"tin",
"tupperware",
},
cauldron = {
"bronze",
"cast iron",
"charred",
"copper",
"golden",
"rusty",
"silver",
},
chalice = {
"bronze",
"copper",
"crystal",
"golden",
"stone",
"silver",
},
cup = {
"bronze",
"copper",
"ceramic",
"crystal",
"earthenware",
"golden",
"glass",
"plastic",
"silver",
"styrofoam",
"tin",
},
mug = {
"ceramic",
"earthenware",
"glass",
"tin",
},
pot = {
"bronze",
"copper",
"ceramic",
"earthenware",
"stone",
"plastic",
"tin",
},
portion = {
"giant",
"large",
"medium",
"small",
"tiny",
},
serving = {
"large",
"small",
},
}
soupe.temps = {
"chilled",
"cold",
"hot",
"scalding hot",
"warm",
}
soupe.articles = {
"brimming with",
"filled with",
"full of",
"glistening with",
"nearly overflowing with",
"of",
}
-- Read a parts of speech file and turn a table of nouns.
function soupe.filter_nouns(mode)
local ns = util.read_file(soupe.nouns_file)
-- The delimiter does not remove the second section of hyphenated words,
-- this is done when looping through the table and comparing the first letter
-- of each word to the previous and next words' first letters. Since the list
-- is in alphabetical order, out-of-order words are excluded.
local nt = util.split_str(ns, "(%a*)[\\N]%S+")
local nouns = {}
if mode == "common" then
for i = 1, #nt do
if (#nt[i] > 4) and
((string.sub(nt[i - 1], 1, 1) == string.sub(nt[i], 1, 1)) or
(string.sub(nt[i], 1, 1) == string.sub(nt[i + 1], 1, 1))) and
(string.match(string.sub(nt[i], 1, 1), "%l") ~= nil) then
table.insert(nouns, nt[i])
end
end
-- Includes both proper and common nouns
else
for i = 1, #nt do
if (#nt[i] > 4) and
((string.sub(nt[i - 1], 1, 1) == string.sub(nt[i], 1, 1)) or
(string.sub(nt[i], 1, 1) == string.sub(nt[i + 1], 1, 1))) then
table.insert(nouns, nt[i])
end
end
end
return nouns
end
-- Return a string description of soup.
function soupe.make_soup(items)
local ingred = items
if ingred == nil then ingred = math.random(1, soupe.max_ingred) end
local nouns = soupe.filter_nouns("common")
local servings = util.table_keys(soupe.servings)
local serving = servings[math.random(1, #servings)]
local adj = ""
local which_adj = math.random(1, 4)
if which_adj == 2 then
adj = soupe.temps[math.random(1, #soupe.temps)] .. " "
elseif which_adj == 3 then
adj = soupe.servings[serving][math.random(1, #soupe.servings[serving])] ..
" "
elseif which_adj == 4 then
adj = soupe.temps[math.random(1, #soupe.temps)] .. " " ..
soupe.servings[serving][math.random(1, #soupe.servings[serving])] ..
" "
end
local soup = adj .. serving .. " " ..
soupe.articles[math.random(1, #soupe.articles)] .. " "
if ingred == 1 then
soup = soup .. nouns[math.random(1, #nouns)]
elseif ingred == 2 then
soup = soup .. nouns[math.random(1, #nouns)] .. " and " ..
nouns[math.random(1, #nouns)]
elseif ingred > 2 then
for i = 1, (ingred - 2) do
soup = soup .. nouns[math.random(1, #nouns)] .. ", "
end
soup = soup .. nouns[math.random(1, #nouns)] .. " and " ..
nouns[math.random(1, #nouns)]
end
return soup
end
return soupe