Jump to content
Sign in to follow this  
JRowe

Array problems

Recommended Posts

JRowe

So I've got a pattern and a test case, and I'd like to build a function that tests the pattern, and returns the wildcard elements as an array if there is a match.

Here's what I want to do:

;Break input into words array

;break pattern into words array

;locate and index stars in pattern

;make an array of word groups in the pattern

;Make an array of word groups in the input

;Compare the two

;Success! if all the word groups are equal

;Failure if they aren't.

I want to get rid of the *s in the $patternGroups and $inputGroups

CODE

#include <GUIEdit.au3>

;Break input into words array

$inputString = "a Dog is a Mammal is a Animal is a bob is a Thing and another Thing and so on, and so forth. Woot."

$inputTest = StringRegExp($inputString, "[\w]+", 3)

;break pattern into words array

$patternString = "a * is a * is a * is a bob is a * and another * and so on, and so forth. *."

$pattern = StringRegExp($patternString , "[*\w]+", 3)

;locate and index stars in pattern

Dim $starIndex[uBound($pattern)]

$y = 0

$z = 0

While $z < UBound($pattern)

If $pattern[$z] = "*" Then

ReDim $starIndex[$y+1]

$starIndex[$y] = $z

$y+=1

EndIf

$z+=1

WEnd

;make an array of word groups in the pattern

Dim $patternGroups[uBound($starIndex)+1]

$y = 0

$z = 0

$x = 0

While $z < UBound($starIndex)

$x = $starIndex[$z]

While $y < $x

$patternGroups[$z] = $patternGroups[$z] & " " & $pattern[$y]

$y+=1

WEnd

$z+=1

WEnd

;Make an array of word groups in the input

Dim $inputGroups[uBound($starIndex)+1]

$y = 0

$z = 0

$x = 0

While $z < UBound($starIndex)

$x = $starIndex[$z]

While $y < $x

$inputGroups[$z] = $inputGroups[$z] & " " & $inputTest[$y]

$y+=1

WEnd

$z+=1

WEnd

;Compare the two

_ArrayDisplay($patternGroups)

_ArrayDisplay($inputGroups)

;Success! if all the word groups are equal

;Failure if they aren't.

I've almost got the function working, but I can't figure out how to drop the first word of each group, and I can't retrieve the last word groups for either of the input or pattern arrays.

Share this post


Link to post
Share on other sites
weaponx

I'm not really sure what you're after. I came up with this:

#include <GUIEdit.au3>
#include <Array.au3>

;Break input into words array
$inputString = "a Dog is a Mammal is a Animal is a bob is a Thing and another Thing and so on, and so forth. Woot."
$inputTest = StringRegExp($inputString, "[\w]+", 3)

;break pattern into words array
$patternString = "a * is a * is a * is a bob is a * and another * and so on, and so forth. *."
$pattern = StringRegExp($patternString , "[*\w]+", 3)

;Dump input to console
ConsoleWrite("Input" & @CRLF)
For $X = 0 to Ubound($inputTest)-1
    ConsoleWrite("["&$X&"]: " & $inputTest[$X] & @CRLF)
Next
ConsoleWrite(@CRLF)

;Dump pattern to console
ConsoleWrite("Pattern" & @CRLF)
For $X = 0 to Ubound($pattern)-1
    ConsoleWrite("["&$X&"]: " & $pattern[$X] & @CRLF)
Next
ConsoleWrite(@CRLF)

;Dump matches to console
ConsoleWrite("Matches" & @CRLF)
For $X = 0 to Ubound($pattern)-1
    If $pattern[$X] = "*" Then
        ConsoleWrite("["&$X&"]: " & $inputTest[$X] & @CRLF)
    EndIf
Next

Output:

Input
[0]: a
[1]: Dog
[2]: is
[3]: a
[4]: Mammal
[5]: is
[6]: a
[7]: Animal
[8]: is
[9]: a
[10]: bob
[11]: is
[12]: a
[13]: Thing
[14]: and
[15]: another
[16]: Thing
[17]: and
[18]: so
[19]: on
[20]: and
[21]: so
[22]: forth
[23]: Woot

Pattern
[0]: a
[1]: *
[2]: is
[3]: a
[4]: *
[5]: is
[6]: a
[7]: *
[8]: is
[9]: a
[10]: bob
[11]: is
[12]: a
[13]: *
[14]: and
[15]: another
[16]: *
[17]: and
[18]: so
[19]: on
[20]: and
[21]: so
[22]: forth
[23]: *

Matches
[1]: Dog
[4]: Mammal
[7]: Animal
[13]: Thing
[16]: Thing
[23]: Woot

Share this post


Link to post
Share on other sites
JRowe

I'm after getting wildcards to work with the AIML parser script. almost there :)

Testing on other random cases, and I'll have to make it into a single function, and throw in the comparison of non wildcard elements.

Thanks!

If they're different, then the pattern isn't valid, and the result would be a mismatched response. I only want to return a set of matches if the non-wildcard words are identical.

Edited by Jrowe

Share this post


Link to post
Share on other sites
weaponx

Even simpler:

;Break input into words array
$inputString = "a Dog is a Mammal is a Animal is a bob is a Thing and another Thing and so on, and so forth. Woot."

$patternString = "a * is a * is a * is a bob is a * and another * and so on, and so forth. *."

$format_pattern = StringReplace($patternString, ".", "\.")
$format_pattern = StringReplace($format_pattern, "*", "(.*?)")

$simple = StringRegExp($inputString, $format_pattern, 3)
For $X = 0 to Ubound($simple)-1
    ConsoleWrite("["&$X&"]: " & $simple[$X] & @CRLF)
Next

Share this post


Link to post
Share on other sites
JRowe

Oh, wow. That's awesome... now it can even accept multiple word wildcards.

;Break input into words array
$inputString = "Generic AIML Test going on Here"

$patternString = "Generic * Test * Here"

$format_pattern = StringReplace($patternString, ".", "\.")
$format_pattern = StringReplace($format_pattern, "*", "(.*?)")

$simple = StringRegExp($inputString, $format_pattern, 3)
For $X = 0 to Ubound($simple)-1
    ConsoleWrite("["&$X&"]: " & $simple[$X] & @CRLF)
Next

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.