Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function getPeriods(sChapter)
- --input string, output table
- local periods = {}
- for i = 1,#sChapter do
- if sChapter:sub(i,i) == "." then
- table.insert(periods,i)
- end
- end
- return periods
- end
- function getSentences(sChapter,periods)
- --input table, output table
- local sentences = {}
- for i,v in ipairs(periods) do
- if i == 1 then
- table.insert(sentences,sChapter:sub(2,v))
- else
- table.insert(sentences,sChapter:sub(periods[i-1]+2,periods[i]))
- end
- end
- return sentences
- end
- function getWords(sentences)
- local sentenceByWords = {}
- for i,sentence in ipairs(sentences) do
- sentenceByWords[i] = {}
- end
- for i,sentence in ipairs(sentences) do
- local spaces = {}
- for i2 = 1,#sentence do
- if sentences[i]:sub(i2,i2) == " " then
- table.insert(spaces,i2)
- end
- end
- for j,k in ipairs(spaces) do
- if j==1 then --spaces[j+1]
- table.insert(sentenceByWords[i],sentences[i]:sub(1,spaces[j]))
- elseif j==#spaces then
- table.insert(sentenceByWords[i],sentences[i]:sub(spaces[j]))
- else
- table.insert(sentenceByWords[i],sentences[i]:sub(spaces[j-1],spaces[j]))
- end
- end
- end
- return sentenceByWords
- end
- function getGoodSentences(sentences)
- local quotes = {}
- for i,v in ipairs(sentences) do
- if v then
- if string.len(v) > 200 then
- table.insert(quotes,v)
- end
- end
- end
- return quotes
- end
- --[[
- function getComplexSentences(sentences,sentenceByWords)
- local quotes = {}
- for i,v in ipairs(sentences) do
- if v then
- if string.len(v) > 200 and sentenceByWords[i]:longest() > 10 then
- table.insert(quotes,v)
- end
- end
- end
- return quotes
- end
- ]]
- function averageLen(sentences)
- local sum = 0
- for i=1,#sentences do
- sum = sum + string.len(sentences[i])
- end
- return sum / #sentences
- end
- function findLongestWord(sentenceByWords)
- local longest = ""
- local longTable = {}
- for _,v in ipairs(sentenceByWords) do
- for i = 1,#v do
- if v[i]:len() > longest:len() then
- longest = v[i]
- end
- end
- end
- return longest
- end
- local h = fs.open("chapter3","r")
- local sChapter = h.readAll()
- local periods = getPeriods(sChapter)
- local sentences = getSentences(sChapter,periods)
- local quotes = getGoodSentences(sentences)
- local wordSentences = getWords(sentences)
- --print(averageLen(sentences))
- --print(textutils.serialize(findLongestWord(wordSentences)))
- if fs.exists("result") then
- fs.delete("result")
- end
- local h = fs.open("result","w")
- h.write(textutils.serialize(quotes))
- h.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement