How to get the first 3 words that appear most in a string

Hi All,

I have a question… i am trying to get the 3 words that appear most in a string. this is what i have

strng = “i am to go up i am to go down i am coming up to reach the highest point "
ListOfWords = strng.Split(” ").GroupBy(Function(w) w).OrderByDescending(Function(g) g.Count())
but the work flow keeps displaying the error - "option strict on disallows implicit conversions from “string” to “char”

ListOfWords is type system.string

What could i be doing wrong?

strng.Split(” ")
You need to use a character when splitting text, because the quotes sees it as a string.

So to do that you can simply use “c” next to it or “(0)” to take the first character.
strng.Split(" "c)
strng.Split(" "(0))
strng.Split({" "}, System.StringSplitOptions.RemoveEmptyEntries)
The last option works for strings by converting it to a character array, and you can use SplitOptions with it.

This will fix the “…from string to char” erorr you got.

As for the rest of your code, it looks right but I’m not sure.

Regards.

Thanks for the reply but i dont seem to fully understand your explanation - first i am trying to split the string into an array of strings like so - {“i”,“am”,“to”,“go”,“up”,“i”,“am”, etc} how can i achieve this?

you dont need to do this, use activity called split string input your string and Specify " " as separator

this activity will return array of string, and you can retrieve first three element from an array

wow thanks didnt know there was such activity. Also i attached a screenshot to this message, i’m wondering why it still gives an error. the list of strings is stored in ListOfWords and i assigned it to this ListOfWords.GroupBy(Function(w) w).OrderByDescending(Function(g) g.Count()), trying to get the words that appear most. What could i be doing wrong?

Error Says it very clearly, your Linq is returning a value of type IOrderedEnumerable(grouping(String)) and you are trying to assign that to String, this is issue with TypeCasting.

oh thanks i missed out something. It works now

1 Like

One more thing… im trying to print out the values but it keeps giving me the type like so - System.Linq.Lookup`2+Grouping[System.String,System.String]

System.Linq.Lookup`2+Grouping[System.String,System.String]

System.Linq.Lookup`2+Grouping[System.String,System.String]

System.Linq.Lookup`2+Grouping[System.String,System.String]

System.Linq.Lookup`2+Grouping[System.String,System.String]

System.Linq.Lookup`2+Grouping[System.String,System.String]

System.Linq.Lookup`2+Grouping[System.String,System.String]

System.Linq.Lookup`2+Grouping[System.String,System.String]

how can i get the key and value instead?

your expression is returning IGrouping its not returning iEnumerable, i think you should change get, which am not sure:|

You think i should change what? :slight_smile:

See this splitandgetcount.xaml (7.7 KB)

Exactly the output i need. Gracias!!

1 Like

De nada! :slight_smile:

Hi @Olaoluwa,

I see that you’ve got your answer here. However, I’d just like to explain what @ClaytonM was trying to help you understand.

When you do something like stringVariable.Split(" "), the method considers " " as a string, EVEN THOUGH it is a string with one character (Space).

The method requires a value of character type. So, instead you do this:

stringVariable.Split(" “(0)) or stringVariable.Split(” "c).This specifies that you’re using a character, not a string.

The other, simpler method is to do it this way:

Split(stringVariable," "), which splits the original string into an array of strings.

4 Likes

Oh thanks for explaining further, i understand it now.