我使用wait.for库来摆脱一些异步调用。我这样做是因为我并不太关心我的服务器是否有效运行,我使用node.js使用Natural NLP npm模块进行一些数据处理。
无论如何,我递归地构建一棵树并为每个节点请求维基百科文章。该维基百科请求是异步的,我试图通过调用wait.for(...)使其同步。如果递归调用在一次激活后结束,则请求正常工作,但如果递归继续进行,则抛出错误。
视觉上,我的意思是
[ROOT]
/ | \
[] [] [] <--- This level is fine ... No Errors
|
[] <--- Error Here, on level 2
错误是:
/Users/me/Documents/Node Projects/Personal Website/node_modules/wait.for/waitfor.js:37
fiber.run(); //resume fiber after "yield"
^
TypeError: Cannot call method 'toString' of undefined
at TfIdf.tfidf (/Users/me/Documents/Node Projects/Personal Website/node_modules/natural/lib/natural/tfidf/tfidf.js:132:42)
at isIrrelevant (/Users/me/Documents/Node Projects/Personal Website/finalproject/wikitree.js:126:47)
以下是递归调用的代码:
var WikiTree = function(node) {
this.name = node.name;
this.context = node.context;
this.text = node.article;
if (node.children) {
this.children = node.children;
}
function createArticleSummary(article) {
// TODO: Must Implement
return article;
}
function isIrrelevant(article, title, linkText) {
// Tweak Here
var articleTfIdf = new TfIdf();
articleTfIdf.addDocument(article);
var titleRelevanceToArticle = articleTfIdf.tfidf(title,0); // ERROR HERE: This is wikitree.js:126
var textRelevanceToArticle = articleTfIdf.tfidf(linkText,0);
var titleRelevanceToZen = zenTfidf.tfidf(title,0);
var textRelevanceToZen = zenTfidf.tfidf(linkText,0);
return ( titleRelevanceToZen / titleRelevanceToArticle >= 1 ) || ( textRelevanceToZen / textRelevanceToArticle >= 1 );
}
WikiTree.prototype.searchChild = function(child) {
console.log("Searching for Link Title \"" + child.title + "\" And Link Text \"" + child.linkText + "\"");
var childQuery = {
query : child.title,
format : "html",
summaryOnly : false
};
var childArticle = wait.for(wikipedia.searchArticle, childQuery);
console.log("Got Wikipedia Response for " + child.title);
var childWikiLinks = extractLinkedWikis(childArticle);
console.log("Found " + childWikiLinks.length + " Links for " + child.title);
for (var i = childWikiLinks.length - 1; i >= 0; i--) {
if ( LinkTrie.contains(childWikiLinks[i].title) || LinkTrie.contains(childWikiLinks[i].linkText) ) {
childWikiLinks.splice(i,1);
} else if ( isIrrelevant( childWikiLinks[i] ) ) {
childWikiLinks.splice(i,1);
} else {
LinkTrie.addStrings([ childWikiLinks[i].title, childWikiLinks[i].text ]);
}
};
console.log("After pruning, " + childWikiLinks.length + " Links left for " + child.title);
var newChildWT = new WikiTree({
name : child.title,
context : child.linkText,
article : childArticle,
children : createArticleSummary(childWikiLinks)
});
return newChildWT;
};
WikiTree.prototype.descend = function() {
// Descend to the next level ... Meaning find the children of the current children (If there are any)
if (!this.children) {
console.log("Leaf at " + this.name);
return;
}
for (var i = this.children.length - 1; i >= 0; i--) {
if ( this.children[i].title.match(/Category:/) ) {
console.log("Found 'Category:' at " + i + " in " + this.children[i].title);
this.children.splice(i,1);
} else
this.children[i] = this.searchChild(this.children[i]);
};
console.log("Done With Descend For " + this.name);
};
this.descend();
};
var result = ...; // A String
var zenLinks = ...; // An Array of objects
// Actually make the first recursive call here
var zenTree = new WikiTree({
name : 'Zen & The Art of Motorcycle Maintenance',
article : result,
children : zenLinks
});
无论如何,任何帮助将不胜感激。谷歌搜索没有太多帮助,所以任何有wait.for或节点光纤经验的人都可以帮助我。
答案 0 :(得分:0)
我想你可能会这样做:
var childArticle = wait.forMethod(wikipedia, 'searchArticle', childQuery);