Skip to content

Instantly share code, notes, and snippets.

@horitaku1124
Last active May 1, 2019 16:09
Show Gist options
  • Save horitaku1124/5e6f09790e3c5904f1af3548afe89081 to your computer and use it in GitHub Desktop.
Save horitaku1124/5e6f09790e3c5904f1af3548afe89081 to your computer and use it in GitHub Desktop.
const string2 = `
0 The Class-Path
0 :: Spring Boot ::
0 name: default
1 1:34:23.868
1 1:34:23.868
1 1:34:23.870
1 1:34:23.871
1 1:34:23.917
1 1:34:23.917
1 1:34:24.567
1 1:34:24.635
1 1:34:24.976
1 1:34:25.353
1 1:34:25.371
1 1:34:25.382
1 1:34:25.382
1 1:34:25.395
1 1:34:25.489
1 1:34:25.490
1 1:34:25.690
`;
const string = `
0 The Class-Path
1 21:34:25.690
`;
const CharLimit = 10;
let lines = string2.split("\n");
// console.log(lines);
function strToVec(str, charLimit) {
let vec = [];
for (let i = 0;i < charLimit;i++) {
if (str.length <= i) {
vec.push(0);
} else {
vec.push(str.charCodeAt(i));
}
}
return vec;
}
let lineNum = 0;
let allVecs = [];
let belongs = [];
for (lineSet of lines) {
if (lineSet == "") continue;
let annotation = lineSet.indexOf(" ");
let belong = parseInt(lineSet.substring(0));
let line = lineSet.substring(annotation + 3);
// console.log(lineNum, belong, line);
if (belongs[belong] == null) {
belongs[belong] = [];
}
belongs[belong].push(lineNum);
let vec = strToVec(line, CharLimit);
allVecs.push(vec);
lineNum++;
}
// console.log(allVecs);
// console.log(belongs);
const Clustors = 2;
const VecNum = CharLimit;
let cetroids = [];
for (let i = 0;i < Clustors;i++) {
let cetroid = [];
for (let j = 0;j < VecNum;j++) {
cetroid[j] = 0;
}
let casified = belongs[i];
for (let j of casified) {
let vec = allVecs[j];
for (let j = 0;j < VecNum;j++) {
cetroid[j] += vec[j];
}
}
if (casified.length > 0) {
for (let j = 0;j < VecNum;j++) {
cetroid[j] = cetroid[j] / casified.length;
}
cetroids[i] = cetroid;
}
}
console.log(cetroids);
let tests = ["21:34:25.690", "restartedMain"];
for (test of tests) {
let leastDistance = Number.MAX_VALUE;
let neighber = null;
let record = strToVec(test, CharLimit);
for (let j = 0;j < cetroids.length;j++) {
let weight = cetroids[j];
let distance = 0;
for (let i = 0;i < VecNum;i++) {
let diff = weight[i] - record[i];
distance += diff * diff;
}
distance = Math.sqrt(distance);
if (distance < leastDistance) {
leastDistance = distance;
neighber = j;
}
}
console.log(test, neighber);
}
let fs = require('fs');
let utils = require('./utils.js');
let readFilePath = process.argv[2];
let teacherData = fs.readFileSync(readFilePath).toString();
let trainResult = JSON.parse(teacherData);
const Clustors = trainResult.Clustors;
const VecNum = trainResult.VecNum;
console.log("Clustors", Clustors);
console.log("VecNum", VecNum);
let cetroids = trainResult.cetroids;
console.log(cetroids);
let tests = ["21:34:25.690", "restartedMain"];
for (test of tests) {
let leastDistance = Number.MAX_VALUE;
let neighber = null;
let record = utils.strToVec(test, VecNum);
for (let j = 0;j < cetroids.length;j++) {
let weight = cetroids[j];
let distance = 0;
for (let i = 0;i < VecNum;i++) {
let diff = weight[i] - record[i];
distance += diff * diff;
}
distance = Math.sqrt(distance);
if (distance < leastDistance) {
leastDistance = distance;
neighber = j;
}
}
console.log(test, neighber);
}
let data = [
[1,2],
[2,3],
[11,12],
[12,13]
];
const Clustors = 2;
const VecNum = 2;
let belongs = [];
for (let i = 0;i < Clustors;i++) {
belongs.push([]);
}
for(let i = 0;i < data.length;i++) {
let initClustor = parseInt(Math.random() * Clustors);
belongs[initClustor].push(i);
}
console.log(belongs);
let lastCentrois;
let lastBelongs;
for (let l = 0;l < 3;l++) {
let cetroids = [];
for (let i = 0;i < Clustors;i++) {
let cetroid = [];
for (let j = 0;j < VecNum;j++) {
cetroid[j] = 0;
}
let casified = belongs[i];
for (let j of casified) {
let vec = data[j];
for (let j = 0;j < VecNum;j++) {
cetroid[j] += vec[j];
}
}
if (casified.length > 0) {
for (let j = 0;j < VecNum;j++) {
cetroid[j] = cetroid[j] / casified.length;
}
cetroids[i] = cetroid;
}
}
// console.log(cetroids);
belongs = [];
for (let i = 0;i < Clustors;i++) {
belongs.push([]);
}
for (let k = 0;k < data.length;k++) {
let record = data[k];
let leastDistance = Number.MAX_VALUE;
let neighber = null;
for (let j = 0;j < cetroids.length;j++) {
let weight = cetroids[j];
let distance = 0;
for (let i = 0;i < VecNum;i++) {
let diff = weight[i] - record[i];
distance += diff * diff;
}
distance = Math.sqrt(distance);
if (distance < leastDistance) {
leastDistance = distance;
neighber = j;
}
}
belongs[neighber].push(k);
}
// console.log(cetroids);
console.log(belongs);
if (belongs === lastBelongs) {
break;
}
lastBelongs = belongs;
lastCentrois = cetroids;
}
console.log(lastCentrois);
console.log(lastBelongs);
let utils = require('./utils.js');
let dictionary = [
"word", "car", "verb"
];
const CharLimit = 5;
let cetroids = dictionary.map(word => utils.strToVec2(word, CharLimit));
console.log(cetroids);
let tests = [
"ward",
"cat",
"verd",
];
for (test of tests) {
let leastDistance = Number.MAX_VALUE;
let neighber = null;
let record = utils.strToVec2(test, CharLimit);
console.log(test, record);
for (let j = 0;j < cetroids.length;j++) {
let weight = cetroids[j];
let distance = 0;
for (let i = 0;i < CharLimit;i++) {
let diff = weight[i] - record[i];
// let diff = (weight[i] - record[i]) ? 1 : 0;
distance += diff * diff;
}
distance = Math.sqrt(distance);
console.log(j, distance);
if (distance < leastDistance) {
leastDistance = distance;
neighber = j;
}
}
console.log(test, neighber);
}
let fs = require('fs');
let utils = require('./utils.js');
let readFilePath = process.argv[2];
let train_data = fs.readFileSync(readFilePath).toString();
const CharLimit = 10;
let lines = train_data.split("\n");
// console.log(lines);
let lineNum = 0;
let allVecs = [];
let belongs = [];
for (lineSet of lines) {
if (lineSet == "") continue;
let annotation = lineSet.indexOf(" ");
let belong = parseInt(lineSet.substring(0));
let line = lineSet.substring(annotation + 3);
// console.log(lineNum, belong, line);
if (belongs[belong] == null) {
belongs[belong] = [];
}
belongs[belong].push(lineNum);
let vec = utils.strToVec(line, CharLimit);
allVecs.push(vec);
lineNum++;
}
// console.log(allVecs);
// console.log(belongs);
const Clustors = Object.keys(belongs).length;
const VecNum = CharLimit;
let cetroids = [];
for (let i = 0;i < Clustors;i++) {
let cetroid = [];
for (let j = 0;j < VecNum;j++) {
cetroid[j] = 0;
}
let clasified = belongs[i];
for (let j of clasified) {
let vec = allVecs[j];
for (let j = 0;j < VecNum;j++) {
cetroid[j] += vec[j];
}
}
if (clasified.length > 0) {
for (let j = 0;j < VecNum;j++) {
cetroid[j] = cetroid[j] / clasified.length;
}
cetroids[i] = cetroid;
}
}
// console.log("Clustors", Clustors);
// console.log("VecNum", VecNum);
let trainResult = {
"Clustors": Clustors,
"VecNum": VecNum,
"cetroids": cetroids,
}
console.log(JSON.stringify(trainResult));
let utils = {};
utils.strToVec = function(str, charLimit) {
let vec = [];
for (let i = 0;i < charLimit;i++) {
if (str.length <= i) {
vec.push(0);
} else {
vec.push(str.charCodeAt(i));
}
}
return vec;
};
const ABC = "eaoui tsnrhldcmpfgywbvkjxqz";
utils.strToVec2 = function(str, charLimit) {
let vec = [];
for (let i = 0;i < charLimit;i++) {
if (str.length <= i) {
vec.push(0);
} else {
let char = str.charAt(i);
let index = ABC.indexOf(char);
vec.push(index);
}
}
return vec;
};
module.exports = utils;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment