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.
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:
Public Class Form1
Private Function WordPercentage(ByVal AllWords As Boolean, Optional ByVal WordToFind As String = Nothing) As String
'If you specify False to matching AllWords and don't provide a WordToFind then there can be no results
If Not (AllWords) AndAlso WordToFind = Nothing Then Return Nothing
'Create a lookup table that can store every word found in the RichTextBox and the number of times it occurs
Dim UniqueWords As New Dictionary(Of String, Integer)
'If you only want the percentage for a specific word then set the word in the Table. (No further entries will be added)
If Not (AllWords) Then UniqueWords.Add(WordToFind, 0)
'Split the TichTextBox using the Space char. This will leave you with a string array containing all the words
Dim RTBWords() As String = RichTextBox1.Text.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
'Calculate the total number of words in the RichTextBox (needed to calculate the percentage)
Dim NoWords As Integer = RTBWords.Count
'If there are no words then exit the sub
If NoWords = 0 Then Return Nothing
For Each Word As String In RTBWords
'+++++++ Remove this line if you do not want to allow case-insensitive matches +++++++
Word = Word.ToLower
'Check if the current word was found in the UniqueWords lookup table
If UniqueWords.ContainsKey(Word) Then
'If it was then add 1 to the word count
UniqueWords.Item(Word) += 1
Else
'If you want to find the percentage of all the words in the RichTextBox then add each word into the Lookup table
If AllWords Then UniqueWords.Add(Word, 1)
End If
Next Word
'++++++++++++ Edit the Output to suit your own needs ++++++++++++
'Compile the results using stringbuilder
Dim Output As New System.Text.StringBuilder("The following Words were found in the RichTextBox:" & vbCrLf & vbCrLf)
'For every word that was found in the RichTextBox
For Each Entry As KeyValuePair(Of String, Integer) In UniqueWords
'Append the word to the output and the calculated percentage
Output.AppendLine(Entry.Key & ": " & ((Entry.Value * 100) / NoWords).ToString & "%")
Next Entry
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'Return the Output
Return Output.ToString
End Function
End Class
You can implement this function in your code using something like:
VB.NET Code:
Private Sub YourSub()
'If you want to get the percentage of all the words in the RichTextBox:
Dim AllWordsPercentage As String = WordPercentage(True)
If Not (AllWordsPercentage) = Nothing Then MessageBox.Show(AllWordsPercentage) Else MessageBox.Show("No results were found")
'If you want to get the percentage of a particular word (e.g. "music"):
Dim SpecificWordPercentage As String = WordPercentage(False, "music")
If Not (SpecificWordPercentage) = Nothing Then MessageBox.Show(SpecificWordPercentage) Else MessageBox.Show("No results were found")
End Sub
Hope this helps :)
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:
Imports System.Text.RegularExpressions
Public Class Form1
Private Structure word
Dim count As Integer
Dim percentage As Decimal
End Structure
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim words() As String = Regex.Split(RichTextBox1.Text, "\s|\,\s?|\.\s?|\;\s?|\:\s?|$")
Dim wordDictionary As Dictionary(Of String, word) = (From w In words.Where(Function(s) s.Trim <> "").Distinct _
Select New With { _
.word = w, _
.count = words.Count(Function(s) w.ToLower = s.ToLower), _
.percentage = (.count * 100) / words.Where(Function(s) s.Trim <> "").Count}). _
ToDictionary(Function(kvp) kvp.word, Function(kvp) New word With _
{.count = kvp.count, .percentage = kvp.percentage})
If wordDictionary.Keys.Contains(txtFind.Text) Then _
MsgBox(String.Format("Word: {0}, occurences {1}%", txtFind.Text, wordDictionary(txtFind.Text).percentage))
End Sub
End Class
Re: percent of a word in a richtextbox
Thanks for your help.
Quote:
Originally Posted by
jay20aiii
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:
Public Class Form1
Private Function WordPercentage(ByVal AllWords As Boolean, Optional ByVal WordToFind As String = Nothing) As String
'If you specify False to matching AllWords and don't provide a WordToFind then there can be no results
If Not (AllWords) AndAlso WordToFind = Nothing Then Return Nothing
'Create a lookup table that can store every word found in the RichTextBox and the number of times it occurs
Dim UniqueWords As New Dictionary(Of String, Integer)
'If you only want the percentage for a specific word then set the word in the Table. (No further entries will be added)
If Not (AllWords) Then UniqueWords.Add(WordToFind, 0)
'Split the TichTextBox using the Space char. This will leave you with a string array containing all the words
Dim RTBWords() As String = RichTextBox1.Text.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
'Calculate the total number of words in the RichTextBox (needed to calculate the percentage)
Dim NoWords As Integer = RTBWords.Count
'If there are no words then exit the sub
If NoWords = 0 Then Return Nothing
For Each Word As String In RTBWords
'+++++++ Remove this line if you do not want to allow case-insensitive matches +++++++
Word = Word.ToLower
'Check if the current word was found in the UniqueWords lookup table
If UniqueWords.ContainsKey(Word) Then
'If it was then add 1 to the word count
UniqueWords.Item(Word) += 1
Else
'If you want to find the percentage of all the words in the RichTextBox then add each word into the Lookup table
If AllWords Then UniqueWords.Add(Word, 1)
End If
Next Word
'++++++++++++ Edit the Output to suit your own needs ++++++++++++
'Compile the results using stringbuilder
Dim Output As New System.Text.StringBuilder("The following Words were found in the RichTextBox:" & vbCrLf & vbCrLf)
'For every word that was found in the RichTextBox
For Each Entry As KeyValuePair(Of String, Integer) In UniqueWords
'Append the word to the output and the calculated percentage
Output.AppendLine(Entry.Key & ": " & ((Entry.Value * 100) / NoWords).ToString & "%")
Next Entry
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'Return the Output
Return Output.ToString
End Function
End Class
You can implement this function in your code using something like:
VB.NET Code:
Private Sub YourSub()
'If you want to get the percentage of all the words in the RichTextBox:
Dim AllWordsPercentage As String = WordPercentage(True)
If Not (AllWordsPercentage) = Nothing Then MessageBox.Show(AllWordsPercentage) Else MessageBox.Show("No results were found")
'If you want to get the percentage of a particular word (e.g. "music"):
Dim SpecificWordPercentage As String = WordPercentage(False, "music")
If Not (SpecificWordPercentage) = Nothing Then MessageBox.Show(SpecificWordPercentage) Else MessageBox.Show("No results were found")
End Sub
Hope this helps :)
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.
here's my example using regex + LINQ. it also uses an rtb, a button + a textbox (txtFind):
vb Code:
Imports System.Text.RegularExpressions
Public Class Form1
Private Structure word
Dim count As Integer
Dim percentage As Decimal
End Structure
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim words() As String = Regex.Split(RichTextBox1.Text, "\s|\,\s?|\.\s?|\;\s?|\:\s?|$")
Dim wordDictionary As Dictionary(Of String, word) = (From w In words.Where(Function(s) s.Trim <> "").Distinct _
Select New With { _
.word = w, _
.count = words.Count(Function(s) w.ToLower = s.ToLower), _
.percentage = (.count * 100) / words.Where(Function(s) s.Trim <> "").Count}). _
ToDictionary(Function(kvp) kvp.word, Function(kvp) New word With _
{.count = kvp.count, .percentage = kvp.percentage})
If wordDictionary.Keys.Contains(txtFind.Text) Then _
MsgBox(String.Format("Word: {0}, occurences {1}%", txtFind.Text, wordDictionary(txtFind.Text).percentage))
End Sub
End Class