Files
kiss-vscode/projects/_deprecated/flixel-desktop-handloose/source/DocumentModel.kiss

58 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)
~charFrequencies)
(prop &mut :String content "")
(method :Void type [:String str]
(+= content str)
(File.saveContent path content))
// TODO make this a map of String,DateTime and re-learn from files that were modified more recently
(savedVar :Map<String,Bool> filesLearnedFrom (new Map))
(savedVar :Map<String,Map<String,Float>> charFrequencies (new Map))
(savedVar :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)))))))
(method learnAllFrequencies []
// Use files with the same extension in the current working directory to determine letter frequencies
(withMutProperties [filesLearnedFrom]
(walkDirectory "" (FileSystem.absolutePath "")
->file (when (= (Path.extension file) (Path.extension path))
(unless (filesLearnedFrom.exists path)
(print "learning from $file")
(learnFrequencies (File.getContent file))
(dictSet filesLearnedFrom path true))))))
(method learnFrequencies [:String str]
(let [chars (str.split "")]
(when chars
(withMutProperties [charFrequencies wordFrequencies]
(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 or the word in progress is a substring of a common word
(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)))))