我有一些Python代码需要转换成Javascript:
word_groups = defaultdict(set)
for sentence in sentences:
sentence.tokens = stemmed_words(sentence.str_)
for token in sentence.tokens:
word_groups[sentence.actual_val].add(token)
我对Javascript不是很了解,所以这是我能做到的最好:
var word_groups = {}
for(var isent = 0; isent < sentences.length; isent++) {
var sentence = sentences[isent]
sentence.tokens = stemmed_words(sentence.str_)
for(var itoken = 0; itoken < sentence.tokens.length; itoken++) {
var token = sentence.tokens[itoken]
if(!(sentence.actual_val in word_groups))
word_groups[sentence.actual_val] = []
var group = word_groups[sentence.actual_val]
if(!(token in group))
group.push(token)
}
}
有人能提供一些方法,使javascript代码更像python吗?