Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can this Javascript be expressed more succinctly?

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?

like image 426
Jesse Aldridge Avatar asked Dec 07 '25 12:12

Jesse Aldridge


2 Answers

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
}, {})
like image 168
grncdr Avatar answered Dec 09 '25 01:12

grncdr


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
  })
})
like image 41
davidchambers Avatar answered Dec 09 '25 01:12

davidchambers