I need to match a keyword (which is in its lexical form) with a text and return whether the keyword matches (just like a search engine)
for example
text: I want some apples
keyword: apple
result: matched
keyword: banana
result: not matched
This is my method, I always get null for the tag
- (BOOL)matchKeywordWithText:(NSString *)text keyword:(NSString *)keyword {
__block NSMutableArray *words = [NSMutableArray array];
NSLinguisticTaggerOptions options = NSLinguisticTaggerOmitWhitespace | NSLinguisticTaggerOmitPunctuation | NSLinguisticTaggerJoinNames;
NSLinguisticTagger *tagger = [[NSLinguisticTagger alloc] initWithTagSchemes: [NSLinguisticTagger availableTagSchemesForLanguage:@"en"] options:options];
tagger.string = text;
[tagger enumerateTagsInRange:NSMakeRange(0, [text length]) scheme:NSLinguisticTagSchemeLemma options:options usingBlock:^(NSString *tag, NSRange tokenRange, NSRange sentenceRange, BOOL *stop) {
NSString *token = [text substringWithRange:tokenRange];
//TODO: TAG IS ALWAYS NULL HERE
NSLog(@"words are %@: %@", token, tag);
if (tag.length > 0) {
[words addObject:tag];
}
}];
for (NSString *word in words) {
if ([word isEqualToString:keyword]) {
return YES;
}
}
return NO;
}