64 lines
2.6 KiB
Plaintext
64 lines
2.6 KiB
Plaintext
(defNew [&prop :String path]
|
|
(when (FileSystem.exists path) (set content (File.getContent path)))
|
|
(type "") // If the file can't be written to, throw the error right away
|
|
(learnAllFrequencies))
|
|
|
|
(prop &mut :String content "")
|
|
(method :Void type [:String str]
|
|
(+= content str)
|
|
(File.saveContent path content)
|
|
(print content))
|
|
|
|
(prop :Map<String,Map<String,Float>> charFrequencies (new Map))
|
|
(prop :Map<String,Map<String,Float>> wordFrequencies (new Map))
|
|
|
|
(method incFrequency [:Map<String,Map<String,Float>> m :String c :String following]
|
|
(let [&mut weight 1.0]
|
|
(when c
|
|
(when (= c "\r") (set c "\n"))
|
|
(when (= c following " ") (set c "\t") (set weight 0.25))
|
|
(unless (m.exists following) (dictSet m following (new Map)))
|
|
(let [followingMap (dictGet m following)]
|
|
(dictSet followingMap c (+ weight (or (dictGet followingMap c) 0)))))))
|
|
|
|
(defMacro prelearnAllFrequencies []
|
|
/* TODO - add Context.definedValues to macro variables
|
|
- walk LEARN_FROM, adding to a frequency map in prelearnFrequencies() below
|
|
- stringify the frequency map with "" specially added around the keys
|
|
- inject a Context.parse of the stringified frequency map
|
|
*/
|
|
(throw "not implemented"))
|
|
|
|
(defMacroFunction prelearnFrequencies [str]
|
|
(throw "not implemented"))
|
|
|
|
(method learnAllFrequencies []
|
|
(#if LEARN_FROM
|
|
(prelearnAllFrequencies)
|
|
// Use files with the same extension in the current working directory to determine letter frequencies
|
|
(walkDirectory "" (FileSystem.absolutePath "")
|
|
->file (when (= (Path.extension file) (Path.extension path))
|
|
(learnFrequencies (File.getContent file))))))
|
|
|
|
(method learnFrequencies [:String str]
|
|
(let [chars (str.split "")]
|
|
(when chars
|
|
(incFrequency charFrequencies (first chars) "")
|
|
(doFor [following c] (pairs chars)
|
|
(incFrequency charFrequencies c following)))
|
|
// TODO learn word frequencies
|
|
))
|
|
|
|
(prop :FlxRandom r (new FlxRandom))
|
|
(var ANY_CHANCE 25) // percent
|
|
(method :ArrowStuff generateArrowStuff []
|
|
// TODO also generate word arrows if lastChar is a space
|
|
(let [lastChar (substr content -1)
|
|
charFreq (dictGet charFrequencies lastChar)
|
|
chars []
|
|
:Array<Float> weights []]
|
|
(doFor =>c weight charFreq
|
|
(chars.push c)
|
|
(weights.push weight))
|
|
(let [c (r.getObject chars (if (r.bool ANY_CHANCE) null weights))]
|
|
(object text c action ->(type c))))) |