Node.js 資料處理 (1)

基本介紹

教學目標

透過 underscore.js 套件進行文章中字元數的計算,主要以 MapReduce 的概念進行實作。

前置作業

  1. 完成 Node.js 套件安裝與設置。

套件安裝

1
$ npm install underscore --save

使用教學

撰寫程式碼

statistic.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
var _ = require('underscore');

var document = "The Heirs depicts the friendships, rivalries and love lives of young, rich heirs led by Kim Tan (Lee Min-Ho) and a girl named Cha Eun-Sang (Park Shin-Hye). Unlike the others, Cha Eun-Sang is considered ordinary and comes from a poor background. 18-year-old Cha Eun-Sang lives with her mother who is mute. Her mother works as a housekeeper and Eun-Sang also works part-time jobs to help out. One day, Eun-Sang's older sister (Yoon Jin-Seo) calls from the U.S. and informs them that she will marry. Eun-Sang's mother then asks Eun-Sang to send her older sister some money from her savings account. Eun-Sang isn't happy with her situation and decides to go to the U.S. herself to see if she can start a better life with her sister. When Eun-Sang arrives in California she is in for a shock. Her older sister lives in a filthy home with a deadbeat boyfriend who seems violent. Her sister is also not getting married or going to school and works full-time as a waitress. When Eun-Sang finally tracks down her sister working at a beach front restaurant, she takes the money Eun-Sang brought for her wedding and leaves without even saying goodbye. Eun-Sang's dreams of starting a better life in America quickly vanishes and she crumbles to the ground in tears. Meanwhile, Kim Tan is not your typical high school student, but a handsome and wealthy Korean heir to a large conglomeration. He is sitting out on the patio of the beach front restaurant, watching everything unfold between Eun-Sang and her sister. Kim Tan's American friend then sees a ziplock bag with a powdery substance fall out of Eun-Sang's suitcase and runs over to scoop it up. Kim Tan knows immediately that the ziplock bag doesn't contain drugs, but rather powder grain to make traditional Korean drinks. Soon, Eun-Sang runs off after Kim Tan's friend and Kim Tan goes to help Eun-Sang. When the dust is settled, Eun-Sang walks out of a police station and her passport is temporarily confiscated. With nowhere to go for Eun-Sang, Kim Tan offers to let her stay at his home. Although they seem to come from different worlds, they quickly develop a rapport for each other. But, when Kim Tan is at school, Eun-Sang meets a girl named Rachel (Kim Ji-Won). Rachel informs Eun-Sang that she is Kim Tan's fiance and she demands that Eun-Sang leaves his home immediately. Eventually, Eun-Sang returns to South Korea. Kim Tan now feels nothing but emptiness after Eun-Sang's departure. He decides to go back to Korea even though his older stepbrother Won (Choi Jin-Hyuk) strongly opposes his return. When Kim Tan gets back to his luxurious mansion like home, he sees someone briefly that resembles Eun-Sang. Kim Tan goes to his mother Ki-Ae (Kim Sung-Ryoung) and asks about the unfamiliar, but familiar girl that he briefly saw. His mother tells him that the girl is their housekeeper's daughter and her name is Cha Eun-Sang. She's also staying in their home.";

var string_array = document.split(/[\s]/);

var map = _.map(string_array, function(element, index) {
var statistic = {};
if (element){
for (var n=0; n<element.length; n++){
var charactor = element.charAt(n);
if (charactor.match(/\w/)){
if (statistic[charactor]){
statistic[charactor] += 1;
} else {
statistic[charactor] = 1;
}
}
}
}
return statistic;
});

var reduce = _.reduce(map, function(count, element, index){
_.each(element, function(value, key, list){
if(count[key]){
count[key] += value;
} else {
count[key] = value;
}
});
return count;
}, {});

var reduces = [];

_.each( reduce, function( val, key ) {
if ( val ) {
reduces.push({charactor:key,statistic:val});
}
});

var result = _.sortBy(reduces, function(output) { return -output.statistic; })
console.log("統計結果: ");
console.log(result);

執行程式

1
$  node statistic.js

輸出結果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
統計結果:
[ { charactor: 'e', statistic: 245 },
{ charactor: 'n', statistic: 188 },
{ charactor: 'a', statistic: 187 },
{ charactor: 't', statistic: 174 },
{ charactor: 'o', statistic: 166 },
{ charactor: 's', statistic: 163 },
{ charactor: 'i', statistic: 158 },
{ charactor: 'r', statistic: 146 },
{ charactor: 'h', statistic: 126 },
{ charactor: 'l', statistic: 79 },
{ charactor: 'u', statistic: 76 },
{ charactor: 'd', statistic: 70 },
{ charactor: 'g', statistic: 67 },
{ charactor: 'm', statistic: 67 },
{ charactor: 'f', statistic: 46 },
{ charactor: 'c', statistic: 42 },
{ charactor: 'y', statistic: 35 },
{ charactor: 'S', statistic: 33 },
{ charactor: 'p', statistic: 30 },
{ charactor: 'w', statistic: 29 },
{ charactor: 'b', statistic: 27 },
{ charactor: 'E', statistic: 26 },
{ charactor: 'k', statistic: 26 },
{ charactor: 'K', statistic: 19 },
{ charactor: 'v', statistic: 17 },
{ charactor: 'T', statistic: 13 },
{ charactor: 'H', statistic: 10 },
{ charactor: 'W', statistic: 7 },
{ charactor: 'C', statistic: 6 },
{ charactor: 'A', statistic: 4 },
{ charactor: 'U', statistic: 3 },
{ charactor: 'J', statistic: 3 },
{ charactor: 'R', statistic: 3 },
{ charactor: 'M', statistic: 2 },
{ charactor: 'q', statistic: 2 },
{ charactor: 'z', statistic: 2 },
{ charactor: '1', statistic: 1 },
{ charactor: '8', statistic: 1 },
{ charactor: 'L', statistic: 1 },
{ charactor: 'P', statistic: 1 },
{ charactor: 'j', statistic: 1 },
{ charactor: 'O', statistic: 1 },
{ charactor: 'Y', statistic: 1 },
{ charactor: 'B', statistic: 1 },
{ charactor: 'x', statistic: 1 } ]

相關資源