tetext = [[
Once upon a time, in a small village nestled between rolling hills and lush forests, there lived a curious young girl named Elara. She had an insatiable hunger for adventure and knowledge, always exploring the boundaries of her village and delving into the depths of the surrounding woods. The villagers often told tales of magical creatures and ancient spirits residing in the forests, and Elara was determined to uncover the truth behind these stories.
One day, as she wandered deeper into the forest than ever before, Elara stumbled upon an old, hidden path. The path was overgrown with vines and flowers, and it seemed as though no one had traveled it in many years. Driven by her curiosity, she decided to follow the path, unaware of where it might lead.
As the sun began to dip below the horizon, casting an amber glow through the canopy of leaves, Elara found herself standing before an ancient, towering tree. Its trunk was wide, and its roots stretched deep into the earth, while its branches seemed to touch the sky. There was something mystical about the tree, and Elara felt an inexplicable pull towards it.
Then, she noticed a peculiar marking on the tree's bark—a symbol that looked like a swirling star. As she traced the symbol with her fingers, a gentle breeze stirred the leaves, and suddenly, an entrance revealed itself at the base of the tree. Hesitating only for a moment, Elara stepped inside.
Inside, the tree was hollow, but it wasn’t dark or empty. It was filled with a warm, golden light that illuminated the entire space. Books lined the walls from floor to ceiling, and a small, wooden desk sat in the center, cluttered with quills, ink pots, and parchments. It was as if she had discovered a secret library hidden away from the world.
Elara spent hours exploring the library, reading tales of far-off lands, legendary heroes, and magical realms. She learned about the language of the stars, the secrets of the ocean, and the wisdom of the ancient trees. With each turn of a page, her excitement grew, and she knew she had found something truly extraordinary.
From that day on, whenever she needed an escape or sought wisdom, Elara returned to the hidden library inside the ancient tree. Her adventures in the mystical forest had only just begun, and she knew there were countless more wonders waiting to be discovered.
]]
-- Функция для получения всех слов из входного потока
function allwords()
local line = tetext -- текущая строка
local pos = 1 -- текущая позиция в строке
return function() -- итерирующая функция
while line do -- повторять, пока есть строки
local s, e = string.find(line, "%w+", pos)
if s then -- слово найдено?
pos = e + 1 -- обновить следующую позицию
return string.sub(line, s, e) -- вернуть слово
else
line = io.read() -- слово не найдено; пробовать следующую строку
pos = 1 -- перезапуск с первой позиции
end
end
return nil -- строк больше нет; конец обхода
end
end
-- Функция для создания префикса из двух слов
function prefix(w1, w2)
return w1 .. " " .. w2
end
local statetab = {}
-- Функция для вставки слова в таблицу состояний
function insert(index, value)
local list = statetab[index]
if list == nil then
statetab[index] = {value}
else
list[#list + 1] = value
end
end
-- Основная часть программы
local N = 2
local MAXGEN = 10000
local NOWORD = "\n"
-- Строит таблицу
local w1, w2 = NOWORD, NOWORD
for w in allwords() do
insert(prefix(w1, w2), w)
w1 = w2
w2 = w
end
insert(prefix(w1, w2), NOWORD)
-- Генерирует текст
w1 = NOWORD
w2 = NOWORD -- новая инициализация
for i = 1, MAXGEN do
local list = statetab[prefix(w1, w2)]
-- Наугад выбирает элемент из списка
local r = math.random(#list)
local nextword = list[r]
if nextword == NOWORD then return end
io.write(nextword, " ")
w1 = w2
w2 = nextword
end
чому код ничо не печатает а ток дает вечное приглашение ввода?