Advertisement
guitarplayer616

GetQuotes

Nov 29th, 2016
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.50 KB | None | 0 0
  1.  
  2. function getPeriods(sChapter)
  3.     --input string, output table
  4.     local periods = {}
  5.     for i = 1,#sChapter do
  6.         if sChapter:sub(i,i) == "." then
  7.             table.insert(periods,i)
  8.         end
  9.     end
  10.     return periods
  11. end
  12.  
  13. function getSentences(sChapter,periods)
  14.     --input table, output table
  15.     local sentences = {}
  16.     for i,v in ipairs(periods) do
  17.         if i == 1 then
  18.             table.insert(sentences,sChapter:sub(2,v))
  19.         else
  20.             table.insert(sentences,sChapter:sub(periods[i-1]+2,periods[i]))
  21.         end
  22.     end
  23.     return sentences
  24. end
  25.  
  26. function getWords(sentences)
  27.     local sentenceByWords = {}
  28.     for i,sentence in ipairs(sentences) do
  29.         sentenceByWords[i] = {}
  30.     end
  31.     for i,sentence in ipairs(sentences) do
  32.         local spaces = {}
  33.         for i2 = 1,#sentence do
  34.             if sentences[i]:sub(i2,i2) == " " then
  35.                 table.insert(spaces,i2)
  36.             end
  37.         end
  38.         for j,k in ipairs(spaces) do
  39.             if j==1 then --spaces[j+1]
  40.                 table.insert(sentenceByWords[i],sentences[i]:sub(1,spaces[j]))
  41.             elseif j==#spaces then
  42.                 table.insert(sentenceByWords[i],sentences[i]:sub(spaces[j]))
  43.             else
  44.                 table.insert(sentenceByWords[i],sentences[i]:sub(spaces[j-1],spaces[j]))
  45.             end
  46.         end
  47.     end
  48.     return sentenceByWords
  49. end
  50.  
  51.  
  52.  
  53. function getGoodSentences(sentences)
  54.     local quotes = {}
  55.     for i,v in ipairs(sentences) do
  56.         if v then
  57.             if string.len(v) > 200 then
  58.                 table.insert(quotes,v)
  59.             end
  60.         end
  61.     end
  62.     return quotes
  63. end
  64.  
  65. --[[
  66. function getComplexSentences(sentences,sentenceByWords)
  67.     local quotes = {}
  68.     for i,v in ipairs(sentences) do
  69.         if v then
  70.             if string.len(v) > 200 and sentenceByWords[i]:longest() > 10 then
  71.                 table.insert(quotes,v)
  72.             end
  73.         end
  74.     end
  75.     return quotes
  76. end
  77. ]]
  78.  
  79. function averageLen(sentences)
  80.     local sum = 0
  81.     for i=1,#sentences do
  82.         sum = sum + string.len(sentences[i])
  83.     end
  84.     return sum / #sentences
  85. end
  86.  
  87. function findLongestWord(sentenceByWords)
  88.     local longest = ""
  89.     local longTable = {}
  90.     for _,v in ipairs(sentenceByWords) do
  91.         for i = 1,#v do
  92.             if v[i]:len() > longest:len() then
  93.                 longest = v[i]
  94.             end
  95.         end
  96.     end
  97.     return longest
  98. end
  99.  
  100. local h = fs.open("chapter3","r")
  101. local sChapter = h.readAll()
  102. local periods = getPeriods(sChapter)
  103. local sentences = getSentences(sChapter,periods)
  104. local quotes = getGoodSentences(sentences)
  105. local wordSentences = getWords(sentences)
  106. --print(averageLen(sentences))
  107. --print(textutils.serialize(findLongestWord(wordSentences)))
  108.  
  109. if fs.exists("result") then
  110.     fs.delete("result")
  111. end
  112.  
  113. local h = fs.open("result","w")
  114. h.write(textutils.serialize(quotes))
  115. h.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement