Permalink
Browse files

change WordFreq API, put all calculation into worker scope

  • Loading branch information...
timdream committed Oct 11, 2011
1 parent 65a580a commit 213af3309657b447ba3b2396c81ddcee8d55322b
Showing with 239 additions and 188 deletions.
  1. +3 −3 tests/wordfreq-getcontent.html
  2. +4 −4 tests/wordfreq.html
  3. +35 −28 wordcloud.js
  4. +46 −77 wordfreq.js
  5. +151 −76 wordfreq.worker.js
@@ -175,7 +175,7 @@ <h2>Source</h2>
'submit',
function () {
try {
- done(wordfreq.empty());
+ wordfreq.empty(done);
} catch (e) { error(e); }
return false;
}
@@ -184,7 +184,7 @@ <h2>Source</h2>
'submit',
function () {
try {
- done(wordfreq.getList());
+ wordfreq.getList(done);
} catch (e) { error(e); }
return false;
}
@@ -193,7 +193,7 @@ <h2>Source</h2>
'submit',
function () {
try {
- done(wordfreq.getSortedList());
+ wordfreq.getSortedList(done);
} catch (e) { error(e); }
return false;
}
View
@@ -151,7 +151,7 @@ <h2>Source</h2>
'submit',
function () {
try {
- done(wordfreq.empty());
+ wordfreq.empty(done);
} catch (e) { error(e); }
return false;
}
@@ -160,7 +160,7 @@ <h2>Source</h2>
'submit',
function () {
try {
- done(wordfreq.getList());
+ wordfreq.getList(done);
} catch (e) { error(e); }
return false;
}
@@ -169,7 +169,7 @@ <h2>Source</h2>
'submit',
function () {
try {
- done(wordfreq.getSortedList());
+ wordfreq.getSortedList(done);
} catch (e) { error(e); }
return false;
}
@@ -178,7 +178,7 @@ <h2>Source</h2>
'submit',
function () {
try {
- done(wordfreq.analyizeVolume());
+ wordfreq.analyizeVolume(done);
} catch (e) { error(e); }
return false;
}
View
@@ -529,36 +529,43 @@ jQuery(function ($) {
wordfreq.processText(
text,
function () {
- changeUIState.ready();
-
- list = wordfreq.getSortedList();
+ wordfreq.getSortedList(
+ function (l) {
+ if (l.length < 5) {
+ changeUIState.error(t('errorWordCount'));
+ return;
+ }
- if (list.length < 5) {
- changeUIState.error(t('errorWordCount'));
- return;
- }
+ list = l;
+ wordfreq.analyizeVolume(
+ function (volume) {
+ changeUIState.ready();
+
+ weightFactor = Math.sqrt($c[0].offsetHeight * $c[0].offsetWidth / volume) * 1;
+ gridSize = 8;
+
+ var wordLength = list.length.toString(10),
+ maxCount = list[0][1].toString(10);
+
+ $c.wordCloud({
+ wordColor: theme[themeid].wordColor,
+ backgroundColor: theme[themeid].backgroundColor,
+ fontFamily: theme[themeid].fontFamily,
+ rotateRatio: 0,
+ wordList: [
+ [t('readyList_1', wordLength, maxCount), t('readyList_1C')],
+ [t('readyList_2', wordLength, maxCount), t('readyList_2C')],
+ [t('readyList_3', wordLength, maxCount), t('readyList_3C')],
+ [t('readyList_4', wordLength, maxCount), t('readyList_4C')]
+ ]//,
+ //abortThreshold: 200,
+ //abort: changeUIState.too_slow
+ });
+ }
+ );
+ }
+ );
- weightFactor = Math.sqrt($c[0].offsetHeight * $c[0].offsetWidth / wordfreq.analyizeVolume()) * 1;
- gridSize = 8;
-
- var wordLength = list.length.toString(10),
- maxCount = list[0][1].toString(10);
-
-
- $c.wordCloud({
- wordColor: theme[themeid].wordColor,
- backgroundColor: theme[themeid].backgroundColor,
- fontFamily: theme[themeid].fontFamily,
- rotateRatio: 0,
- wordList: [
- [t('readyList_1', wordLength, maxCount), t('readyList_1C')],
- [t('readyList_2', wordLength, maxCount), t('readyList_2C')],
- [t('readyList_3', wordLength, maxCount), t('readyList_3C')],
- [t('readyList_4', wordLength, maxCount), t('readyList_4C')]
- ]//,
- //abortThreshold: 200,
- //abort: changeUIState.too_slow
- });
}
);
};
View
@@ -9,11 +9,11 @@
usage:
var wordfreq = WordFreq(options); // init
wordfreq.processText(text, callback); // run text with WordFreq, save the result, then execute callback() when complete
- wordfreq.terminate // stop the Web Worker
- wordfreq.empty // empty results saved
- wordfreq.getList // get a list of words from results
- wordfreq.getSortedList // get a list of words, sorted by the number of appearance.
- wordfreq.analyizeVolume // (experimental) return a number that indicate the volume of words
+ wordfreq.terminate(); // stop the Web Worker
+ wordfreq.empty(callback); // empty results saved
+ wordfreq.getList(callback); // get a list of words from results
+ wordfreq.getSortedList(callback); // get a list of words, sorted by the number of appearance.
+ wordfreq.analyizeVolume(callback); // (experimental) return a number that indicate the volume of words
options:
worker: // path to worker.js relative to the document (not JS), subject to same origin policy.
@@ -51,89 +51,58 @@ var WordFreq = function (settings) {
six_gram: true,
mincount: 3
},
- words = {},
- reps = {},
- processText = function (text, callback) {
- worker.onmessage = function (ev) {
- for (var word in ev.data.words) if (ev.data.words.hasOwnProperty(word)) {
- if (typeof words[word] !== 'number') words[word] = ev.data.words[word];
- else words[word] += ev.data.words[word];
- if (ev.data.reps[word]) {
- if (!reps[word]) reps[word] = ev.data.reps[word];
- // else TBD
+ callbacks = [],
+ runTask = function (task) {
+ return function (callback) {
+ var callbackId = callbacks.length;
+ callbacks[callbackId] = callback;
+ worker.postMessage(
+ {
+ task: task,
+ callbackId: callbackId
}
- };
- callback.apply(this, [ev.data.words]);
+ );
};
- worker.postMessage(
- {
- settings: settings,
- text: text
- }
- );
- },
- terminate = function () {
- worker.terminate();
- },
- empty = function () {
- words = {};
- reps = {};
- },
- getList = function () {
- var list = [];
- for (var word in words) if (words.hasOwnProperty(word)) {
- if (words[word] < settings.mincount) continue;
- var maxRep;
- if (typeof reps[word] === 'object') {
- var c = 0;
- // https://gist.github.com/878204 by monoceroi, thanks!
- for (var rep in reps[word]) if (Object.hasOwnProperty.call(reps[word], rep)) {
- if (typeof reps[word][rep] === 'number' && reps[word][rep] > c) {
- maxRep = rep;
- c = reps[word][rep];
- }
- }
- }
- list.push([maxRep || word, words[word]]);
- maxRep = false;
- }
- return list;
- },
- getSortedList = function () {
- return getList().sort(
- function (a, b) {
- if (a[1] > b[1]) return -1;
- if (a[1] < b[1]) return 1;
- var t = [a[0], b[0]];
- t = t.sort();
- if (t[0] !== a[0]) return 1;
- return 0;
- }
- );
- },
- analyizeVolume = function () {
- var v = 0;
- for (var word in words) if (words.hasOwnProperty(word)) {
- if (words[word] < settings.mincount) continue;
- v += word.length*words[word]*words[word];
- }
- return v;
+ };
+
+ worker = new Worker(settings.worker);
+
+ worker.onmessage = function (ev) {
+ if (typeof callbacks[ev.data.callbackId] === 'function') callbacks[ev.data.callbackId].call(this, ev.data.returnData);
+ delete callbacks[ev.data.callbackId];
};
if (!settings) settings = {};
for (var opt in options) if (options.hasOwnProperty(opt)) {
if (typeof settings[opt] === 'undefined') settings[opt] = options[opt];
}
- worker = new Worker(settings.worker);
+ worker.postMessage(
+ {
+ task: 'init',
+ settings: settings
+ }
+ );
return {
- processText: processText,
- terminate: terminate,
- empty: empty,
- getList: getList,
- getSortedList: getSortedList,
- analyizeVolume: analyizeVolume
+ processText: function (text, callback) {
+ var callbackId = callbacks.length;
+ callbacks[callbackId] = callback;
+ worker.postMessage(
+ {
+ task: 'processText',
+ callbackId: callbackId,
+ text: text
+ }
+ );
+ },
+ terminate: function () {
+ worker.terminate();
+ },
+ empty: runTask('empty'),
+ getList: runTask('getList'),
+ getSortedList: runTask('getSortedList'),
+ analyizeVolume: runTask('analyizeVolume')
};
};
Oops, something went wrong.

0 comments on commit 213af33

Please sign in to comment.