Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

242
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda