Results 1 to 6 of 6

Thread: Applying a Rule Set to different Lines of Text

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2012
    Posts
    26

    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?

  2. #2
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    Re: Applying a Rule Set to different Lines of Text

    vb Code:
    1. 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
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  3. #3
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    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:
    1. Imports System.Runtime.CompilerServices
    2.  
    3. Public Module StringExtensions
    4.  
    5.     <Extension()>
    6.     Public Function Contains(ByRef source As String, value As String, comparison As StringComparison) As Boolean
    7.         If value Is Nothing Then Return False
    8.         Return source.IndexOf(value, comparison) >= 0
    9.     End Function
    10.  
    11. 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:
    1. Public Class Form1
    2.  
    3.     Private ReadOnly Property FindMatchingWords() As Func(Of String, Boolean)
    4.         Get
    5.             Return Function(s) InputArea.Text.Contains(s, StringComparison.CurrentCultureIgnoreCase)
    6.         End Get
    7.     End Property
    8.  
    9.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    10.         Dim Printer = {"printing", "print", "printer", "output", "toner", "paper", "jam"}
    11.         Dim Software = {"software", "loading", "crash", "crashed", "require", "install", "installing"}
    12.         Dim Telephone = {"telephone", "phone", "ringing", "dial", "call", "handset", "voicemail"}
    13.         Dim Password = {"password", "incorrect", "username", "log", "logging", "message", "forgot", "forgotten"}
    14.  
    15.         InputArea.Text = "THE TONER IS OUT FOR MY PRINTER"
    16.  
    17.         If GetErrorClass(Printer) Then PrinterErrors(Printer)
    18.         If GetErrorClass(Software) Then SoftwareErrors(Software)
    19.  
    20.         'Left these as boolean checks
    21.         Dim isTelephone = GetErrorClass(Telephone)
    22.         Dim isPassword = GetErrorClass(Password)
    23.     End Sub
    24.  
    25.     Private Function GetErrorClass(ByVal TheArray As String()) As Boolean
    26.         Return TheArray.Any(Function(s) InputArea.Text.Contains(s, StringComparison.CurrentCultureIgnoreCase))
    27.     End Function
    28.  
    29.     Private Sub PrinterErrors(ByVal PrinterArray As String())
    30.         Const format As String = "Printer - {0}"
    31.         Dim keywords = GetKeywords(PrinterArray)
    32.         If keywords.Contains("jam") Then
    33.             ClassTextBox.Text = String.Format(format, "Jam")
    34.         ElseIf keywords.Contains("toner") Then
    35.             ClassTextBox.Text = String.Format(format, "Toner")
    36.         Else
    37.             ClassTextBox.Text = "Printer"
    38.         End If
    39.     End Sub
    40.  
    41.     Private Sub SoftwareErrors(ByVal SoftwareArray As String())
    42.         Const format As String = "Software - {0}"
    43.         Dim keywords = GetKeywords(SoftwareArray)
    44.         'Conditional checks for Software
    45.     End Sub
    46.  
    47.     Private Function GetKeywords(ByVal TheArray As String()) As String()
    48.         Return TheArray.Where(FindMatchingWords).ToArray()
    49.     End Function
    50. 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.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Feb 2012
    Posts
    26

    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?

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 2012
    Posts
    26

    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?

  6. #6
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    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
  •  



Click Here to Expand Forum to Full Width