Guest User

Untitled

a guest
Feb 19th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. //First version of Io Markov Chain generator
  2. //jamis:~/Work/Io rodney$ io Markov2.io
  3. //proposition. 5 Propositions are truth-functions of atomic facts. 3 The logical picture of //proposition. 7 Whereof one must be silent.
  4. //!! Exception: nil does not respond to 'pickWord'// ---------
  5. //nil pickWord Markov2.io 85
  6. // WordPredictor pickNext Markov2.io 98
  7.  
  8. Message do(
  9. evalLightMethod := method(scope,
  10. call message arguments slice(1) foreach(i, arg,
  11. scope setSlot(arguments at(i) name, call sender doMessage(arg, call sender))
  12. )
  13. scope doMessage(self arguments last, scope)
  14. )
  15. )
  16.  
  17. Map update := method(key,
  18. atPut(key,
  19. value := if(hasKey(key),
  20. call argAt(2) evalLightMethod(call sender, at(key))
  21. ,
  22. call evalArgAt(1)
  23. )
  24. )
  25. value
  26. )
  27.  
  28. List foreachPair := method(
  29. msg := call message
  30. last := first
  31. slice(1) foreach(v,
  32. msg evalLightMethod(call sender, last, v)
  33. last = v
  34. )
  35. )
  36.  
  37. WordCount := Object clone do(
  38. newSlot("counts")
  39. newSlot("total", 0)
  40.  
  41. init := method(
  42. counts = Map clone
  43. )
  44.  
  45. addWord := method(word,
  46. counts update(word, 1, (c, c + 1))
  47. total = total + 1
  48. )
  49.  
  50. asString := method(
  51. r := Sequence clone
  52. counts foreach(word, count,
  53. r appendSeq(word, "(", count, ") ")
  54. )
  55. r
  56. )
  57.  
  58. pickWord := method(
  59. words := counts keys
  60. loop(
  61. word := words anyOne
  62. if(counts at(word) >= Random value * total, break)
  63. )
  64. word
  65. )
  66. )
  67.  
  68. WordPredictor := Object clone do(
  69. newSlot("words")
  70.  
  71. init := method(
  72. words = Map clone
  73. )
  74.  
  75. addWordPair := method(first, second,
  76. words atIfAbsentPut(first, WordCount clone) addWord(second)
  77. )
  78.  
  79. asString := method(
  80. r := Sequence clone
  81. words foreach(word, wordCounts,
  82. r appendSeq(word, ": ", wordCounts asString, "\n")
  83. )
  84. r
  85. )
  86.  
  87. anyWord := method(
  88. words keys anyOne
  89. )
  90.  
  91. pickNext := method(current,
  92. words at(current) pickWord
  93. )
  94. )
  95.  
  96. words := WordPredictor clone
  97.  
  98. File clone setPath("thecore1_5.txt") contents splitNoEmpties foreachPair(first, second,
  99. words addWordPair(first, second)
  100. )
  101.  
  102. word := words anyWord
  103. 100 repeatTimes(
  104. write(word, " ")
  105. word = words pickNext(word)
  106. )
  107. writeln
Add Comment
Please, Sign In to add comment