Skip to content

Instantly share code, notes, and snippets.

@muety
Created December 7, 2016 22:27
Show Gist options
  • Save muety/c2be4cc7ced97ec9c697d0c263a11ebd to your computer and use it in GitHub Desktop.
Save muety/c2be4cc7ced97ec9c697d0c263a11ebd to your computer and use it in GitHub Desktop.
'use strict';
const fs = require('fs');
const FILE1 = './dbr_Karlsruhe_properties.txt';
const FILE2 = './KPProperties-DBpedia39-22.txt';
const CLUSTER = 1;
let properties = {};
let result = [];
let input1 = fs.createReadStream(FILE1);
let input2 = fs.createReadStream(FILE2);
readLines(input1, line => {
properties[line.replace(/"/g, '')] = true;
}, () => {
let currentCluster = 0;
readLines(input2, line => {
if (line.indexOf('Cluster') === 0) currentCluster = parseInt(line.replace(/:/, '').replace(/ /, '').replace(/Cluster/, ''));
let prop = line.substr(1, line.indexOf('>') - 1);
if (properties[prop] && currentCluster == CLUSTER) result.push(line);
}, () => {
console.log(`${result.length} properties in common.`);
fs.writeFileSync('result.txt', result);
});
});
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