Results 1 to 5 of 5

Thread: percent of a word in a richtextbox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2011
    Posts
    294

    Wink percent of a word in a richtextbox

    Is possible get the percent of a word in the content of a richtexbox? if yes, can you give me some example?

    For example if I have 100 words in a richtextbox and I want to know what is the percent of a word in this content (for example if a word called "music" is repeated 10 times = 10%. If this is not possible in a richtextbox, is possible in other box like textbox?

    Sorry for my bad english.

  2. #2
    Hyperactive Member
    Join Date
    Apr 2011
    Location
    England
    Posts
    421

    Re: percent of a word in a richtextbox

    Hi romanos,

    Here is a function you can try that uses the String.Split Method and the Dictionary Class (I have marked the code that you might want to edit yourself using ++++++++'s):
    VB.NET Code:
    1. Public Class Form1
    2.  
    3.     Private Function WordPercentage(ByVal AllWords As Boolean, Optional ByVal WordToFind As String = Nothing) As String
    4.  
    5.         'If you specify False to matching AllWords and don't provide a WordToFind then there can be no results
    6.         If Not (AllWords) AndAlso WordToFind = Nothing Then Return Nothing
    7.  
    8.         'Create a lookup table that can store every word found in the RichTextBox and the number of times it occurs
    9.         Dim UniqueWords As New Dictionary(Of String, Integer)
    10.         'If you only want the percentage for a specific word then set the word in the Table. (No further entries will be added)
    11.         If Not (AllWords) Then UniqueWords.Add(WordToFind, 0)
    12.  
    13.         'Split the TichTextBox using the Space char. This will leave you with a string array containing all the words
    14.         Dim RTBWords() As String = RichTextBox1.Text.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
    15.  
    16.         'Calculate the total number of words in the RichTextBox (needed to calculate the percentage)
    17.         Dim NoWords As Integer = RTBWords.Count
    18.  
    19.         'If there are no words then exit the sub
    20.         If NoWords = 0 Then Return Nothing
    21.  
    22.         For Each Word As String In RTBWords
    23.  
    24.             '+++++++ Remove this line if you do not want to allow case-insensitive matches +++++++
    25.             Word = Word.ToLower
    26.  
    27.             'Check if the current word was found in the UniqueWords lookup table
    28.             If UniqueWords.ContainsKey(Word) Then
    29.                 'If it was then add 1 to the word count
    30.                 UniqueWords.Item(Word) += 1
    31.             Else
    32.                 'If you want to find the percentage of all the words in the RichTextBox then add each word into the Lookup table
    33.                 If AllWords Then UniqueWords.Add(Word, 1)
    34.             End If
    35.  
    36.         Next Word
    37.  
    38.  
    39.         '++++++++++++ Edit the Output to suit your own needs ++++++++++++
    40.  
    41.         'Compile the results using stringbuilder
    42.         Dim Output As New System.Text.StringBuilder("The following Words were found in the RichTextBox:" & vbCrLf & vbCrLf)
    43.         'For every word that was found in the RichTextBox
    44.         For Each Entry As KeyValuePair(Of String, Integer) In UniqueWords
    45.             'Append the word to the output and the calculated percentage
    46.             Output.AppendLine(Entry.Key & ": " & ((Entry.Value * 100) / NoWords).ToString & "%")
    47.         Next Entry
    48.  
    49.         '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    50.  
    51.         'Return the Output
    52.         Return Output.ToString
    53.  
    54.     End Function
    55.  
    56. End Class
    You can implement this function in your code using something like:
    VB.NET Code:
    1. Private Sub YourSub()
    2.     'If you want to get the percentage of all the words in the RichTextBox:
    3.     Dim AllWordsPercentage As String = WordPercentage(True)
    4.     If Not (AllWordsPercentage) = Nothing Then MessageBox.Show(AllWordsPercentage) Else MessageBox.Show("No results were found")
    5.     'If you want to get the percentage of a particular word (e.g. "music"):
    6.     Dim SpecificWordPercentage As String = WordPercentage(False, "music")
    7.     If Not (SpecificWordPercentage) = Nothing Then MessageBox.Show(SpecificWordPercentage) Else MessageBox.Show("No results were found")
    8. End Sub
    Hope this helps

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: percent of a word in a richtextbox

    here's my example using regex + LINQ. it also uses an rtb, a button + a textbox (txtFind):

    vb Code:
    1. Imports System.Text.RegularExpressions
    2.  
    3. Public Class Form1
    4.  
    5.     Private Structure word
    6.         Dim count As Integer
    7.         Dim percentage As Decimal
    8.     End Structure
    9.  
    10.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    11.         Dim words() As String = Regex.Split(RichTextBox1.Text, "\s|\,\s?|\.\s?|\;\s?|\:\s?|$")
    12.         Dim wordDictionary As Dictionary(Of String, word) = (From w In words.Where(Function(s) s.Trim <> "").Distinct _
    13.                                                              Select New With { _
    14.                                                              .word = w, _
    15.                                                              .count = words.Count(Function(s) w.ToLower = s.ToLower), _
    16.                                                              .percentage = (.count * 100) / words.Where(Function(s) s.Trim <> "").Count}). _
    17.                                                              ToDictionary(Function(kvp) kvp.word, Function(kvp) New word With _
    18.                                                                                                       {.count = kvp.count, .percentage = kvp.percentage})
    19.         If wordDictionary.Keys.Contains(txtFind.Text) Then _
    20.             MsgBox(String.Format("Word: {0}, occurences {1}%", txtFind.Text, wordDictionary(txtFind.Text).percentage))
    21.     End Sub
    22.  
    23. End Class

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2011
    Posts
    294

    Wink Re: percent of a word in a richtextbox

    Thanks for your help.

    Quote Originally Posted by jay20aiii View Post
    Hi romanos,

    Here is a function you can try that uses the String.Split Method and the Dictionary Class (I have marked the code that you might want to edit yourself using ++++++++'s):
    VB.NET Code:
    1. Public Class Form1
    2.  
    3.     Private Function WordPercentage(ByVal AllWords As Boolean, Optional ByVal WordToFind As String = Nothing) As String
    4.  
    5.         'If you specify False to matching AllWords and don't provide a WordToFind then there can be no results
    6.         If Not (AllWords) AndAlso WordToFind = Nothing Then Return Nothing
    7.  
    8.         'Create a lookup table that can store every word found in the RichTextBox and the number of times it occurs
    9.         Dim UniqueWords As New Dictionary(Of String, Integer)
    10.         'If you only want the percentage for a specific word then set the word in the Table. (No further entries will be added)
    11.         If Not (AllWords) Then UniqueWords.Add(WordToFind, 0)
    12.  
    13.         'Split the TichTextBox using the Space char. This will leave you with a string array containing all the words
    14.         Dim RTBWords() As String = RichTextBox1.Text.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
    15.  
    16.         'Calculate the total number of words in the RichTextBox (needed to calculate the percentage)
    17.         Dim NoWords As Integer = RTBWords.Count
    18.  
    19.         'If there are no words then exit the sub
    20.         If NoWords = 0 Then Return Nothing
    21.  
    22.         For Each Word As String In RTBWords
    23.  
    24.             '+++++++ Remove this line if you do not want to allow case-insensitive matches +++++++
    25.             Word = Word.ToLower
    26.  
    27.             'Check if the current word was found in the UniqueWords lookup table
    28.             If UniqueWords.ContainsKey(Word) Then
    29.                 'If it was then add 1 to the word count
    30.                 UniqueWords.Item(Word) += 1
    31.             Else
    32.                 'If you want to find the percentage of all the words in the RichTextBox then add each word into the Lookup table
    33.                 If AllWords Then UniqueWords.Add(Word, 1)
    34.             End If
    35.  
    36.         Next Word
    37.  
    38.  
    39.         '++++++++++++ Edit the Output to suit your own needs ++++++++++++
    40.  
    41.         'Compile the results using stringbuilder
    42.         Dim Output As New System.Text.StringBuilder("The following Words were found in the RichTextBox:" & vbCrLf & vbCrLf)
    43.         'For every word that was found in the RichTextBox
    44.         For Each Entry As KeyValuePair(Of String, Integer) In UniqueWords
    45.             'Append the word to the output and the calculated percentage
    46.             Output.AppendLine(Entry.Key & ": " & ((Entry.Value * 100) / NoWords).ToString & "%")
    47.         Next Entry
    48.  
    49.         '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    50.  
    51.         'Return the Output
    52.         Return Output.ToString
    53.  
    54.     End Function
    55.  
    56. End Class
    You can implement this function in your code using something like:
    VB.NET Code:
    1. Private Sub YourSub()
    2.     'If you want to get the percentage of all the words in the RichTextBox:
    3.     Dim AllWordsPercentage As String = WordPercentage(True)
    4.     If Not (AllWordsPercentage) = Nothing Then MessageBox.Show(AllWordsPercentage) Else MessageBox.Show("No results were found")
    5.     'If you want to get the percentage of a particular word (e.g. "music"):
    6.     Dim SpecificWordPercentage As String = WordPercentage(False, "music")
    7.     If Not (SpecificWordPercentage) = Nothing Then MessageBox.Show(SpecificWordPercentage) Else MessageBox.Show("No results were found")
    8. End Sub
    Hope this helps

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2011
    Posts
    294

    Wink Re: percent of a word in a richtextbox

    Paul this work superrrrr cooooool. Thanks you are always a big help, God bless you.

    Quote Originally Posted by .paul. View Post
    here's my example using regex + LINQ. it also uses an rtb, a button + a textbox (txtFind):

    vb Code:
    1. Imports System.Text.RegularExpressions
    2.  
    3. Public Class Form1
    4.  
    5.     Private Structure word
    6.         Dim count As Integer
    7.         Dim percentage As Decimal
    8.     End Structure
    9.  
    10.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    11.         Dim words() As String = Regex.Split(RichTextBox1.Text, "\s|\,\s?|\.\s?|\;\s?|\:\s?|$")
    12.         Dim wordDictionary As Dictionary(Of String, word) = (From w In words.Where(Function(s) s.Trim <> "").Distinct _
    13.                                                              Select New With { _
    14.                                                              .word = w, _
    15.                                                              .count = words.Count(Function(s) w.ToLower = s.ToLower), _
    16.                                                              .percentage = (.count * 100) / words.Where(Function(s) s.Trim <> "").Count}). _
    17.                                                              ToDictionary(Function(kvp) kvp.word, Function(kvp) New word With _
    18.                                                                                                       {.count = kvp.count, .percentage = kvp.percentage})
    19.         If wordDictionary.Keys.Contains(txtFind.Text) Then _
    20.             MsgBox(String.Format("Word: {0}, occurences {1}%", txtFind.Text, wordDictionary(txtFind.Text).percentage))
    21.     End Sub
    22.  
    23. End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width