firefly avatar firefly committed b30b41a

Remove old markov code.

Comments (0)

Files changed (1)

modules/05-cmd-markov.coffee

-fs = require 'fs'
-
-filenameMap =
-#	"bible"        : '/home/firefly/tmp/b/short-bible'
-	"zelda-la"     : '/home/firefly/annat/texts/zelda-la.txt'
-#	"pkmn-red"     : '/home/firefly/tmp/b/dred.txt'
-#	"kinginyellow" : '/home/firefly/tmp/b/kinginyellow'
-	"portal2"      : '/home/firefly/annat/texts/portal2-subtitles'
-#	"lagtext"      : '/home/firefly/tmp/b/lagtext'
-	"raytracer"    : '/home/firefly/annat/texts/raytracer'
-#	"wp-sweden"    : '/home/firefly/tmp/b/wp-sweden'
-	"communist"    : '/home/firefly/annat/texts/communist-manifest.txt'
-	"art-of-war"   : '/home/firefly/annat/texts/art-of-war.txt'
-	"datalogi"     : '/home/firefly/annat/texts/datalogi-a.txt'
-#	"tullingelogs" : '/tmp/logdump'
-
-cachedChains = {}
-
-markovChains = (str) ->
-	words = (str.match /[.,]|[\wåäö][\wéåäö\d']*/g).map (word) ->
-		     do word.toLowerCase
-	
-	chainMap = {}
-	
-	for word,idx in words.slice 2
-		pprev = words[idx]
-		prev  = words[idx + 1]
-		
-		chainMap[pprev]       = {} if not chainMap[pprev]?
-		chainMap[pprev][prev] = {} if not chainMap[pprev][prev]?
-		
-		map = chainMap[pprev][prev]
-		
-		if map[word]?
-			map[word]++
-		else
-			map[word] = 1
-	
-	chainMap
-	
-
-readFile = (filename) ->
-	do (fs.readFileSync filename).toString
-
-getChainMap = (name) ->
-	cachedChains[name] ?= markovChains readFile filenameMap[name]
-
-generateSentence = (chainMap, length) ->
-	allWords = Object.keys chainMap
-	
-	findNextWord = (word1, word2) ->
-		wordMap     = chainMap[word1][word2]
-		nextWords   = Object.keys wordMap
-		nextChances = nextWords.map (k) -> wordMap[k]
-		sumChances  = nextChances.reduce (x,y) -> x+y
-		
-		idx = Math.floor (Math.random() * sumChances)
-		
-		for word in nextWords
-			idx -= Math.floor wordMap[word]
-			return word if idx <= 0
-	
-	randomElement = (arr) ->
-		arr[Math.floor (Math.random() * arr.length)]
-	
-	firstUpper = (str) ->
-		str[0].toUpperCase() + str.substr 1
-	
-	#curr1 = allWords[Math.floor (Math.random() * allWords.length)]
-	curr1 = "."
-	curr2 = randomElement Object.keys chainMap[curr1]
-	
-	outArr = [curr1, firstUpper curr2]
-	
-	for i in [1..length]
-		next = findNextWord curr1, curr2
-		outArr.push " " if not (next in ".,")
-		
-		if curr2 == "."
-			outArr.push firstUpper next
-		else
-			outArr.push next
-		
-		curr1 = curr2
-		curr2 = next
-	
-	outArr.slice(1).join ""
-
-fetchFile = (url, callback) ->
-	chunks = []
-	totalLength = 0
-	
-	backend.net.get url, (err, data, done) ->
-		if done
-			callback data
-			return
-		
-		chunks.push data
-		totalLength += data.length
-		
-		if totalLength > 10000000
-			callback chunks.join ""
-		
-		totalLength < 10000000
-
-bot.triggers.command.add 'markov',
-	help: "Generates random output via markov chains from a given set of words."
-	exec: (data, setName) ->
-		getOutputText = (chainMap) ->
-			((generateSentence chainMap, 60).replace /[,.][^,.]*$/, '') + "."
-		
-		if data.line.match /^http:|^www\./
-			url = (data.line.match /(?:http:|www\.)[^ ]*/)[0]
-			fetchFile url, (data) =>
-				wordcount = (data.split " ").length
-				
-				if wordcount < 10
-					@reply "Error: Way too little data!"
-				else
-					@reply getOutputText markovChains "." + data
-		
-		else
-			if not setName?
-				@reply "Available wordsets: #{(Object.keys filenameMap).join ', '}"
-				return
-			
-			if not filenameMap[setName]?
-				@reply "No such wordset available. :( (see 'markov' for available wordsets.)"
-				return
-			
-			@reply getOutputText getChainMap setName
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.