-
Apr 27th, 2012, 09:58 AM
#1
Thread Starter
Junior Member
Applying a Rule Set to different Lines of Text
Hello,
The general purpose of my application simply reads a line of text for example:
1. I have a printer issue
2. I have a software issue
I have a rule set as follows:
Code:
'Define the class 'Printer' with 14 values as a string...
Dim Printer(13) As String
'The string 'Printer' gets assigned a value, this is repeated another 13 times...
Printer(0) = "Printing"
Printer(1) = "printing"
Printer(2) = "Print"
Printer(3) = "print"
Printer(4) = "Printer"
Printer(5) = "printer"
Printer(6) = "Output"
Printer(7) = "output"
Printer(8) = "Toner"
Printer(9) = "toner"
Printer(10) = "Paper"
Printer(11) = "paper"
Printer(12) = "Jam"
Printer(13) = "jam"
'Define the class 'Software' with 14 values as a string...
Dim Software(13) As String
'The string 'Software' gets assigned a value, this is repeated another 13 times...
Software(0) = "Software"
Software(1) = "software"
Software(2) = "Loading"
Software(3) = "loading"
Software(4) = "Crash"
Software(5) = "crash"
Software(6) = "Crashed"
Software(7) = "crashed"
Software(8) = "Require"
Software(9) = "require"
Software(10) = "Install"
Software(11) = "install"
Software(12) = "Installing"
Software(13) = "installing"
'Define the class 'Telephone' with 14 values as a string...
Dim Telephone(13) As String
'The string 'Telephone' gets assigned a value, this is repeated another 13 times...
Telephone(0) = "Telephone"
Telephone(1) = "telephone"
Telephone(2) = "Phone"
Telephone(3) = "phone"
Telephone(4) = "Ringing"
Telephone(5) = "ringing"
Telephone(6) = "Dial"
Telephone(7) = "dial"
Telephone(8) = "Call"
Telephone(9) = "call"
Telephone(10) = "Handset"
Telephone(11) = "handset"
Telephone(12) = "Voicemail"
Telephone(13) = "voicemail"
'Define the class 'Password' with 14 values as a string...
Dim Password(13) As String
'The string 'Password' gets assigned a value, this is repeated another 13 times...
Password(0) = "Password"
Password(1) = "password"
Password(2) = "Incorrect"
Password(3) = "incorrect"
Password(4) = "Username"
Password(5) = "username"
Password(6) = "log"
Password(7) = "logging"
Password(8) = "Message"
Password(9) = "message"
Password(10) = "Forgot"
Password(11) = "forgot"
Password(12) = "Forgotten"
Password(13) = "forgotten"
'Defines i as representing one of the numbers from 0-13...
Dim i As Integer = 0
For i = 0 To 13
'Rule set applies here, the order of the code DOES matter, because the code is applied to the string from top to bottom, hence jam gets searched for before toner
If InputArea.Text.Contains(Printer(13)) Then
ClassTextBox.Text = "Printer - Jam"
ElseIf InputArea.Text.Contains(Printer(9)) Then
ClassTextBox.Text = "Printer - Toner"
ElseIf InputArea.Text.Contains(Printer(i)) Then
ClassTextBox.Text = "Printer"
'Rule set applies here, the order of the code DOES matter, because the code is applied to the string from top to bottom, hence Failure gets searched for before Install
ElseIf InputArea.Text.Contains(Software(7)) Then
ClassTextBox.Text = "Software - Failure"
ElseIf InputArea.Text.Contains(Software(11)) Then
ClassTextBox.Text = "Software - Install"
ElseIf InputArea.Text.Contains(Software(13)) Then
ClassTextBox.Text = "Software - Install"
ElseIf InputArea.Text.Contains(Software(i)) Then
ClassTextBox.Text = "Software"
'Rule set applies here, the order of the code DOES matter, because the code is applied to the string from top to bottom, hence Voicemail gets searched for before Handset
ElseIf InputArea.Text.Contains(Telephone(13)) Then
ClassTextBox.Text = "Telephone - Voicemail"
ElseIf InputArea.Text.Contains(Telephone(11)) Then
ClassTextBox.Text = "Telephone - Handset"
ElseIf InputArea.Text.Contains(Telephone(i)) Then
ClassTextBox.Text = "Telephone"
'Rule set applies here, the order of the code DOES matter, because the code is applied to the string from top to bottom, hence Forgotten gets searched for before Logon
ElseIf InputArea.Text.Contains(Password(11)) Then
ClassTextBox.Text = "Password - Forgotten"
ElseIf InputArea.Text.Contains(Password(6)) Then
ClassTextBox.Text = "Password - Logon"
ElseIf InputArea.Text.Contains(Password(i)) Then
ClassTextBox.Text = "Password"
End If
The code simply uses a number of keywords to detect if the issue is a 'printer', 'software', 'telephone',. or 'password' problem.
I want to apply this rule set to 10 issues within the same rich text box, with the results sent to a text file.
The exporting to a text file I can do, however the main issue is, i cannot read the lines individually.
Can anyone help?
-
Apr 27th, 2012, 10:26 AM
#2
Frenzied Member
Re: Applying a Rule Set to different Lines of Text
vb Code:
InputArea.Text.IndexOf("String", Sytem.StringComparison.CurrentCultureIgnoreCase)
Might work better for you, so you don't have to have so many variations of the word stored.
That way if the use typed "PrInTeR", you could still catch it.
If you use a richtextbox, you can read the entire text, then split it at '/n/r' or whatever the return or linefeed character is. That's assuming they actually separate every sentence by a return.
Why not just let them choose a category and then type their description, then save your file named "[User]-[Tech-Ticket#xxxx]
Then have their details as the text in the file.
Justin
-
Apr 27th, 2012, 11:42 AM
#3
Re: Applying a Rule Set to different Lines of Text
Extension method to add overload of String.Contains that accepts a StringComparison.
vb.net Code:
Imports System.Runtime.CompilerServices
Public Module StringExtensions
<Extension()>
Public Function Contains(ByRef source As String, value As String, comparison As StringComparison) As Boolean
If value Is Nothing Then Return False
Return source.IndexOf(value, comparison) >= 0
End Function
End Module
Here's the start of refactoring the code for reusability. I've left some obvious things left to do. You can see that the property FindMatchingWords and methods GetErrorClass and GetKeywords have been abstracted for reusability.
vb.net Code:
Public Class Form1
Private ReadOnly Property FindMatchingWords() As Func(Of String, Boolean)
Get
Return Function(s) InputArea.Text.Contains(s, StringComparison.CurrentCultureIgnoreCase)
End Get
End Property
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim Printer = {"printing", "print", "printer", "output", "toner", "paper", "jam"}
Dim Software = {"software", "loading", "crash", "crashed", "require", "install", "installing"}
Dim Telephone = {"telephone", "phone", "ringing", "dial", "call", "handset", "voicemail"}
Dim Password = {"password", "incorrect", "username", "log", "logging", "message", "forgot", "forgotten"}
InputArea.Text = "THE TONER IS OUT FOR MY PRINTER"
If GetErrorClass(Printer) Then PrinterErrors(Printer)
If GetErrorClass(Software) Then SoftwareErrors(Software)
'Left these as boolean checks
Dim isTelephone = GetErrorClass(Telephone)
Dim isPassword = GetErrorClass(Password)
End Sub
Private Function GetErrorClass(ByVal TheArray As String()) As Boolean
Return TheArray.Any(Function(s) InputArea.Text.Contains(s, StringComparison.CurrentCultureIgnoreCase))
End Function
Private Sub PrinterErrors(ByVal PrinterArray As String())
Const format As String = "Printer - {0}"
Dim keywords = GetKeywords(PrinterArray)
If keywords.Contains("jam") Then
ClassTextBox.Text = String.Format(format, "Jam")
ElseIf keywords.Contains("toner") Then
ClassTextBox.Text = String.Format(format, "Toner")
Else
ClassTextBox.Text = "Printer"
End If
End Sub
Private Sub SoftwareErrors(ByVal SoftwareArray As String())
Const format As String = "Software - {0}"
Dim keywords = GetKeywords(SoftwareArray)
'Conditional checks for Software
End Sub
Private Function GetKeywords(ByVal TheArray As String()) As String()
Return TheArray.Where(FindMatchingWords).ToArray()
End Function
End Class
There is also the question of problems that span multiple arrays.
What do you do for this entry: "I forgot the password to my voicemail"?
This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.
The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.
-
Apr 30th, 2012, 07:42 AM
#4
Thread Starter
Junior Member
Re: Applying a Rule Set to different Lines of Text
thank you for your replies, very helpful, when trying to use your code you gave me MattP, the line:
Code:
StringComparison.CurrentCultureIgnoreCase)
there is a blue line error under this code, do you know how I could fix this?
-
Apr 30th, 2012, 07:45 AM
#5
Thread Starter
Junior Member
Re: Applying a Rule Set to different Lines of Text
Also MonkOfox, when you say I could split the lines with a full stop at the end to show that this is where I want to stop reaidng then move onto the enxt line to then read that, how would this be done?
-
Apr 30th, 2012, 08:33 AM
#6
Re: Applying a Rule Set to different Lines of Text
You'll need to add a module to your project and put the first code snippet that I provided in it.
This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.
The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|