import numpy as np
from sklearn.feature_extraction.text import CountVectorizer
def wordcount(data):
cv = CountVectorizer(min_df=0, strip_accents="ascii", decode_error ="ignore")
counts = cv.fit_transform([data]).toarray().ravel()
words = np.array(cv.get_feature_names())
counts = map(int, counts)
words = [i.tolist() for i in words]
words = [x.encode('UTF8') for x in words]
unsorted_result = zip(words, counts)
print sorted(unsorted_result,key=lambda x: x[1], reverse=True)
var letterArray = "asdf\njkasdf\nthe\nsadf".split('\n');
function count(letterArray) {
let mapping = {};
for (let i=0; i < letterArray.length; i++){
if (mapping[letterArray[i]] !== undefined){ // if the letter already exists in the mapping increment it
mapping[letterArray[i]] += 1;
}else { //if the letter does not exist add it and initialize it
mapping[letterArray[i]] = 1;
}
}
return mapping;
}
console.log("count: ", count(letterArray));
在节点.js在做
npm i split2 through2 -S
之后我使用python方法得到了您所期望的结果
将数据作为字符串发送到此函数
您可以稍微修改它以接受来自文本文件的输入
相关问题 更多 >
编程相关推荐