This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package levenshtein | |
import ( | |
"unicode/utf8" | |
) | |
func computeLevenshteinValue(a, b string) int { | |
f := make([]int, utf8.RuneCountInString(b)+1) | |
for j := range f { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func predict(location string, classifier *bayesian.Classifier) { | |
probabilities, _, _ := classifier.ProbScores([]string{readFile(location)}) | |
fmt.Println(probabilities) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func main() { | |
classifier := bayesian.NewClassifier(Business, Tech) | |
enumerateClasses() | |
learn(classifier) | |
predict(testFileLocation, classifier) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"github.com/jbrukh/bayesian" | |
) | |
// exported: the classes that are used to store the learned data. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
OlderNewer