Skip to content

Commit ef9ef17

Browse files
committed
comparisonvector-factory works
1 parent e4a5674 commit ef9ef17

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+

css-simplify.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
var css = require('css'),
2+
fs = require('fs'),
3+
_ = require('underscore');
4+
5+
var cumulativeComparisonVectorFactory = function() {
6+
7+
var dict = {}
8+
dictlength = 0;
9+
10+
11+
return function (rule) {
12+
var vector = Array.apply(null, Array(dictlength)).map(Number.prototype.valueOf,0);
13+
14+
console.log(dict.length);
15+
_.each(rule.selectors, function(element){
16+
17+
18+
if (dict[element]) {
19+
vector[dict[element]] = 1;
20+
}else {
21+
dict[element] = dictlength++;
22+
vector.push(1);
23+
}
24+
});
25+
26+
// look up rules in dictionary, add the ones that doesn't exist.
27+
// ... while simultaneously building an array (the vector)
28+
// return that vector:
29+
console.log(vector);
30+
31+
return vector;
32+
}
33+
};
34+
35+
var vecfac = cumulativeComparisonVectorFactory();
36+
37+
function parseit(err, res) {
38+
if (res != null) {
39+
_.each(_.filter(css.parse(res).stylesheet.rules, function(x) { return x.type === 'rule';}), vecfac);
40+
} else {
41+
console.log(err);
42+
}
43+
}
44+
45+
fs.readFile('lol.css', 'utf8', parseit);
46+
47+
48+
var safeCosineSimilarity = function(a, b) {
49+
var result = 0, denom = 0;
50+
for (var i = 0; i < a.length && i < b.length; ++i ) {
51+
result += a[i] * b[i];
52+
}
53+
var reducer = function(memo, value) { return memo + value*value; };
54+
return result / (1+ sqrt( _.reduce(a, reducer, 0) * _.reduce(b, reducer, 0) ));
55+
};
56+
57+
var findSimilarRules = function () {
58+
// how can we save time and computations when doing the comparisons? can we?
59+
// what are we really looking for, how do we find the most similar things?
60+
61+
};

package.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "css-simplify",
3+
"version": "0.0.1",
4+
"description": "A utility script that tries to simplify a css by using cosine similarity",
5+
"main": "css-simplify.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "http://github.com/velfundert/css-simplify.git"
12+
},
13+
"keywords": [
14+
"css"
15+
],
16+
"author": "Christian Strandenæs",
17+
"license": "GPL-3.0",
18+
"devDependencies": {
19+
"css": "^2.2.1"
20+
}
21+
}

0 commit comments

Comments
 (0)