Skip to content

Commit de2ce66

Browse files
Refactoring sweep.
Also renamed some stuff to more strictly conform to convention.
1 parent 5a01149 commit de2ce66

File tree

4 files changed

+51
-47
lines changed

4 files changed

+51
-47
lines changed

Markov.coffee

+10-6
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@ github.com/SyntaxColoring/Markov-Word-Generator
44
55
Released under the MIT license.
66
7-
Copyright (c) 2013 SyntaxColoring
7+
Copyright (c) 2013 Max Marrone
8+
89
Permission is hereby granted, free of charge, to any person obtaining a copy of
910
this software and associated documentation files (the "Software"), to deal in
1011
the Software without restriction, including without limitation the rights to
1112
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
1213
the Software, and to permit persons to whom the Software is furnished to do so,
1314
subject to the following conditions:
15+
1416
The above copyright notice and this permission notice shall be included in all
1517
copies or substantial portions of the Software.
18+
1619
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1720
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
1821
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
@@ -26,16 +29,16 @@ class Markov
2629
# (to collectively use as a corpus) and value for n (to use as the Markov order).
2730
# sequences may be empty. n must be an integer no lower than 0.
2831
# Feel free to directly access and modify an object's .sequences and .n.
29-
constructor: (@sequences = [], @n = 2) ->
32+
constructor: (@sequences = [], @n = 2, @maxLength = 20) ->
3033

3134
# Generates a new pseudorandom sequence generated by the Markov chain and
32-
# returns it as an array. Its length will be truncated to maxLength if necessary.
33-
generate: (maxLength = 20) ->
35+
# returns it as an array. Its length will be truncated to @maxLength if necessary.
36+
generate: ->
3437
result = []
3538
currentState = => # Returns at most the last @ elements of result.
3639
result[Math.max(0, result.length-@n)...result.length]
3740
continuation = => @continue(currentState())
38-
while result.length < maxLength and (nextElement = continuation())?
41+
while result.length < @maxLength and (nextElement = continuation())?
3942
result.push nextElement
4043
return result
4144

@@ -101,4 +104,5 @@ class Markov
101104
if sum >= target then return continuationName
102105
return null # Either the node was null or it had no continuations.
103106

104-
(exports ? window).Markov = Markov
107+
if module? then module.exports = Markov
108+
else this.Markov = Markov

ReadMe.md

