Skip to content

Instantly share code, notes, and snippets.

View kmaher9's full-sized avatar
🥑
Focusing

Kieran Maher kmaher9

🥑
Focusing
View GitHub Profile
@kmaher9
kmaher9 / levenshtein.go
Created August 24, 2018 18:21
this is the only file needed - it is a package implementation of the Levenshtein Distance algorithm, as used in string comparison.
package levenshtein
import (
"unicode/utf8"
)
func computeLevenshteinValue(a, b string) int {
f := make([]int, utf8.RuneCountInString(b)+1)
for j := range f {
func enumerateDirectory(location string) []string {
var files []string
listing, err := ioutil.ReadDir(location)
if err != nil {
panic(err)
}
for _, l := range listing {
files = append(files, location+"/"+l.Name())
}
return files
func learn(classifier *bayesian.Classifier) {
classifier.Learn(businessFiles, Business)
classifier.Learn(techFiles, Tech)
}
func enumerateClasses() {
businessDirectory := enumerateDirectory(businessDirectoryLocation) // retrieves a list of all filenames stored in the directory.
for _, file := range businessDirectory {
fileContent := readFile(file) // returns the entire contents of the files as a string.
businessFiles = append(businessFiles, fileContent)
func predict(location string, classifier *bayesian.Classifier) {
probabilities, _, _ := classifier.ProbScores([]string{readFile(location)})
fmt.Println(probabilities)
}
func main() {
classifier := bayesian.NewClassifier(Business, Tech)
enumerateClasses()
learn(classifier)
predict(testFileLocation, classifier)
}
package main
import (
"fmt"
"io/ioutil"
"github.com/jbrukh/bayesian"
)
// exported: the classes that are used to store the learned data.
var fs = require('fs') // to get directory listings, and read files - npm install fs
var brain = require('brain.js') // contains core functions - npm install brain.js
var businessPath = "path/to/business/dataset" // location of 100 training files
var techPath = "path/to/tech/dataset" // second class 100 training files
var testFile = "path/to/test/file.txt" // file which will be used to test accuracy - belongs to tech
var inputs = [] // will be used to store all training data prior to training
var errorThreshold = 0.085 // the minimum error margin needed to class the nn as trained
var businessFiles = fs.readdirSync(businessPath) // retrieve directory listing
var techFiles = fs.readdirSync(techPath)
for (var i = 0; i < businessFiles.length; i++) {
var file = businessFiles[i]
var content = fs.readFileSync(businessFiles + "\\" + file) // opens the file as a stream, reading the whole file into memory
inputs.push({input: content.toString(), output: 'business' }) // the entire file content is stored in a two dimensional array
}
net.train(inputs, {
log: true, // prints to the screen the error rate, and current iteration
errorThresh: 0.05, // a relatively high error margin - but I'm not shooting for 100% accuracy (and it's merely a test)
})
var testFileContent = fs.readFileSync(testFile) // read the entire sample file into memory
var output = net.run(testFileContent) // and then run it! This will predict the class for the testFile
console.log(output)
// output:
var fs = require('fs')
var brain = require('brain.js')
var bPath = "Z:\\Development\\Javascript\\NodeJS\\Neural Networks\\Brain.JS\\002\\src\\dataset\\business"
var tPath = "Z:\\Development\\Javascript\\NodeJS\\Neural Networks\\Brain.JS\\002\\src\\dataset\\tech"
var sample = "Z:\\Development\\Javascript\\NodeJS\\Neural Networks\\Brain.JS\\002\\src\\dataset\\011.txt"
var net = new brain.recurrent.LSTM()
var bFiles = fs.readdirSync(bPath)