Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- DATA FORMAT (nbt)
- {
- VERSION:0s,
- DEPTH = 4b, --How many words back it searches
- LOADED_FILES:{str filepath:true,str filepath2:true},
- STARTER_WORDS:{
- __COUNT:int numStarterWords,
- word:short frequency,
- ...
- },
- NEXT_WORDS:{
- __COUNT:int number of words,
- word:[
- {
- next_word:short frequency,
- ...
- }, --most common next words
- {
- next_word:short frequency,
- ...
- }, --most common next words 2 from now
- ...
- ],
- ...
- },
- KEYWORD_BIAS:{
- __COUNT:int number of keywords,
- word:{
- next_word:short frequency,
- ...
- } --How often the next_word's are seen with word as a keyword
- ]]
- local nbt = require("nbt")
- local zip = require("LibDeflate")
- local expect = require("cc.expect").expect
- local VERSION,DEPTH,WORD_STRING_LENGTH = 0,16,2
- local data_path = "GPT_DATA.nbt"
- local gpt_data = nbt.COMPOUND()
- if fs.exists(data_path) then
- local f = fs.open(data_path,"r")
- local data = f.readAll()
- f.close()
- local bin = assert(zip:DecompressGzip(data))
- gpt_data = assert(nbt.frombinary(bin))
- else
- gpt_data.VERSION = nbt.SHORT(VERSION)
- gpt_data.DEPTH = nbt.BYTE(DEPTH)
- gpt_data.LOADED_FILES = nbt.COMPOUND()
- gpt_data.STARTER_WORDS = nbt.COMPOUND({__COUNT=nbt.INT(0)})
- gpt_data.NEXT_WORDS = nbt.COMPOUND({__COUNT=nbt.INT(0)})
- gpt_data.KEYWORD_BIAS = nbt.COMPOUND({__COUNT=nbt.INT(0)})
- gpt_data.WORD_STRING_LENGTH = nbt.SHORT(WORD_STRING_LENGTH)
- end
- local GPT = {}
- local function split(input,sep)
- local t = {}
- for str in input:gmatch("[^"..sep.."]+") do
- table.insert(t,str)
- end
- return t
- end
- local function isKeyword(word)
- return #word > 15 or (#word > 10 and math.random(10) == 10)
- end
- local function removeLastWords(tLastWords,tKeywords)
- local word = table.remove(tLastWords,1)
- for index,next_word in pairs(tLastWords) do
- local wList = gpt_data.NEXT_WORDS[word]
- if not wList then wList = nbt.LIST(nbt.tags.COMPOUND) end
- if not wList[index] then wList[index] = nbt.COMPOUND() end
- wList[index][next_word] = nbt.SHORT((wList[index][next_word] or 0) + 1)
- gpt_data.NEXT_WORDS.__COUNT = nbt.INT(gpt_data.NEXT_WORDS.__COUNT + 1)
- gpt_data.NEXT_WORDS[word] = wList
- end
- for keyword in pairs(tKeywords) do
- if not gpt_data.KEYWORD_BIAS[keyword] then
- gpt_data.KEYWORD_BIAS[keyword] = nbt.COMPOUND()
- end
- gpt_data.KEYWORD_BIAS[keyword][word] = nbt.SHORT((gpt_data.KEYWORD_BIAS[keyword][word] or 0)+1)
- gpt_data.KEYWORD_BIAS.__COUNT = gpt_data.KEYWORD_BIAS.__COUNT + 1
- end
- end
- function GPT.loadText(txt)
- expect(1,txt,"string")
- local depth = nbt.tonumber(gpt_data.DEPTH)
- for paragraph in txt:gmatch("[^\n]+") do
- local tLastWords,tKeywords = {},{}
- local index = 0
- for word in paragraph:gmatch("[^%s]+") do
- table.insert(tLastWords,word)
- if isKeyword(word) then
- tKeywords[word] = true
- end
- if #tLastWords > depth then
- removeLastWords(tLastWords,tKeywords)
- end
- if index == 0 then
- gpt_data.STARTER_WORDS[word] = nbt.SHORT((gpt_data.STARTER_WORDS[word] or 0) + 1)
- gpt_data.STARTER_WORDS.__COUNT = gpt_data.STARTER_WORDS.__COUNT + 1
- end
- index = index + 1
- end
- for i=1,#tLastWords do
- table.insert(tLastWords,"__EOF")
- removeLastWords(tLastWords,tKeywords)
- end
- end
- end
- function GPT.getData()
- return gpt_data
- end
- --tLastWords are newest word first and oldest word last
- function GPT.getNextWord(tLastWords,tKeywords)
- expect(1,tLastWords,"table","nil")
- expect(2,tKeywords,"table","nil")
- if (tLastWords == nil) or (next(tLastWords) == nil) then
- local target = math.random(nbt.tonumber(gpt_data.STARTER_WORDS.__COUNT))
- for k,v in pairs(gpt_data.STARTER_WORDS) do
- if k ~= "__COUNT" then
- if target <= nbt.tonumber(v) then return k else target = target - v end
- end
- end
- return nil
- else
- local tWordFreqs,tWordFreqsRaw = {},{}
- local count = 0
- for index,word in pairs(tLastWords) do
- if gpt_data.NEXT_WORDS[word] then
- for k,v in pairs(gpt_data.NEXT_WORDS[word][index] or {}) do
- --OLD
- --v = math.floor(v*1.1^(gpt_data.DEPTH - index)) --scale bias towards more recent messages
- --tWordFreqs[k] = (tWordFreqs[k] or 0)+v
- --count = count + v
- --OLD
- --NEW
- -- if tWordFreqs[k] or index == 1 then --only allow words that follow the message
- -- tWordFreqs[k] = (tWordFreqs[k] or 0)+v
- -- count = count + v
- -- end
- if not tWordFreqsRaw[k] then tWordFreqsRaw[k] = {} end
- tWordFreqsRaw[k][1] = (tWordFreqsRaw[k][1] or 0)+v
- if tWordFreqsRaw[k][2] then
- tWordFreqsRaw[k][2][index] = true
- else
- tWordFreqsRaw[k][2] = {[index] = true}
- end
- --NEW
- end
- end
- end
- local word_depth = nbt.tonumber(gpt_data.WORD_STRING_LENGTH)
- for k,v in pairs(tWordFreqsRaw) do
- local flag = true
- for i=1,word_depth do
- if not v[2][i] then
- flag = ((not tLastWords) or #tLastWords < word_depth)
- break
- end
- end
- if flag then
- tWordFreqs[k] = v[1]
- count = count + v[1]
- end
- end
- if nbt.tonumber(count) == 0 then return nil end
- if tKeywords then
- local tKeywordBias = {}
- for index,keyword in pairs(tKeywords) do
- for word,value in pairs(gpt_data.KEYWORD_BIAS[keyword] or {}) do
- tKeywordBias[word] = (tKeywordBias[word] or 0) + value
- end
- end
- for word,value in pairs(tKeywordBias) do
- local scaleFactor = 1.1^value
- if tWordFreqs[word] then
- count = count - tWordFreqs[word] + math.floor(tWordFreqs[word]*scaleFactor)
- tWordFreqs[word] = math.floor(tWordFreqs[word]*scaleFactor)
- end
- end
- end
- local target = math.random(count)
- for word,frequency in pairs(tWordFreqs) do
- if frequency >= target then
- if word == "__EOF" then return nil else return word end
- else target = target - frequency end
- end
- end
- return nil
- end
- function GPT.finishSentence(inputstr,tKeywords)
- expect(1,inputstr,"string")
- local str = ""
- local tWords = {}
- for s in inputstr:gmatch("%S+") do
- table.insert(tWords,1,s)
- end
- local depth = nbt.tonumber(gpt_data.DEPTH)
- while #tWords > depth do table.remove(tWords) end
- local word = GPT.getNextWord(tWords,tKeywords)
- while word do
- str = str .. word .. " "
- table.insert(tWords,1,word)
- if #tWords > depth then table.remove(tWords) end
- word = GPT.getNextWord(tWords,tKeywords)
- end
- return str:gsub(" $","")
- end
- function GPT.getSentence(tKeywords)
- expect(1,tKeywords,"table",'nil')
- local str = ""
- local word = GPT.getNextWord(nil,tKeywords)
- local depth = nbt.tonumber(gpt_data.DEPTH)
- local tWords = {}
- while word do
- str = str .. word .. " "
- table.insert(tWords,1,word)
- if #tWords > depth then table.remove(tWords) end
- word = GPT.getNextWord(tWords,tKeywords)
- end
- return str:gsub(" $","")
- end
- function GPT.save()
- local bin = assert(nbt.tobinary(gpt_data))
- local com = assert(zip:CompressGzip(bin))
- local f = fs.open(data_path,"w+")
- f.write(com)
- f.close()
- end
- function GPT.loadFile(path)
- expect(1,path,"string")
- if gpt_data.LOADED_FILES[path] then return nil,"file already loaded" end
- local f = assert(fs.open(path,"r"))
- GPT.loadText(f.readAll())
- f.close()
- gpt_data.LOADED_FILES[path] = nbt.BYTE(true)
- return true
- end
- return GPT
Advertisement
Add Comment
Please, Sign In to add comment