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

348
Visualizações
How to find and count all repeating 2/3/4 word phrases

I've been trying to parse 300-400 word pieces of text and get all repeating 2/3/4 word phrases and I can't figure out the solution.

With one word commonalities I simply .split the text, .each_slice 1, then .map and .tally.

But with 2+ words, .each_slice doesn't work since I can't account for order.. For example the text is:

An alligator walked and an alligator walked alone.

It's gonna split to:

The alligator | walked and | another alligator | walked alone

What can I code to recognize that "alligator walked" repeats itself?

The only solution I can think of is removing the first word each time, that way the pair is gonna change constantly and get all options, but that seems awfully stupid..

Any help is appreciated!

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The method you are looking for is Enumerable#each_cons, not Enumerable#each_slice.

I don't know exactly what your current solution looks like (it would have been better to share your actual code instead of a loose description of your code: "I simply .split the text, .each_slice 1, then .map and .tally"!), but for example you could do something like:

input = "An alligator walked and an alligator walked alone"

input.split(' ').map(&:downcase).each_cons(2).tally
# => {
#   ["an", "alligator"]=>2,
#   ["alligator", "walked"]=>2,
#   ["walked", "and"]=>1,
#   ["and", "an"]=>1,
#   ["walked", "alone"]=>1
#  }
over 4 years ago · Santiago Trujillo 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