-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
227 lines (189 loc) · 5.85 KB
/
script.js
File metadata and controls
227 lines (189 loc) · 5.85 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
Chooply 2011
Main part
*/
$(document).ready(function(){
///////////////////////////CONFIGURATION///////////////////////////
var precision = 70; // smaller more precise
var max_keywords = 30; //maximun of keywords
//////////////////////////////////////////
var score = new Array(); //score of each words
var total = 0; //total of words in the page
/////////////////////////////////////////////////
//fonction to sort array by number
function sortNumber(a,b)
{
return a - b;
}
/////////////////////////////////////////////////
//fonction to count the frequence of each words
function calcul_score(words, importance)
{
for(i=0; i<words.length; i++)
{
var word = words[i];
if(!stopWords[word.toLowerCase()])
{
/////////////////////////////////
//simple word
//if start with majuscule increase score
var mul_maj = 1;
if(/^[A-Z][a-z]{3,16}$/.test(word))
{
mul_maj = 2;
}
word = word.toLowerCase();
var note = 1 * mul_maj * importance;
total++; //increment total of words
//if words is in the array
if(score[word])
{
score[word] += note;
}
else
{
score[word] = note;
}
/////////////////////////////////
//bigram
if(i < words.length - 1)
{
var word_double = words[i+1];
if(!stopWords[word_double.toLowerCase()])
{
word += " " + word_double; //concate 2 words
//if start with majuscule increase score
if(/^[A-Z][a-z]{3,16}$/.test(word_double))
{
mul_maj += 2;
}
word = word.toLowerCase();
var note = 1 * mul_maj * importance;
total++;
//if bigram is in the array
if(score[word])
{
score[word] += note;
}
else
{
score[word] = note;
}
}
}
} //end stop words
}
}
/////////////////////
//modify original page
//add margin top to original page for the bar
$('html').css("margin-top","30px");
//add div on body of the page to create the bar
$('body').prepend('<div id="chooply_bar"><ul id="chooply_keywords"></ul><a id="chooply_more">more</a> <div id="chooply_share"><a name="fb_share" type="icon_link" href="http://www.facebook.com/sharer.php">share</a></div><script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share"></script></div>');
////////////////////////
//get content
//get words of the first div
var words = $('body>div div').text().match(/\b([a-zA-Zéèàê]{4,14})\b/g); //increase importance of depth div
calcul_score(words , 1); //content of div importance of 1
var hn = $('h1, h2').text().match(/\b([a-zA-Zéèàê]{4,14})\b/g); //increase importance of depth div
calcul_score(words , 2); //content of h1 and h2 importance of 2
var hn = $('title').text().match(/\b([a-zA-Zéèàê]{4,14})\b/g); //increase importance of depth div
calcul_score(words , 4); //content of title importance of 4
//////////////////
//sort associative array
var score_sort = new Array(); //to store words
var score_note = new Array(); //to store score
for(key in score)
{
//focus on more important words
if(score[key] > total/precision)
{
//test plurials
if((/s$/.test(key)) && (score[key.replace(/s$/, "")]))
{
key = key.replace(/s$/, "");
}
//if score not present create an array of array
if(score_sort[score[key]])
{
score_sort[score[key]].push(key);
}
else
{
score_note.push(score[key])
score_sort[score[key]] = new Array();
score_sort[score[key]].push(key);
}
}
}
var keywords_used = new Array;
///////////////////////
//show keywords
score_note = score_note.sort(sortNumber).reverse();
var total_printed = 0;
for(note in score_note)
{
//$('#chooply_bar').append(score_note[note] +"<br>"); //show frequence
for(word in score_sort[score_note[note]])
{
//check maximun of words
if(total_printed < max_keywords)
{
//test if it's bigram. words in bigrams could be only use one time
if(/[a-z]*\s[a-z]/.test(score_sort[score_note[note]][word]))
{
//extract each words
var bigram_words = score_sort[score_note[note]][word].split(' ');
//test if words used before in keywords
if((!keywords_used[bigram_words[0]]) && (!keywords_used[bigram_words[1]]))
{
$('#chooply_bar #chooply_keywords').append("<li><span>" + score_sort[score_note[note]][word] + "</span></li>");
//set words used
keywords_used[bigram_words[0]] = 1;
keywords_used[bigram_words[1]] = 1;
total_printed++;
}
}
else //simple words
{
//test if used
if(!keywords_used[score_sort[score_note[note]][word]])
{
$('#chooply_bar #chooply_keywords').append("<li><span>" + score_sort[score_note[note]][word] + "</span></li>");
//set word use
keywords_used[score_sort[score_note[note]][word]] = 1;
total_printed++;
}
}
}
else
{
break;
}
}
} //end show keywords
////////////////////////
//more button
//show button more if necessary
var max_size = $('#chooply_bar #chooply_keywords').height();
if( max_size > 50)
{
$('#chooply_bar #chooply_more').show();
}
//set max line of ul for labels on one line
$('#chooply_bar #chooply_keywords').css("max-height", "20px");
//click on more
$('#chooply_bar #chooply_more').click(function() {
//already expand
if($('#chooply_bar #chooply_keywords').height() > 50)
{
$('#chooply_bar #chooply_keywords').css('height','auto').css('max-height',20).removeClass("chooply_expand"); //retract
$('#chooply_bar #chooply_more').text("more"); //change value of the button
}
else //expand
{
$('#chooply_bar #chooply_keywords').css('height','auto').css('max-height',350).addClass("chooply_expand"); //expand
$('#chooply_bar #chooply_more').text("less"); //change value of the button
}
});
})