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

349
Views
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 answers
Answer question

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 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!