Click here to Skip to main content
14,096,353 members
Rate this:
 
Please Sign up or sign in to vote.
Hi, I recognize this question has already been answered and it's an old post. But, I have been attempting this for a long time using many different codes (which didn't work) to find repeated words in a Rich Text Box, and then I found Solution number 2 under this heading, which is the code I found that works for me!

I'm very grateful for the example code, but I would like to find a way to list a single instance of each repeated word found in RTB1 text in the first column of a Listview (i.e. Word), then in the second column (i.e. Frequency) of the same listview, I would also like to show a frequency count of each repeated word found in RTB1?

I have tested this by showing the result of duplicate_words in a separate RTB(2) and using the phrase: This is a test, repeated several times. I get the following (i.e. desired) result:

Code:
RichTextBox2.Text = duplicate_names


Result:

is
a
test
This

However, when I attempt to show this same result in the Word column of ListView1, I get the following (i.e. undesired) result:

Code:
'For Each Value As String In duplicate_names OR
        Dim Value As String = duplicate_names

With
ListView1.Items.Add(Value)


(Note: I have separately tried both the For each loop and Dim value line together with the LV.Items.Add line)

Result:

isatestThis

(I.e. this shows in the first row of the Word column?)

1. How can I get the repeated words to show as a list on separate rows (i.e. one under the other) in LV1 in the same way they show in the RTB2 example?
2. How can I show the count for each repeated word listed in LV1 alongside each word in column 2 (i.e. Frequency).
3. How can I show the repeated word frequency percentage in a third column?

The results I would like to achieve would be something like the following:

WORD FREQUENCY %
is 6 25
a 6 25
test 6 25
This 6 25

I would very much appreciate any help on this as I have spent a lot of time searching the internet and testing stuff, but haven't managed to achieve the desired results yet!

What I have tried:

Code: <pre>RichTextBox2.Text = duplicate_names


Result:

is
a
test
This


Code: <pre>'For Each Value As String In duplicate_names OR
        Dim Value As String = duplicate_names

With
ListView1.Items.Add(Value)


Result:

isatestThis


Desired Result (Total words in text = 24):

WORD      FREQUENCY      %
is            6          25
a             6          25
test          6          25
This          6          25
Posted
Updated 25-Apr-19 9:39am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Use a dictionary: Dictionary<string,int>

Use string.Split on your text to get words (tokens[]).

Iterate over the tokens, adding them to the dictionary (with count 1) or updating (count++).

You wind up with a dictionary of words and frequency counts.

Use LINQ to get sums and percentages.

int count = 0;

if (myDic.ContainsKey(token)) {
   count = myDic[token];
}

myDic[token] = ++count;
   
Comments
Member 13032047 25-Apr-19 17:50pm
   
Thanks for your help Gerry! I'll have a look back at using dictionaries to refresh my memory! Also, I will need to study up on LINQ! I don't know if you took a look at the code under the original heading, but I think the code uses an array? Would it be possible to add to the original code using the repeated words list generated in the array by that code, etc.? Not very experienced at programming, but I'm learning! :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.190518.1 | Last Updated 25 Apr 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100