I have some Python code that I'm porting to 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)
I don't know a lot about Javascript, so this was the best I could do:
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)
}
}
Can anyone suggest ways to make the javascript code look more like the python?
I'm going to assume that if you're using an environment where forEach is available, reduce and Object.keys are available as well. (e.g. ECMAScript >= 1.8.5):
var word_groups = sentences.reduce(function (groups, sentence) {
var val = sentence.actual_val
var group = groups[val] = groups[val] || []
stemmed_words(sentence.str_).forEach(function (t) {
if (!(t in group)) group.push(t)
})
return groups
}, {})
It's quite possible that I've misinterpreted what your Python code does, but assuming you're after word counts, I'd write it as follows:
var word_groups = {}
sentences.forEach(function (sentence) {
sentence.tokens = stemmed_words(sentence.str_)
sentence.tokens.forEach(function (token) {
var val = sentence.actual_val
word_groups[val] = (word_groups[val] || 0) + 1
})
})
The above will fail should the word "constructor" appear in the input. It's possible to work around this JavaScript quirk:
var word_groups = {}
sentences.forEach(function (sentence) {
sentence.tokens = stemmed_words(sentence.str_)
sentence.tokens.forEach(function (token) {
var val = sentence.actual_val
if (!word_groups.hasOwnProperty(val)) word_groups[val] = 0
word_groups[val] += 1
})
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With