Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting the number of times two letters appear together

Tags:

regex

ruby

I am trying to make a Ruby program that counts the numer of times two letters appear together. This is what is written in the file I'm reading:

hola
chau

And this is what I'm trying to get:

ho;ol;la;ch;ha;au;
1;1;1;1;1;1;

I can't get it to work properly. This is my code so far:

file = File.read(gets.chomp)
todo = file.scan(/[a-z][a-z]/).each_with_object(Hash.new(0)) { 
    |a, b| b[a] += 1 
}

keys  = ''
values = ''

todo.each_key {
    |key| keys += key + ';' 
}
todo.each_value {
    |value| values += value.to_s + ';'
}

puts keys
puts values

This is the result I'm getting:

ho;la;ch;au;
1;1;1;1;

Why am I not getting every combination of characters? What should I ad to my regex so it would count every combination of characters?

like image 479
Catu Seoane Avatar asked Sep 01 '14 15:09

Catu Seoane


2 Answers

Because the characters are overlapped, you need to use a lookahead to capture the overlapped characters.

(?=([a-z][a-z]))

DEMO

like image 189
Avinash Raj Avatar answered Nov 15 '22 01:11

Avinash Raj


This is one way.

def char_pairs(str)
  str.split(/\s+/).flat_map { |w| w.chars.each_cons(2).map(&:join) }
                  .each_with_object({}) { |e,h| h[e] = (h[e] ||= 0 ) + 1 }
end

char_pairs("hello jello")
  #=> {"he"=>1, "el"=>2, "ll"=>2, "lo"=>2, "je"=>1}

char_pairs("hello yellow jello")
  #=> {"he"=>1, "el"=>3, "ll"=>3, "lo"=>3, "ye"=>1, "ow"=>1, "je"=>1}

Having the hash, it is an easy matter to convert it to any output format you desire.

like image 34
Cary Swoveland Avatar answered Nov 14 '22 23:11

Cary Swoveland