need to parse text. Any ideas on how to write the conversion routine?
-
Hey fellow coders. I'm writing a program in VB that is designed to take the question and answer file for IRC trivia bots and convert the file format over to be used with another type of trivia bot. As an example of what I need to do here is an example of text I need to parse I need to convert the line below: What band got their name from the sixties movie Barbarella?*Duran Duran To this line: Duran Duran|What band got their name from the sixties movie Barbarella? Now, this program opens a text file and copies the entire file to one string variable. That code looks like this: Public Class Form1 Inherits System.Windows.Forms.Form Dim currsep As String, newsep As String, lineoftext As String, filetext As String Private Sub mnu_file_open_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnu_file_open.Click OpenFileDialog.Filter = "Text Files (*.txt)|*.txt" OpenFileDialog.ShowDialog() If OpenFileDialog.FileName <> "" Then FileOpen(1, OpenFileDialog.FileName, OpenMode.Input) Do Until EOF(1) 'read file into RAM lineoftext = LineInput(1) filetext = filetext & lineoftext & vbCrLf Loop lbl_open.Text = "File is open and ready to be converted" FileClose(1) End If End Sub Private Sub cmd_conv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_conv.Click 'this procedure takes the file (in variable filetext) and performs the parsing. End Sub End Class any ideas for cmd_conv_click?
-
Hey fellow coders. I'm writing a program in VB that is designed to take the question and answer file for IRC trivia bots and convert the file format over to be used with another type of trivia bot. As an example of what I need to do here is an example of text I need to parse I need to convert the line below: What band got their name from the sixties movie Barbarella?*Duran Duran To this line: Duran Duran|What band got their name from the sixties movie Barbarella? Now, this program opens a text file and copies the entire file to one string variable. That code looks like this: Public Class Form1 Inherits System.Windows.Forms.Form Dim currsep As String, newsep As String, lineoftext As String, filetext As String Private Sub mnu_file_open_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnu_file_open.Click OpenFileDialog.Filter = "Text Files (*.txt)|*.txt" OpenFileDialog.ShowDialog() If OpenFileDialog.FileName <> "" Then FileOpen(1, OpenFileDialog.FileName, OpenMode.Input) Do Until EOF(1) 'read file into RAM lineoftext = LineInput(1) filetext = filetext & lineoftext & vbCrLf Loop lbl_open.Text = "File is open and ready to be converted" FileClose(1) End If End Sub Private Sub cmd_conv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_conv.Click 'this procedure takes the file (in variable filetext) and performs the parsing. End Sub End Class any ideas for cmd_conv_click?
Don't read the entire file into a string variable. That isn't necessary at all an makes you job that much harder. What you do is open 2 files. 1 is the file you want to convert, 2 is the file you want to convert to. You do the same thing you did in your sample code with the LineInput but you look for the '*' character and split the string into two strings. Then recombine the strings into the new format and write that into the second file.
Private Sub Convert() Dim SourceString As String Dim TargetString As String Dim Parsed() As String FileOpen(1, "C:\\Source.txt", OpenMode.Input) FileOpen(2, "C:\\Target.txt", OpenMode.Output) Do Until EOF(1) SourceString = LineInput(1) Parsed = SourceString.Split("\*") TargetString = Parsed(1) & "|" & Parsed(0) PrintLine(2, TargetString) Loop FileClose(1, 2) End Sub
RageInTheMachine9532
-
Don't read the entire file into a string variable. That isn't necessary at all an makes you job that much harder. What you do is open 2 files. 1 is the file you want to convert, 2 is the file you want to convert to. You do the same thing you did in your sample code with the LineInput but you look for the '*' character and split the string into two strings. Then recombine the strings into the new format and write that into the second file.
Private Sub Convert() Dim SourceString As String Dim TargetString As String Dim Parsed() As String FileOpen(1, "C:\\Source.txt", OpenMode.Input) FileOpen(2, "C:\\Target.txt", OpenMode.Output) Do Until EOF(1) SourceString = LineInput(1) Parsed = SourceString.Split("\*") TargetString = Parsed(1) & "|" & Parsed(0) PrintLine(2, TargetString) Loop FileClose(1, 2) End Sub
RageInTheMachine9532
That code works.....for the most part. The problem is if the question/answer set is not all on one line it will crash with a "Index was outside the bounds of the array". error. one condition that will cause this is having a question/answer run over onto another line. Here's the current code: Private Sub mnu_file_convert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnu_file_convert.Click currsep = txt_current.Text newsep = txt_desired.Text OpenFileDialog.Filter = "Text Files (*.txt)|*.txt" SaveFileDialog.Filter = "Text Files (*.txt)|*.txt" OpenFileDialog.ShowDialog() SaveFileDialog.ShowDialog() If OpenFileDialog.FileName <> "" And SaveFileDialog.FileName <> "" Then FileOpen(1, OpenFileDialog.FileName, OpenMode.Input) FileOpen(2, SaveFileDialog.FileName, OpenMode.Output) 'Stop Do Until EOF(1) SourceString = LineInput(1) Parsed = SourceString.Split(currsep) If lst_current.SelectedIndex = 1 And lst_desired.SelectedIndex = 0 Then TargetString = Parsed(1) & newsep & Parsed(0) PrintLine(2, TargetString) Else TargetString = Parsed(0) & newsep & Parsed(1) PrintLine(2, TargetString) End If Loop End If End Sub End Class
-
That code works.....for the most part. The problem is if the question/answer set is not all on one line it will crash with a "Index was outside the bounds of the array". error. one condition that will cause this is having a question/answer run over onto another line. Here's the current code: Private Sub mnu_file_convert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnu_file_convert.Click currsep = txt_current.Text newsep = txt_desired.Text OpenFileDialog.Filter = "Text Files (*.txt)|*.txt" SaveFileDialog.Filter = "Text Files (*.txt)|*.txt" OpenFileDialog.ShowDialog() SaveFileDialog.ShowDialog() If OpenFileDialog.FileName <> "" And SaveFileDialog.FileName <> "" Then FileOpen(1, OpenFileDialog.FileName, OpenMode.Input) FileOpen(2, SaveFileDialog.FileName, OpenMode.Output) 'Stop Do Until EOF(1) SourceString = LineInput(1) Parsed = SourceString.Split(currsep) If lst_current.SelectedIndex = 1 And lst_desired.SelectedIndex = 0 Then TargetString = Parsed(1) & newsep & Parsed(0) PrintLine(2, TargetString) Else TargetString = Parsed(0) & newsep & Parsed(1) PrintLine(2, TargetString) End If Loop End If End Sub End Class
OK. Then when you parse the string (TargetString=...). Check to see if the array that is returned has 2 elements in it. If not, then you have to go back and read the next line and append it to the one you already have, then parse it again and it should work. Thing is, this will only work if you have 1 question and 1 answer per line or two. If the second line contains the rest of a question, then and answer, then starts another question, you'll run into problems. I know '*' comes between a question and its answer, but is there a second seperator that appears between answer followed by a question? RageInTheMachine9532