markov_chains_spec.lua 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package.path = "../?.lua;" .. package.path
  2. local MarkovGraph = require("markov_chains")
  3. describe("markov_chains", function()
  4. local graph = MarkovGraph()
  5. local graph2 = MarkovGraph()
  6. local trainingstr = "abc def ghi"
  7. it("repeats", function()
  8. graph:learn_line(trainingstr)
  9. assert.equals(trainingstr, graph:get_sentence())
  10. end)
  11. it("randomizes", function()
  12. graph:learn_line("abc ghi")
  13. graph:learn_line("def abc abc ghi def")
  14. local random = false
  15. for i = 1, 10
  16. do
  17. random = random or graph:get_sentence() ~= trainingstr
  18. end
  19. assert(random)
  20. assert.equals("number", type(math.huge))
  21. end)
  22. it("culls", function()
  23. assert.equals(3, graph:count_known_words())
  24. graph:cull(2)
  25. assert(graph:count_known_words() <= 2, "Doesn't cull to max size!")
  26. end)
  27. it("learns from file", function()
  28. local graph = MarkovGraph()
  29. graph:learn_from_file("tests/test_resource_learner.txt")
  30. assert.equals(8, graph:count_known_words())
  31. end)
  32. it("stores words seperately for each graph", function()
  33. assert.equals(0, graph2:count_known_words())
  34. end)
  35. it("can learn from each other", function()
  36. local graph = MarkovGraph()
  37. graph:learn_from_file("tests/test_resource_learner.txt")
  38. graph2:learn_line(graph:get_sentence())
  39. assert(graph2:count_known_words() ~= 0)
  40. end)
  41. it("culls the least used", function()
  42. local graph = MarkovGraph()
  43. graph:learn_line("abc abc def")
  44. graph:learn_line("abc")
  45. assert.equals(2, graph:count_known_words())
  46. graph:cull(1)
  47. assert.equals(1, graph:count_known_words())
  48. graph:learn_line("def")
  49. assert.equals(2, graph:count_known_words())
  50. end)
  51. it("culls circles", function()
  52. local graph = MarkovGraph()
  53. graph:learn_line("it is the elephant's fault is")
  54. graph:learn_line("it is de elefnts falt as")
  55. assert.equals(9, graph:count_known_words())
  56. graph:cull(5)
  57. assert.equals(5, graph:count_known_words())
  58. end)
  59. end)