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.
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.
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
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.
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,
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.
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
Thanks Jan. I got your point. Thanks for your help.
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