serendipity
serendipity

Reputation: 862

Check if a word in a string is contained in a set

I have a set called namePrefixSuffixSet which has possible prefix and suffix information. E.g Jr, Mr., Mrs., Attorney General etc.

I am extracting proper nouns from a sentence and checking them against this set to see if the proper noun contains a prefix or a suffix. If it does then I classify this proper noun as a person. e.g John Briggs Jr

I'm providing a snippet of code on how I am doing this. I am unable to get a match.

if (namePrefixSuffixList.contains(entry.getKey().toLowerCase())){
                    Set<String> roleStrings = roleStringsMap.containsKey("PERSON") 
                        ? roleStringsMap.get("PERSON") : new HashSet<String>();
                        roleStrings.add(entry.getKey());
                        roleStringsMap.put(SemanticRole.PERSON, roleStrings);
                        continue;
          }

In the code above entry.getKey() = John Briggs Jr

What am I doing wrong? Please let me know if you need more info.

Upvotes: 1

Views: 681

Answers (1)

user6073886
user6073886

Reputation:

The problem is you are calling the contains method of your list which will only return true if one of the entries is an exact match (So in your example if namePrefixSuffixList contained "John Briggs Jr").

You could however loop through your List and do a contains check on the Strings themself, which will also return true for partial matches ("John Briggs Jr".contains("Jr") will return true for example):

boolean prefixSuffixFound = false
for ( String prefixSuffix : namePrefixSuffixList ) {
     if(entry.getKey().toLowerCase().contains(prefixSuffix) {
          prefixSuffixFound = true;
          break;
     }
}

Upvotes: 1

Related Questions