function allwords () local line = tetext -- текущая строка local pos = 1 -- текущая…
- User
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
tetext = [[ When we apply a word frequency program to a text, the most frequent words are usually short, uninteresting words like articles and prepositions. Change the program so that it skips words that are less than four letters long. ]]
поправь чтобы работало :3
- Assistant
Конечно, давайте поправим функцию
allwords, чтобы она искала только слова длиной не менее четырех символов. Мы также добавим локальную переменнуюtetextи изменим её позицию в коде, чтобы она находилась выше функции . Вот исправленный код: - User
дай для тестов длинный текст на англ
- Assistant
Конечно! Вот пример длинного текста на английском языке, который вы можете использовать для тестирования:
tetext = [[ In the year 2020, the world faced an unprecedented challenge as the COVID-19 pandemic swept across the globe. People from all walks of life were affected, and nations had to come together to combat the spread of the virus. The pandemic led to widespread changes in how societies functioned, with lockdowns, remote work, and social distancing becoming the norm. Despite the challenges, communities showed incredible resilience and adaptability. Healthcare workers emerged as heroes, working tirelessly to care for infected patients and save lives. Scientists worked at unprecedented speeds to develop vaccines, which were rolled out to the public in record time. The pandemic also had a significant impact on the global economy, leading to job losses, business closures, and financial hardships for many individuals and families. Education systems across the world had to pivot to online learning, presenting both opportunities and challenges for students and educators. While some adapted quickly to the new mode of learning, others faced difficulties due to lack of access to technology or stable internet connections. Throughout the crisis, technology played a crucial role in keeping people connected, informed, and entertained. Video conferencing tools became essential for remote work, virtual events, and maintaining social connections. Streaming services saw a surge in usage as people spent more time at home seeking entertainment. As countries slowly began to recover and adapt to the new normal, the pandemic served as a reminder of the importance of global solidarity, innovation, and preparedness in the face of unexpected challenges. It also underscored the resilience of the human spirit and our ability to overcome adversity when we work together. ]] - User
давай другой
- Assistant