Skip to content

Instantly share code, notes, and snippets.

@muety
Created December 7, 2016 07:36
Show Gist options
  • Save muety/3264b34f5328f0525f74e7e075bbf9a3 to your computer and use it in GitHub Desktop.
Save muety/3264b34f5328f0525f74e7e075bbf9a3 to your computer and use it in GitHub Desktop.
'use strict';
const fs = require('fs');
const SEARCH_CLASS = 'http://yago-knowledge.org/resource/wikicat_Towns_in_Baden-Württemberg';
const FILE = './KPClasses-Yago2-22.txt';
let weights = [];
function func(line) {
if (line.indexOf(SEARCH_CLASS) === 0) {
weights.push(parseFloat(line.substr(SEARCH_CLASS.length).replace(',', '.')));
}
}
function end() {
let max = 0;
let index = -1;
for (let i = 0; i < weights.length; i++) {
if (weights[i] > max) {
max = weights[i];
index = i + 1;
}
}
console.log('Total # found:', weights.length);
console.log('Max:', max);
console.log('Cluster:', index);
}
let input = fs.createReadStream(FILE);
readLines(input, func, end);
function readLines(input, func, end) {
let remaining = '';
input.on('data', data => {
remaining += data;
let index = remaining.indexOf('\n');
let last = 0;
while (index > -1) {
let line = remaining.substring(last, index);
last = index + 1;
func(line);
index = remaining.indexOf('\n', last);
}
remaining = remaining.substring(last);
});
input.on('end', end);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment