Identify Repetitive words in a String and their counts (Quick approach)

“big black bug bit a big black dog on his big black nose” – Input Statement

Task is to identify repetitive words and their counts.

Please provide me with workflow snaps to understand its solution in detail.

P.S.: I am using UiPath studio Version 2023.4.0

Thanks and Regards,
Yugandhara Shirbhate

Hi @Yugandhara_Shirbhate

Here is my quick approach:

The Flow:

The Invoke Code:

Dim words = inputText.ToLower().Split(" "c)

For Each word As String In words
    If wordCountDict.ContainsKey(word) Then
        wordCountDict(word) += 1
    Else
        wordCountDict(word) = 1
    End If
Next

The Invoke Code Arguments

The Output:

Explanation to the Code, If you didn’t understand it:

//Convert text to lower case and then convert to an Array using Split function
//You can use inputText.Split(" "c) if you want to consider case-sensitivity.

Dim words = inputText.ToLower().Split(" "c) 

//Loop through the Array of Words

For Each word As String In words

   // If the word is present in the Dictionary as a Key, Then Increment the count
    If wordCountDict.ContainsKey(word) Then
        wordCountDict(word) += 1

    // If the word is not present in the Dictionary as a Key, then create it with count value 1
    Else
        wordCountDict(word) = 1
    End If
Next

If you want the Output in a Certain Way, Do let me know.

If you add the below code with outputText as Out Argument in that Invoke Code, you will get a String Output.

outputText = ""
For Each keyValue As KeyValuePair(Of String, Integer) In wordCountDict
    outputText &= keyValue.Key & ": " & keyValue.Value.ToString() & Environment.NewLine
Next

If this solves your query, Do mark it as a solution
Happy Automation :star_struck:

Hi @Yugandhara_Shirbhate

To add to my previous response:

If you want only the words that are repeated (ignore the words with count=1), Then Update the Invoke code as shown below:

Dim words = inputText.ToLower().Split(" "c)

For Each word As String In words
    If wordCountDict.ContainsKey(word) Then
        wordCountDict(word) += 1
    Else
        wordCountDict(word) = 1
    End If
Next

// The below code, will remove the words that have count = 1

For Each kvp As KeyValuePair(Of String, Integer) In wordCountDict
    If kvp.Value = 1 Then
		wordCountDict.Remove(kvp.Key)
    End If
Next

The output:

Hi @Yugandhara_Shirbhate

Use the below syntax in Assign activity:

Assign -> inputText = "big black bug bit a big black dog on his big black nose"

Assign -> words = inputText.ToLower().Split({" "c}, StringSplitOptions.RemoveEmptyEntries)

Assign -> wordCountDict = words.GroupBy(Function(w) w).ToDictionary(Function(g) g.Key, Function(g) g.Count())

Message Box -> String.Join(Environment.NewLine, wordCountDict.Select(Function(kvp) kvp.Key & ": " & kvp.Value))

DataTypes:

Output:

Hope it helps!!

2 Likes

Invoke Code always gives me the ick these days.

You can make a super nice Static Class for these now using the coded workflows functionality and make a static function to do it.

1 Like

Hello @Yugandhara_Shirbhate

An easy approach would be to use Regex .Matches.Count method to determine the number of times.

Download: Sequence.xaml (9.2 KB)

Regards
Soren

1 Like

@Jon_Smith I totally Agree. :100: