Ruby Challenge: Create a word occurrence counter

I'm a passionate software developer.
Search for a command to run...

I'm a passionate software developer.
No comments yet. Be the first to comment.
Revisited on July 25th, 2023 Description LeetCode Problem 217 Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct. Method structure This is the method structure ...
Components Introduction In order to truly understand props in React, we first need to be clear on what components are. A component is a reusable piece of the user interface that encapsulates structure, styling, and behavior. Instead of repeating the ...

Introduction Context By default, Codespace is using the GITHUB_TOKEN env var (the “Codespaces token”), which can’t create repos. It’s necessary to authenticate gh with a token/account that has the right scopes, or create the repo on the web first. Ob...

Solution Here it is: git commit --allow-empty-message -m "" Explanation To create a Git commit without a message, or with an empty message, you can use the --allow-empty-message flag with the git commit command. git commit --allow-empty-message -m "...

Steps Cleanup phase This phase’s goal is to removes existing containers, volumes, networks and images Stop and remove volumes docker compose down --volumes --remove-orphans The previous command stops and removes containers, networks, and default volu...

Steps Stop the service It’s not mandatory, but before uninstalling, it's a good practice to stop any running Docker services. sudo systemctl stop docker Remove docker packages It’s used to completely and forcefully remove a list of specified packag...

TL; DR: Get the solution on my Github repository.
Create a word occurrence counter.
Given a sentence, such as the following one:
sentence = "I love Ruby, Ruby is a great language!"
The result would be a hash
{"I"=>1, "love"=>1, "Ruby,"=>2, "is"=>1, "a"=>1, "great"=>1, "language!"=>1}
Let's build our solution one step at a time, so, you can grasp it.
Basically, there are 3 steps:
Store the result;
Match words;
Count the words.
Let's start by creating a Hash object. This object is our counter.
def count_word_occurrences(sentence)
word_counter = Hash.new(0)
return word_counter
end
In this step, we're going to identify and separate each word.
In order to achieve this, we're going to use the scan method. Take a look at the following approach:
sentence.scan(/\w+/)
The previous code snippet uses the scan method. This method is provided by the Ruby's String class. It is used to search a string (sentence in this case) for all occurrences of a specified pattern and returns an array of all matches found.
In the given code snippet, the pattern specified is /\w+/.
\w is a metacharacter that matches any word character, which includes alphabetic characters (both uppercase and lowercase) and digits. It is equivalent to the character class [A-Za-z0-9_].
The plus sign + is a quantifier that specifies that the preceding pattern (\w in this case) should be matched one or more times consecutively. Therefore, /\w+/ matches one or more consecutive word characters.
The scan method scans the sentence string and finds all occurrences of the pattern /\w+/. It then returns an array containing all the words found in the sentence.
sentence = "I love Ruby, Ruby is a great language!"
def count_word_occurrences(sentence)
word_counter = Hash.new(0)
# Use regular expression to match each word in the sentence
@words = sentence.scan(/\w+/)
word_counter
end
count_word_occurrences(sentence)
puts @words
Since we're using the scan method, the previous code would result in an array:
["I", "love", "Ruby", "Ruby", "is", "a", "great", "language"]
Output

As we already checked, by using the scan method, we got an array of words.
Now, we need to iterate through this array and check if the word element is repeated, cause if it is, we're going to add one to the world_counter variable:
words.each do |word|
word_counter[word] += 1
end
The previous code is our cornerstone. Every iteration is going to add a key value to the word_counter
word_counter[word]
And add 1 to the word_counter if the word is repeated:
word_counter[word] += 1
sentence = "I love Ruby, Ruby is a great language!"
def count_word_occurrences(sentence)
word_counter = Hash.new(0)
words = sentence.scan(/\w+/)
words.each do |word|
word_counter[word] += 1
end
word_counter
end
puts count_word_occurrences(sentence)
Take a look at it here.

I hope this article helped you. Let me know if you have any questions.
Your thoughts, suggestions and corrections are more than welcome.
By the way, feel free to drop your suggestions on new blog articles.
Hope to see you next time.