Guest User

Untitled

a guest
Aug 30th, 2014
244
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Name generation unit
  2.  
  3. markov = {
  4.  
  5.     -- Set of working letters and digits ('#' and '$' represents starting and ending word boundary respectively)
  6.     letters = {
  7.         'a', 'b', 'c', 'd', 'e',
  8.         'f', 'g', 'h', 'i', 'j',
  9.         'k', 'l', 'm', 'n', 'o',
  10.         'p', 'q', 'r', 's', 't',
  11.         'u', 'v', 'w', 'x', 'y',
  12.         'z', 'A', 'B', 'C', 'D',
  13.         'E', 'F', 'G', 'H', 'I',
  14.         'J', 'K', 'L', 'M', 'N',
  15.         'O', 'P', 'Q', 'R', 'S',
  16.         'T', 'U', 'V', 'W', 'X',
  17.         'Y', 'Z', ' ', '-', '_',
  18.         '#', '$'
  19.     },
  20.  
  21.     -- The dictionary itself
  22.     dictionary = {},
  23.    
  24.     -- Creates the dictionary setting all letter transitions to zero
  25.     createdictionary = function(athis)
  26.         local lletter1, lletter2
  27.         for lletter1 = 1, 57 do
  28.             for lletter2 = 1, 57 do
  29.                 print('Setting frequency of : ' .. letters[lletter1] .. ' -> ' .. letters[lletter2] .. ' to zero.')
  30.                 athis.dictionary[letters[lletter1] .. letters[lletter2]] = 0
  31.             end
  32.         end
  33.     end,
  34.    
  35.     -- Increases the frequency of a certain letter transition
  36.     increasefrequency = function(athis, aletter1, aletter2)
  37.         print('Increase frequency of : ' .. aletter1 .. ' -> ' .. aletter2)
  38.         athis.dictionary[aletter1 .. aletter2] = athis.dictionary[aletter1 .. aletter2] + 1
  39.     end,
  40.    
  41.     -- Normalizes all letter frequencies to within 0..100 range
  42.     normalizefrequencies = function(athis, aletter1, aletter2)
  43.         local lmax = 0
  44.         local lmin = 0
  45.         local lletter1, lletter2
  46.         for lletter1 = 1, 57 do
  47.             for lletter2 = 1, 57 do
  48.                 if athis.dictionary[letters[lletter1] .. letters[lletter2]] > lmax then
  49.                     lmax = athis.dictionary[letters[lletter1] .. letters[lletter2]]
  50.                 elseif athis.dictionary[letters[lletter1] .. letters[lletter2]] > lmin then
  51.                     lmin = athis.dictionary[letters[lletter1] .. letters[lletter2]]
  52.                 else
  53.                 end
  54.             end
  55.         end
  56.         local lsize = lmax - lmin
  57.         local lratio = lsize / 100
  58.         for lletter1 = 1, 57 do
  59.             for lletter2 = 1, 57 do
  60.                 athis.dictionary[letters[lletter1] .. letters[lletter2]] = athis.dictionary[letters[lletter1] .. letters[lletter2]] / lratio
  61.             end
  62.         end
  63.     end,
  64.    
  65.     -- Adds a word to the dictionary, increasing the letters transitions accordingly
  66.     addword = function(athis, aword)
  67.         local lword = '#' .. aword .. '$'
  68.         print('Adding word to the dictionary : ' .. lword)
  69.         for lchar = 1, strlen(lword) - 1 do
  70.             athis:increasefrequency(string.sub(lword, lchar, 2))
  71.         end
  72.     end,
  73.    
  74.     -- Gets a row of frequencies from a certain letter aletter towards each other letter frequency
  75.     -- Applies a random factor to their frequencies
  76.     getfrequencies = function(athis, aletter)
  77.         local result = {}
  78.         for lletter = 1, 57 do
  79.             local freq = athis.dictionary[aletter .. letters[lletter]]
  80.             print('Frequency of ' .. aletter .. ' -> ' .. letters[lletter] .. ' is ' .. freq)
  81.             result[letters[lletter]] = freq * math.random() * math.random(25)
  82.         end
  83.     end,
  84.    
  85.     -- Get the most probably next letter from a certain letter
  86.     getnextletter = function(athis, astartingletter)
  87.         local lfreq = athis:getfrequencies(astartingletter)
  88.         local selletter = '$'
  89.         local selletterfreq = -100
  90.         for lletter = 1, 57 do
  91.             if lfreq[letters[lletter]] > selletterfreq then
  92.                 selletter = letters[lletter]
  93.                 selletterfreq = lfreq[letters[lletter]]
  94.             end
  95.         end
  96.     end,
  97.    
  98.     -- Concatenates a letter into a word
  99.     appendletter = function(athis, alump, anextletter)
  100.         if anextletter = '#' then
  101.         elseif anextletter = '$' then
  102.         else
  103.             return alump .. anextletter
  104.         end
  105.     end,
  106.    
  107.     -- Generates a new word
  108.     generateword = function(athis)
  109.         local lnextletter = athis.letters[math.random(57)]
  110.         while lnextletter = '#' or lnextletter = '$' do
  111.             lnextletter = athis.letters[math.random(57)]
  112.         end
  113.         local lword = ''
  114.         while lnextletter ~= '$' do
  115.             athis:appendletter(lword, lnextletter)
  116.             lnextletter = athis:getnextletter(lnextletter)
  117.         end
  118.         return lword
  119.     end
  120. }
RAW Paste Data