Thursday, 22 August 2013

Convert an asynchronous function to a synchronous function

Convert an asynchronous function to a synchronous function

I use a certain Node.js class for text categorization. In its simplest
form, it looks like this:
function TextCategorizer(preprocessors) {
...
}
"preprocessors" is an array of functions of the form:
function(text) {
return "<modified text>"
}
They can be used, for example, to remove punctuations, convert to lower
case, etc.
I can use the TextCategorizer like this:
var cat = newTextCategorizer(preprocessors);
cat.train(text1,class1);
cat.train(text2,class2);
...
console.log(cat.classify(text3,class3);
The preprocessors are called in order for every training text and
classified text.
Now, I need to add a new preprocessor function - a spelling correcter. The
best spelling corrected I found works asynchronously (through a
web-service), so, the function looks like this:
correctSpelling(text, callback) {
...
callback(corrected_version_of_text);
}
i.e. it does not return a value, but rather calls a callback function with
the value.
My question is: how can I use the correctSpelling function, as one of the
preprocessors in the preprocessors array I send to TextCategorizer?

No comments:

Post a Comment