我正在统计进入地图的.txt文件中的单词。之后,我想检查每个单词,看它是否是一个名词。
我的代码可能不是最优化的,但是可以工作。 我已经尝试将这段代码放入各个领域,无论我尝试什么,都不会起作用。我还为.csv文件创建了基本配置。
这是当前情况:
function createWordMap(wordsArray) {
// count the number of appearances for each word
var wordsMap = {};
wordsArray.forEach(function(count) {
if (wordsMap.hasOwnProperty(count)) {
wordsMap[count]++;
} else {
wordsMap[count] = 1;
}
});
console.log ('wordsMap Done')
return wordsMap;
}
function sortByCount(wordsMap) {
// create the map that will go into the .cvs file
var finalWordsArray = [];
// This should add a value to the map
Object.keys(wordsMap).forEach(function(Noun) {
wordpos.isNoun(Noun).then(function(value){
if (value === true) {
wordsMap[Noun] = 'Noun';
} else {
wordsMap[Noun] = '';
}})});
finalWordsArray = Object.keys(wordsMap).map(function(key, count, Noun) {
return {
name: key,
total: wordsMap[count],
noun: wordsMap[Noun] };
});
// sort by count in descending order
finalWordsArray.sort(function(a, b) {
return b.total - a.total;
});
console.log ('finalWordsArray Done')
return finalWordsArray;
}
这是原始代码,非常适合计数出现次数:
function createWordMap(wordsArray) {
// count the number of appearances for each word
var wordsMap = {};
wordsArray.forEach(function(key) {
if (wordsMap.hasOwnProperty(key)) {
wordsMap[key]++;
} else {
wordsMap[key] = 1;
}
});
return wordsMap;
}
function sortByCount(wordsMap) {
// create the map that will go into the .cvs file
var finalWordsArray = [];
finalWordsArray = Object.keys(wordsMap).map(function(key) {
return {
name: key,
total: wordsMap[key]};
});
// sort by count in descending order
finalWordsArray.sort(function(a, b) {
return b.total - a.total;
});
return finalWordsArray;
}
这是我要添加到现有代码中的代码:
wordpos.isNoun(test).then(function(value){
if (value === true) {
console.log ('Noun')
} else {
console.log ('Not Noun')
}})
原始代码提供输出
Name;Total;Noun;verb;adjec;adverb
IS;22226;;;;
THE;21136;;;;
I;12359;;;;
AND;11845;;;;
etc ...
预期输出为
Name;Total;Noun;verb;adjec;adverb
IS;22226;Noun;;;
THE;21136;Noun;;;
I;12359;;Verb;;
AND;11845;;;Adverb;
etc ...
现在,代码似乎永远不会完成,或者可能只需要一段时间,但是不应该花那么长时间。原始代码大约需要2-3秒才能完成。也没有创建任何输出,我确实尽我所能尝试了一切