Skip to content

Commit 69fb7b0

Browse files
committed
simple keywrods and description generations; finish #1
1 parent 59eddf0 commit 69fb7b0

File tree

9 files changed

+106
-9
lines changed

9 files changed

+106
-9
lines changed

CONTRIBUTORS.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
37 RobinQu <[email protected]>
1+
39 RobinQu <[email protected]>

Gruntfile.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ module.exports = function(grunt) {
3232
buildDate: (new Date()).toISOString(),
3333
revision: "<%= meta.revision %>"
3434
},
35-
postCompile: build.postCompile
35+
postCompile: build.postCompile,
36+
preCompile: build.preCompile
3637
}
3738
}
3839
},
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# ES6特性
2+
3+
4+
5+
6+
## References
7+
8+
* http://wiki.ecmascript.org/doku.php?id=harmony:specification_drafts
9+
* http://addyosmani.com/blog/a-few-new-things-coming-to-javascript/
10+
* http://addyosmani.com/blog/ecmascript-6-resources-for-the-curious-javascripter/
11+
* http://kangax.github.io/es5-compat-table/es6/
12+
* https://github.com/paulmillr/es6-shim/
13+
* https://github.com/addyosmani/es6-tools
14+
* http://sankhs.com/jschannel-es6/
15+
* http://esdiscuss.org/topic/es6-es7-es8-and-beyond-a-proposed-roadmap

chapters/index.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* 模拟经典继承模型
1414
* 设计模式
1515
* Javascript AOP
16-
* Javscript的函数化编程
16+
* Javascript的函数化编程
1717
* 高阶函数和柯里化
1818
* Javascript Promise
1919
* ECMAScript特性
@@ -104,12 +104,15 @@
104104
* V8、异步IO、事件驱动
105105
* 应用层模块
106106
* V8编程与node扩展
107+
* web中间件
108+
* express和connect
109+
* koa
110+
* 其他
107111
* 部署和维护
108112
* 使用NPM管理依赖
109113
* pm2与监控
110114
* 使用Docker进行部署
111115
* 云上的Nodejs
112-
* 对Nodejs现状的思考和看法
113116
* Javascript的自动化测试
114117
* TDD与BDD
115118
* Mocha、Jasmine、Chai

lib/build.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
var cheerio = require("cheerio"),
22
context = require("./template/context"),
3-
localization = require("./template/localization");
3+
localization = require("./template/localization"),
4+
keyword = require("./keyword");
5+
6+
exports.preCompile = function(src, data) {
7+
//update keywords prior to the markdown
8+
data.keywords = keyword.generate(src);
9+
};
410

511
exports.postCompile = function(html, data) {
612
var $ = cheerio.load(html), title,
@@ -33,5 +39,9 @@ exports.postCompile = function(html, data) {
3339
});
3440
return prev;
3541
}, []);
42+
43+
44+
data.descriptions = $.root().text().substr(0, 200).split(/[\n\t]/).join(" ");
45+
3646
return $.html();
3747
};

lib/keyword.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
var TfIdf = require("natural").TfIdf,
2+
Segment = require("segment").Segment,
3+
debug = require("debug")("keyword"),
4+
_ = require("lodash");
5+
6+
var count = function(doc, word) {
7+
if(!doc) {
8+
return 0;
9+
}
10+
var c = 0, i = 0, hit = true;
11+
while(hit && i<=doc.length) {
12+
if(doc.indexOf(word, i) > -1) {
13+
c++;
14+
i+= word.length;
15+
} else {
16+
break;
17+
}
18+
}
19+
return c;
20+
};
21+
22+
exports.generate = function(document) {
23+
var segment = new Segment(),
24+
tfidf = new TfIdf(),
25+
words, freqs = {},
26+
rank = {};
27+
segment.useDefault();
28+
words = segment.doSegment(document).map(function(t) {
29+
return t.w;
30+
});
31+
debug("segmented words %s", words);
32+
tfidf.addDocument(words);
33+
// words.forEach(function(item) {
34+
// freqs[item] = count(words, item);
35+
// if(!rank[item]) {
36+
// rank[item] = tfidf.tfidf(item, 0);
37+
// }
38+
// debug(tfidf.listTerms(0).map(function(t) {return t.term;}));
39+
// debug("%s, df %s, idf %s", item, freqs[item], rank[item]);
40+
// });
41+
// return Object.keys(rank).map(function(k) {
42+
// return {
43+
// word: k,
44+
// value: rank[k]
45+
// };
46+
// }).sort(function(a, b) {
47+
// return a.value - b.value;
48+
// });
49+
return _.uniq(tfidf.listTerms(0), true);
50+
};

lib/template/template.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
<meta charset="UTF-8">
66
<meta name="viewport" content="width=device-width">
77
<meta http-equiv="X-UA-Compatible" content="IE=edge">
8-
<meta name="description" content="Javascript编程语言 - 一本开源的Javascript编程指南; An open-sourced Javascript Guide and Tutorial">
9-
<meta name="keywords" content="Javascript, guide, open-sourced">
8+
<meta name="description" content="Javascript编程语言 - 一本开源的Javascript编程指南; An open-sourced Javascript Guide and Tutorial; <%- descriptions %>">
9+
<meta name="keywords" content="Javascript, guide, open-sourced <%- keywords.map(function(item) { return ","+item.term; }) %>">
1010
<link rel="stylesheet" type="text/css" href="/style.css" />
1111

1212
</head>

package.json

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "An open-sourced Javascript Guide and Tutorial",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"test": "DEBUG=* ./node_modules/jasmine-node/bin/jasmine-node spec/"
88
},
99
"repository": {
1010
"type": "git",
@@ -30,6 +30,11 @@
3030
"grunt-open": "~0.2.3",
3131
"grunt-contrib-connect": "~0.7.1",
3232
"grunt-git": "~0.2.6",
33-
"grunt-contrib-copy": "~0.5.0"
33+
"grunt-contrib-copy": "~0.5.0",
34+
"segment": "0.0.5",
35+
"natural": "~0.1.27",
36+
"debug": "~0.8.0",
37+
"jasmine-node": "~1.14.3",
38+
"lodash": "~2.4.1"
3439
}
3540
}

spec/keyword_spec.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var keyword = require("../lib/keyword");
2+
3+
describe("keyword generator", function() {
4+
it("should do simple keyword generations", function() {
5+
var txt = "Nokia 之前推出的 Lumia 1520 的确是一部非常不错的手机,只是其 6 吋大小的萤幕虽然好看,却未必每个人都可以接受。想要一部更近似手机的装置的话,美国的使用者早前就多了 Lumia Icon 这个选择,只是此机乃 Verizon 限定,所以即使是在美国,也不是每个人都可以使用得到。然而这个困局今天将要打破,因为 Nokia 刚刚发表了可以说是 Lumia Icon 国际版的 Lumia 930。";
6+
var results = keyword.generate(txt), i, len;
7+
expect(results.length).toBeTruthy();
8+
for(i=0,len=results.length; i<len-1; i++) {
9+
expect(results[i].tfidf).not.toBeLessThan(results[i+1].tfidf);
10+
}
11+
12+
});
13+
});

0 commit comments

Comments
 (0)