count concurrency words form a file

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2009
Posts: 11
Reputation: doomfrawen is an unknown quantity at this point 
Solved Threads: 0
doomfrawen doomfrawen is offline Offline
Newbie Poster

count concurrency words form a file

 
0
  #1
Aug 30th, 2009
Hi again guys I need to make a program that reads a txt file, show the original file on a RTB and then show the words of the file, sorted alphabetically with the number of words found on the file in another RTB, e.g.

INPUT:
"That fox killed my rabbit, but my dog killed the fox"

OUTPUT:

but 1
dog 1
fox 2
killed 2
my
rabbit 1
That 1
the 1

The program must convert all the strings to minus and eliminate periods , ), (.

The code that i have sort alphabetically, but instead of counting the words count the lines in which the words appear e.g.

OUTPUT:
but 1
dog 1
fox 1
fox 1
killed 1
killed 1
my 1
my 1
rabbit, 1
That 1
the 1

The part where I need to eliminate ., ), ( etc and convert to minus the words I have it but i don't know how to implement it into the code, so basically the program left the counting of concurrene words and convert all the words to minus and eliminate extra characters.

This is my code, the part that do the sort and need to introduce the changes starts @ line 68 and ends @line 87

  1. Imports System.IO
  2.  
  3. Public Class Form1
  4.  
  5. Private Sub AbrirToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AbrirToolStripMenuItem.Click
  6. Dim myStream As Stream = Nothing
  7.  
  8. Dim openFileDialog1 As New OpenFileDialog()
  9. With openFileDialog1 'dando las características al archivo que abriremos
  10. .InitialDirectory = "c:\" 'directorio inicial
  11. .Filter = "Archivos txt (*.txt)|*.txt|Todos los Archivos (*.*)|*.*" 'archivos que podra abrir
  12. .FilterIndex = 1 'indexe del archivo de lectura por defecto
  13. .RestoreDirectory = False 'restaurar directorio default al abrir de nuevo
  14. .Multiselect = False 'cancelando la multiple seleccion
  15. .Title = "Selecciona un Archivo de Texto" 'titulo de la ventana
  16. End With
  17. 'si se encontro el archivo
  18. If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
  19. Try
  20. 'abrir el archivo
  21. myStream = openFileDialog1.OpenFile()
  22. If (myStream IsNot Nothing) Then 'si no es nulo
  23. LoadToRichTextBox(myStream) 'que desamos hacer con el archivo
  24. End If
  25. Catch Ex As Exception
  26. MessageBox.Show("No se puede leer el archivo del disco. Error original: " & Ex.Message)
  27. Finally
  28.  
  29. 'comprobar esto de nuevo, ya que tenemos que asegurarnos de que no se produzca una excepcion cuando este abierto
  30. If (myStream IsNot Nothing) Then 'si no se escogio nada entonces
  31. myStream.Close() 'cerrar el streaming de datos
  32. End If
  33. End Try
  34. End If
  35. End Sub
  36. Private Sub GuardarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GuardarToolStripMenuItem.Click
  37.  
  38. Dim SaveFileDialog1 As New SaveFileDialog()
  39. With SaveFileDialog1
  40. .InitialDirectory = "c:/" 'directorio inicial
  41. .CheckFileExists = True 'checar primero si el archivo no existe primero
  42. .CheckPathExists = True 'checar si la ruta de acceso existe
  43. .Filter = "Archivos txt (*.txt)|*.txt|Todos los Archivos (*.*)|*.*"
  44. .FilterIndex = "1"
  45. .RestoreDirectory = False
  46. .Title = "Guardar Archivo de Texto"
  47. End With
  48. ' si la ventana de diaologo esta abierta y el resultado es positivo
  49. If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
  50. Dim bAppendToFile As Boolean = False
  51. 'se guarda todo el texto, nombre, y de donde se va a guardar
  52. My.Computer.FileSystem.WriteAllText( _
  53. SaveFileDialog1.FileName, RichTextBox2.Text, bAppendToFile)
  54. End If
  55.  
  56. End Sub
  57. Private Sub LoadToRichTextBox(ByVal myStream As Stream)
  58. Dim myStreamReader As StreamReader = New StreamReader(myStream)
  59.  
  60. Me.RichTextBox1.Clear() 'limpiando el RichTextBox
  61. 'hacemos un while para leer de principio a fin el archivo de texto
  62. While myStreamReader.Peek() >= 0
  63. Dim str As String = myStreamReader.ReadToEnd
  64. Me.RichTextBox1.Text = str.ToLower
  65. End While
  66.  
  67. End Sub
  68. Private Sub TabPage2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabPage2.VisibleChanged
  69. 'wtf como uso esto
  70. 'rtbString.Replace(".", "")
  71. 'txtString.Replace(".", "")
  72.  
  73. Dim WordList As New List(Of String) 'Esto contendra nuestra lista ordenada cuando haya terminado
  74. For i As Integer = 0 To RichTextBox1.Lines.Count - 1 'recorrer las primeras líneas del RTB
  75.  
  76. Dim SeparatedWords() As String = RichTextBox1.Lines(i).Split(" "c) 'dividir las palabras en esta línea hasta
  77.  
  78. For Each Word As String In SeparatedWords 'hacemos un bucle a traves de las palabras en esta linea
  79. WordList.Add(Word & " " & (i + 1)) 'añadir cada palabra y el número a nuestra lista
  80. Next
  81. Next
  82.  
  83. WordList.Sort() 'Acomoda alfabeticamente
  84.  
  85. RichTextBox2.Lines = WordList.ToArray 'establece el texto de la otroa RTB a nuestra lista de palabras ordenadas
  86. End Sub
  87. Private Sub SalirToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SalirToolStripMenuItem.Click
  88. Me.Close() 'cierro forma
  89. End Sub
  90.  
  91. Private Sub AcercaDeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AcercaDeToolStripMenuItem.Click
  92. AboutBox1.Visible = True 'hago visible la forma sobre...
  93. End Sub
  94. End Class

Any help will be very appreciated

Thanks in advanced!!

EDIT: I find an easy way to convert from mayus to minus the string words, so the program only left the delete function of characteres and the concurrency of words
Last edited by doomfrawen; Aug 30th, 2009 at 1:33 am. Reason: Fixed something on the code
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 258
Reputation: GeekByChoiCe is on a distinguished road 
Solved Threads: 50
GeekByChoiCe GeekByChoiCe is offline Offline
Posting Whiz in Training

Re: count concurrency words form a file

 
0
  #2
Aug 30th, 2009
i remember that kind of topic was few weeks ago already on that forum. did u use the search function on that site before posting?
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 11
Reputation: doomfrawen is an unknown quantity at this point 
Solved Threads: 0
doomfrawen doomfrawen is offline Offline
Newbie Poster

Re: count concurrency words form a file

 
0
  #3
Aug 30th, 2009
yep, i checked first the forums but i didn't find anything like this
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 258
Reputation: GeekByChoiCe is on a distinguished road 
Solved Threads: 50
GeekByChoiCe GeekByChoiCe is offline Offline
Posting Whiz in Training

Re: count concurrency words form a file

 
0
  #4
Aug 30th, 2009
Last edited by GeekByChoiCe; Aug 30th, 2009 at 1:58 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 11
Reputation: doomfrawen is an unknown quantity at this point 
Solved Threads: 0
doomfrawen doomfrawen is offline Offline
Newbie Poster

Re: count concurrency words form a file

 
0
  #5
Aug 30th, 2009
double weird

404 error pages...
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 258
Reputation: GeekByChoiCe is on a distinguished road 
Solved Threads: 50
GeekByChoiCe GeekByChoiCe is offline Offline
Posting Whiz in Training

Re: count concurrency words form a file

 
0
  #6
Aug 30th, 2009
url's fixed
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 11
Reputation: doomfrawen is an unknown quantity at this point 
Solved Threads: 0
doomfrawen doomfrawen is offline Offline
Newbie Poster

Re: count concurrency words form a file

 
0
  #7
Aug 30th, 2009
wow thx for the urls i really looked before but nothing of this appeared i'll try some examps here, thanks so much 4 ur help.

i'll leave open the thread until i can find the solution

thx again
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 11
Reputation: doomfrawen is an unknown quantity at this point 
Solved Threads: 0
doomfrawen doomfrawen is offline Offline
Newbie Poster

Re: count concurrency words form a file

 
0
  #8
Aug 30th, 2009
hi again, I find a code thx to GeekByChoiCe that maybe could help me this is the url. I modified it alittle bit, but now I can't get the correct output, this is the code:

  1. Private Sub TabPage2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabPage2.VisibleChanged
  2. 'wtf como uso esto
  3. 'rtbString.Replace(".", "")
  4. 'txtString.Replace(".", "")
  5.  
  6. Dim sentence As String = RichTextBox1.Text
  7.  
  8. Dim words() As String = System.Text.RegularExpressions.Regex.Split(sentence, "\W+")
  9.  
  10. Dim dict As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)
  11.  
  12. For Each word In words
  13.  
  14. Dim cnt As Integer
  15.  
  16. If (dict.TryGetValue(word, cnt)) Then
  17.  
  18. dict(word) = cnt + 1
  19.  
  20. Else
  21.  
  22. dict.Add(word, 1)
  23.  
  24. End If
  25.  
  26. Next
  27. For Each key In dict
  28. 'this doesn't work to sort
  29. 'dict.Sort()
  30. 'this doesn't work too, to print the output
  31. 'RichTextBox2.Text = dict.toarray
  32. 'original code to print the output
  33. Console.WriteLine("Word: {0} Count:{1}", key.Key, key.Value)
  34.  
  35.  
  36. Next
  37.  
  38. End Sub

if anyone could help with this I'll really aprecciate it

Thanks in advance
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 258
Reputation: GeekByChoiCe is on a distinguished road 
Solved Threads: 50
GeekByChoiCe GeekByChoiCe is offline Offline
Posting Whiz in Training

Re: count concurrency words form a file

 
0
  #9
Aug 30th, 2009
well comment the line
Console.WriteLine("Word: {0} Count:{1}", key.Key, key.Value)
and add the lines
RichTextBox2.AppendText(key.Key & " - " & key.Value & vbNewLine)
RichTextBox2.ScrollToCaret()
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 11
Reputation: doomfrawen is an unknown quantity at this point 
Solved Threads: 0
doomfrawen doomfrawen is offline Offline
Newbie Poster

Re: count concurrency words form a file

 
0
  #10
Aug 31st, 2009
oh thank you so much actually I used the same sentence but instead of RichTextBox2.AppendText i used richtextbox2.text

You helped me alot thanks again

I still have a lot to learn

Have a good weekend XD

**Marked as solved + reputation
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC