CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2017

    Pattern matching psuedocode for multiple patterns

    I am trying to write a psuedocode for finding five words in a list from a text pattern that whether they exist in the text or not and if they do write their count and occurrences. I have written a psuedocode based on na´ve pattern search algo but the problem is it only finds single pattern in a string not a list of words. how can I do that?

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Hanover Germany

    Re: Pattern matching psuedocode for multiple patterns

    Could you show your pseudocode together with the text pattern so we could see how it works with a single pattern in a string but not with a list of words?
    Victor Nijegorodov

  3. #3
    Join Date
    Nov 2017

    Re: Pattern matching psuedocode for multiple patterns

    Option 2: Word Search Algorithm

    Initialize a List of five patterns (3-6 max characters) to be matched As string:
    List= {p1, p2, p3, p4, p5}
    P1= “red”
    P2= “blue”
    P3= “white”
    P4= “grey”
    P5= “green”
    Initialize a Long Text string (30 max characters) from which patterns to be matched As string:
    T =“vuvyefdarednnjpinkvhsgree”

    BEGIN WordSearch (T, list)
    Set n to length of T ​ //Length of Long Text
    Set s to shift_index //When mismatch occurs, shift moves position right
    Select first pattern p1 from list
    While (list is not full)
    ​Set m to length of pattern P
    For s= 0 to n-m //Iterate n-m number of times
    ​​​​//Initialize a counter up to length of pattern m
    ​​​Set counter to 0
    ​ ​​//Initialize an occurrence of each pattern for total number of times
    ​​​Set occurrence to 0
    ​​​For i = 0 to m //iterate to the length of the pattern word
    //match corresponding characters of two strings
    If (P [i] == T [s+i]) then
    ​​​​increment counter
    ​​​​end if
    ​​​If counter == m then //All characters of a pattern word are matched
    Print “String contains the pattern at location” + s
    increment occurrence
    end if
    ​​next s
    Print “No of times pattern occurs” + occurrence
    move to next pattern
    ​end while

  4. #4
    Join Date
    Nov 2017

    Re: Pattern matching psuedocode for multiple patterns

    Thats what i have made, does it make sense?

  5. #5
    Join Date
    Feb 2017

    Re: Pattern matching psuedocode for multiple patterns

    Quote Originally Posted by zayneb18 View Post
    Thats what i have made, does it make sense?
    It makes sense and I've spotted a bug already . Under certain conditions you will get an out-of-bounds exception because you access T beyond the end. For example it will happen when T ends with "...re" and you check for the 'd' in "red". It's here: If (P [i] == T [s+i]) then.

    You run through T once for each pattern. That's fine but it's more conventional to run through T just once and check for matches with all patterns in one go. The reason is that then the matching can be optimized so the total algorithm becomes faster. For example "grey" and "green" share the first three letters so they could be "co-matched" saving time.

    Regarding the use of pseudo-code. Personally I prefer to use real code together with a development method called step-wise refinement. It's like writing pseudo-code with real code. You start with a general outline which you then refine (make more detailed) in small steps. And most importantly, you make sure everything works after each refining step so the program is working during the whole of development from start to finish. One can argue that pseudo-code is more general than any real programming language but that's just a chimera because pseudo-code is also a language only that it's not formally defined and there are no compilers so it cannot be run and tested.
    Last edited by wolle; November 27th, 2017 at 01:08 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)