Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

236
Views
How do I count the number of word combinations from a array of sentences?

Which js function can solve the following problem?

["lemon hy te", 
"tracy austin", 
"tracy austin mk3", 
"ford mondeo mk3"]

Above you see a list of 4 sentences in an array. The sentences consist of more than 1 words. There will be 100000 to 200000 sentences in the array.

step 1) For each value in the array, do the following. Divide the sentence by word and add the result to the variable resultArr.

So this way: "lemon" "hy" "te"

let resultArr = []

step 2) Then divide the same sentence into each two word combination without changing the order of the sentence and add the result to the variable resultArr.

So in this way: "lemon hy" "hy te"

step 3) Then divide the same sentence into each three word combination without changing the order of the sentence and add the result to the variable resultArr.

So in this way: "lemon hy te"

In step 4,5,6,7 etc repeat this pattern. If a sentence consists of 4 words then in step 7 for example you don't need to add the whole word to the array again. That was already done in step 4.

Variable resultArr now contains the following ["lemon", "hy", "te", "lemon hy", "hy te", "lemon hy te"]

Next, I would like to know how many times each word combination occurs in resultArr. In this case, the result would be the following.

result 1:

[["lemon", 1]
["hy", 1],
["te", 1],
["lemon hy", 1]
["hy te", 1],
["lemon hy te", 1]]

If the result contains the same word combination with number twice as shown in the example below then 1 time ["hy", 1] may be removed leaving ["hy", 1] 1 time.

result 2:

[["lemon", 1]]
["hy", 1]]
["hy", 1],
["te", 1],
["lemon hy", 1]
["hy te", 1],
["lemon hy te", 1]]

Now for the last thing then the function should be called getWordCombos(range, minAmountOfWords, maxAmountOfWords). As you may have noticed, the last two arguments were not explained by me.

Suppose I enter 4 and 12 then the function I call looks something like this. getWordCombos(range, 4, 12) then only the previously described steps 4 to 12 need to be executed.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This is my answer:

function getWordCombos(list, minWords, maxWords){
  
  let resultArr = [];
  let temp = [];
  maxWords += 1;
  
  for(let words = minWords; words < maxWords; words++){
    
    for(let l of list){
      
      temp = l.slice().split(" ");
      
      if(temp.length + 1 < words){ continue; }
      
      while(temp.length + 1 > words){
        
        resultArr.push([temp.slice(0, words).join(" "), 1])
        
        for(let r = 0; r < words; r++){
          temp.shift();
        }
        
      }
      
    }
    
  }
  
  for(let w in resultArr){
    
    for(let ws in resultArr){
      
      if(w == ws){ continue; }
      
      if(resultArr[w][0] == resultArr[ws][0]){
        
        resultArr.splice(ws, 1);
        
        resultArr[w][1]++;
        
        
      }
      
    }
    
  }
  
  return resultArr;
  
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!