DXL - How to find and count a given text in a string?

Greetings,

 

I'm a newbie at dxl programming.

I'm trying to prepare a script with the following tasks:

1.- Extract text for some columns in order to define a string for each object.

2.- Define a second string I'm looking for inside that columns text.

3.- Count how many times the second string has been found.

 

I have complete the 2 first tasks:

int n=0

Column c

Object o

Module m

Buffer b = create

 for o in document m do {

b = text(column 0, o) text(column 1,o) text(column 2,o)

string s = stringOf b

string sub ="XXX"

if (findPlainText(s, sub, offset, len, true)) { n += 1 }

 }

print n

With this I get text from columns 0, 1 and 2, put together in a same string. After that I define the text I'm looking for "XXX". And entire n gives me how many times it is found.

However this script doesn't work properly becuase once the text is found first time in a given object, it continues with the following object. So the text it is only found once for each object. However whether the same text "XXX" it is include many times at the same string s I can't count it (for example, whether s="XXX and XXX but XXX", this script counts 1 instead 3.

Could somebody helps me, correcting the script?

Thank you in advance for your help!!


jmkinki - Wed May 21 10:12:23 EDT 2014

Re: DXL - How to find and count a given text in a string?
Tony_Goodman - Wed May 21 11:18:57 EDT 2014

Check out the fourth post in https://www.ibm.com/developerworks/community/forums/html/topic?id=77777777-0000-0000-0000-000014695315&ps=25

Tony

Re: DXL - How to find and count a given text in a string?
llandale - Wed May 21 16:13:18 EDT 2014

I didn't read Goodman's link but I'm sure its pretty good.  No doubt you need to put findPlainText in a loop; or use some other searcher.

There are some nuances to consider:

  • How many "XXX"s are there in "XXXX"?  1, or 2?
  • How many "SoS"s are there in "SoSoS"?  1, or 2?

In your findPlanText loop, generally I think you want to increment your "offset"

  • +1, if the answers above are "2""
  • +len, if the answers above are "1".

-Louie

 

Re: DXL - How to find and count a given text in a string?
jmkinki - Thu May 22 06:40:34 EDT 2014

Tony_Goodman - Wed May 21 11:18:57 EDT 2014

Check out the fourth post in https://www.ibm.com/developerworks/community/forums/html/topic?id=77777777-0000-0000-0000-000014695315&ps=25

Tony

Thank you Tony.

 

I have tried with this script. It looks like it works fine. However it doesn't work in a bucle.

I get the numbers of shall for each object in the module, but not the total number of shall in the module.

Any idea to get it?? I have seen the other posts in the same link but that code doesn't work. Some error messages are shown.

I have used this code:

Object o
Module m = current
Column c

for o in document m do {
Regexp shall = regexp2 "[^\"a-zA-z]([Ss]hall)[^\"a-zA-z]"

string txt = text(column 0,o) text(column 1,o) text(column 2,o)

int count = 0
while (!null txt && shall txt) {
txt = txt[end 1 + 1:]
count++
}

print count "\n" //=4

}

 

Re: DXL - How to find and count a given text in a string?
jmkinki - Thu May 22 06:41:59 EDT 2014

llandale - Wed May 21 16:13:18 EDT 2014

I didn't read Goodman's link but I'm sure its pretty good.  No doubt you need to put findPlainText in a loop; or use some other searcher.

There are some nuances to consider:

  • How many "XXX"s are there in "XXXX"?  1, or 2?
  • How many "SoS"s are there in "SoSoS"?  1, or 2?

In your findPlanText loop, generally I think you want to increment your "offset"

  • +1, if the answers above are "2""
  • +len, if the answers above are "1".

-Louie

 

Thank you for your answer. Goodman links works for a single string but as I have to look for a complete module...

Re: DXL - How to find and count a given text in a string?
Tony_Goodman - Thu May 22 07:13:15 EDT 2014

jmkinki - Thu May 22 06:41:59 EDT 2014

Thank you for your answer. Goodman links works for a single string but as I have to look for a complete module...

Try this

 

Module m = current
Object o = null
int from = 0
int count = 0
int totalCount = 0

Buffer b = create

Regexp re = regexp2"([Ss][Hh][Aa][Ll][Ll])"

for o in m do
{
 b = o."Object Text" ""
 
 from = 0
 count = 0
 
 while (search(re, b, from))
 {
  count++
  totalCount++
  from += end 0 + 1
 }
 
 if (count != 0)
 {
  print(count " shall in " identifier(o) "\n")
 }
}

print(totalCount " shall in " name(m) "\n")

 

Re: DXL - How to find and count a given text in a string?
jmkinki - Thu May 22 08:43:07 EDT 2014

Tony_Goodman - Thu May 22 07:13:15 EDT 2014

Try this

 

Module m = current
Object o = null
int from = 0
int count = 0
int totalCount = 0

Buffer b = create

Regexp re = regexp2"([Ss][Hh][Aa][Ll][Ll])"

for o in m do
{
 b = o."Object Text" ""
 
 from = 0
 count = 0
 
 while (search(re, b, from))
 {
  count++
  totalCount++
  from += end 0 + 1
 }
 
 if (count != 0)
 {
  print(count " shall in " identifier(o) "\n")
 }
}

print(totalCount " shall in " name(m) "\n")

 

Thank you Tony!!

This code works fine!!

Thank you for your help!!