+14-12
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Please contribute. I'm so lonely.
2626
Report issues [here](https://github.com/SyntaxColoring/Markov-Word-Generator/issues)
2727
and I'll gladly look into them.
2828

29-
[Markov.coffee](Markov.coffee)
29+
[Markov.coffee](markov.coffee)
3030
==============================
3131
The back-end logic for managing the Markov chain is available as a standalone
3232
CoffeeScript module. It's flexible enough for you to use in your own project,
@@ -53,21 +53,23 @@ Setup
5353
-----
5454
After including the script, `Markov` objects can be constructed like this:
5555

56-
markov = new window.Markov ["sassafras", "mississippi"], 1
56+
markov = new Markov ["sassafras", "mississippi"], 1
5757

5858
# Or, on CommonJS:
59-
# markov = new Markov.Markov ["sassafras", "mississippi"], 1
59+
# Markov = require "./markov"
60+
# markov = new Markov ["sassafras", "mississippi"], 1
6061

6162
The first parameter to the constructor is an array of sequences. The sequences
6263
are combined together to form the corpus. The generator takes care not to link
6364
elements across sequence boundaries. In the example above, the last S in *sassafras*
64-
is not associated with the M in *mississippi.* If you really *do* want that to happen,
65-
here's how to do it:
65+
is not associated with the M in *mississippi.* If you really *do* want those letters
66+
to be associated, here's how to do it:
6667

67-
markov = new window.Markov["sassafrasmississippi"], 1
68+
markov = new Markov ["sassafrasmississippi"], 1
6869

6970
The second parameter to the constructor is *n,* the *Markov order* - basically, how
70-
many previous elements the next element depends on. Low values make the Markov chain more random, while high values make it stick closer to the corpus.
71+
many previous elements the next element depends on. Low values make the Markov
72+
chain more random, while high values make it stick closer to the corpus.
7173

7274
If left unspecified, the array of sequences defaults to `[]` and the Markov order defaults to `2`.
7375

@@ -81,17 +83,17 @@ Generation
8183
Make the Markov chain do something useful with `.generate()`. Note that it returns
8284
an array, so if you want a string you'll have to use `.join("")`.
8385

84-
markov = new window.Markov ["sassafras", "mississippi"]
85-
alert markov.generate().join("") # Alerted "rassippi".
86-
alert markov.generate().join("") # Alerted "frassissafrassippi".
86+
markov = new Markov ["sassafras", "mississippi"]
87+
alert markov.generate().join "" # Alerted "rassippi".
88+
alert markov.generate().join "" # Alerted "frassissafrassippi".
8789

8890
`.generate()` takes an optional maximum length parameter, e.g. `markov.generate(10)` to
8991
limit generated words to 10 characters long. If unspecified, it defaults to 20 elements.
9092
There always needs to be a maximum length, because otherwise, things like this
9193
could result in infinite loops:
9294

93-
markov = new window.Markov ["abba"], 1
94-
alert markov.generate().join("") # "bbababbabbbbababababbbabababababbabbbbabbbabababab..."
95+
markov = new Markov ["abba"], 1
96+
alert markov.generate().join "" # "bbababbabbbbababababbbabababababbabbbbabbbabababab..."
9597

9698
Other Stuff
9799
-----------

index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020

2121
<!-- CoffeeScript. -->
2222
<script src = "coffee-script.js"></script>
23-
<script type = "text/coffeescript" src = "Markov.coffee"></script>
24-
<script type = "text/coffeescript" src = "WordGenerator.coffee"></script>
23+
<script type = "text/coffeescript" src = "markov.coffee"></script>
24+
<script type = "text/coffeescript" src = "word-generator.coffee"></script>
2525
</head>
2626

2727
<body>

WordGenerator.coffee renamed to word-generator.coffee

+25-27
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ corpora = [
2727
corpora = ({ name: x, content: corpora[i+1] } for x, i in corpora by 2)
2828

2929

30-
### UI and Initialization: ###
31-
30+
### UI And Initialization: ###
3231
$ ->
3332
$word = $("#word")
3433
$button = $("#button")
@@ -38,35 +37,29 @@ $ ->
3837
$order = $("#order")
3938
$maxLength = $("#maxLength")
4039

41-
markovChain = new window.Markov
42-
maxLength = 0
43-
44-
words = (rawInput) ->
40+
parseWords = (rawInput) ->
4541
rawInput.toLowerCase().replace(/[^a-z\s]/g, "").split(/\s/g)
4642

4743
capitalize = (string) ->
48-
if string.length > 0
49-
string[0].toUpperCase() + string.slice(1)
44+
if string.length > 0 then string[0].toUpperCase() + string.slice(1)
5045
else ""
5146

52-
generateAndShow = ->
53-
$word.text capitalize markovChain.generate(maxLength).join("")
54-
5547
selectCorpus = (index) ->
5648
$corpusName.text(corpora[index].name)
5749
$corpusInput.val(corpora[index].content)
58-
markovChain.sequences = words corpora[index].content
59-
$button.unbind("click").click (generateAndShow)
60-
selectCorpus 0
50+
markovChain.sequences = parseWords corpora[index].content
51+
52+
populatePresetDropdown = ->
53+
for corpus, index in corpora
54+
do (corpus, index) ->
55+
if index is corpora.length - 1
56+
$corpora.append('<li class = "divider">')
57+
newLink = $("<a href = \"#\">#{corpus.name}</a>")
58+
newLink.click((event) -> event.preventDefault(); selectCorpus index)
59+
$("<li>").append(newLink).appendTo($corpora)
6160

62-
# Populate the dropdown list of presets.
63-
for corpus, index in corpora
64-
do (corpus, index) ->
65-
if index is corpora.length - 1
66-
$corpora.append('<li class = "divider">')
67-
newLink = $("<a href = \"#\">#{corpus.name}</a>")
68-
newLink.click((event) -> event.preventDefault(); selectCorpus index)
69-
$("<li>").append(newLink).appendTo($corpora)
61+
generateAndShow = ->
62+
$word.text capitalize markovChain.generate().join("")
7063

7164
# If the user types in the <textarea>, copy the updates into the custom
7265
# corpus and select that as the active corpus.
@@ -76,13 +69,18 @@ $ ->
7669
selectCorpus corpora.length-1;
7770

7871
$order.change ->
79-
markovChain.n = +(@value)
72+
markovChain.n = +@value
8073
@value = markovChain.n
81-
$order.change()
8274

8375
$maxLength.change ->
84-
maxLength = Math.max +(@value), 1
85-
@value = maxLength
86-
$maxLength.change()
76+
markovChain.maxLength = Math.max +@value , 1
77+
@value = markovChain.maxLength
78+
79+
$button.click generateAndShow
8780

81+
markovChain = new Markov
82+
populatePresetDropdown();
83+
selectCorpus 0
84+
$order.change()
85+
$maxLength.change()
8886
generateAndShow()

0 commit comments

Comments
 (0)