Count the occurrence of word in text/String

Posted by navjot on 13-Feb-2019 10:39

Is there any way to count the occurrence of a word in a text field. For example : - if we have a string field value "My name is Navjot. Navjot works as a Senior Software Engineer." . we want to find the occurrence of "Navjot" word in string field.

Please let me know if there is any inbuild function in Corticon for doing this.

Posted by Thierry Ciot on 13-Feb-2019 13:09

You can use the code outlined here: stackoverflow.com/.../22566637

In particular:

String in = "i have a male cat. the color of male cat is Black";

int i = 0;

Pattern p = Pattern.compile("male cat");

Matcher m = p.matcher( in );

while (m.find()) {

   i++;

}

System.out.println(i); // Prints 2

But he point that Harold made is still valid: you can achieve your goal by creating an extended operator.

Let us know how that works for you.

Thierry.

Posted by Thierry Ciot on 13-Feb-2019 14:09

To count instances of strings, you can use the following as suggested here stackoverflow.com/.../22566637

String in = "i have a male cat. the color of male cat is Black";

int i = 0;

Pattern p = Pattern.compile("male cat");

Matcher m = p.matcher( in );

while (m.find()) {

   i++;

}

System.out.println(i); // Prints 2

Of course you would put this code in an extended operator as suggested earlier.

Thierry

All Replies

Posted by Harold-Jan Verlee on 13-Feb-2019 11:27

There is no specific STRING operator which does this today. However, it would be quit easy to create a custom Extended Operator (see documentation) . Java code which could be used to write this ExtOps would look like as follows (you would return the count in the function and your Entity.Attribute(string) would be the input):

// Java program to count the number of occurrence of a word in a given string
// 

import java.io.*;

class GFG {

static int countOccurences(String str, String word)
{
// split the string by spaces in a
String a[] = str.split(" ");

// search for pattern in a
int count = 0;
for (int i = 0; i < a.length; i++)
{
// if match found increase count
if (word.equals(a[i]))
count++;
}

return count;
}

See: Geeks.

Posted by navjot on 13-Feb-2019 11:57

Jan, Thanks for your suggestion.

Code that you provide will not the needful because it will split the string by " " and "Navjot." (with . dot) will not be equals to "Navjot" (without . dot). So count will come as 1 but required were 2.

Thanks,

Posted by Thierry Ciot on 13-Feb-2019 13:09

You can use the code outlined here: stackoverflow.com/.../22566637

In particular:

String in = "i have a male cat. the color of male cat is Black";

int i = 0;

Pattern p = Pattern.compile("male cat");

Matcher m = p.matcher( in );

while (m.find()) {

   i++;

}

System.out.println(i); // Prints 2

But he point that Harold made is still valid: you can achieve your goal by creating an extended operator.

Let us know how that works for you.

Thierry.

Posted by Harold-Jan Verlee on 13-Feb-2019 13:17

Well, alternatively if you don't want to program you can do it inside Corticon too. I still believe an ExtOp is way easier, but here is a [View:/cfs-file/__key/communityserver-discussions-components-files/14/Count-words-in-string.zip:320:240]

to achieve your objectives. 

Posted by Harold-Jan Verlee on 13-Feb-2019 13:20

Forget to mention: notice the Processing Mode (looping construct):

Posted by Thierry Ciot on 13-Feb-2019 14:09

To count instances of strings, you can use the following as suggested here stackoverflow.com/.../22566637

String in = "i have a male cat. the color of male cat is Black";

int i = 0;

Pattern p = Pattern.compile("male cat");

Matcher m = p.matcher( in );

while (m.find()) {

   i++;

}

System.out.println(i); // Prints 2

Of course you would put this code in an extended operator as suggested earlier.

Thierry

Posted by navjot on 13-Feb-2019 15:21

Thanks Jan. I got your point. Thanks for your help.

Posted by navjot on 13-Feb-2019 15:24

Hey Thierry,

I got your point. Logic that you have provided is correct just that if "male cat" comes in end of String then it will not work fine because it will not able to split the last element in string. so to made it work i added one blank space at the end :P and it is working fine.

Thanks for your help :)

Navjot

This thread is closed