iOS NSLinguisticTagger 在获取引理词干时总是返回 null
Posted
技术标签:
【中文标题】iOS NSLinguisticTagger 在获取引理词干时总是返回 null【英文标题】:iOS NSLinguisticTagger always return null when getting the lemma stem 【发布时间】:2014-05-16 16:13:39 【问题描述】:我需要将一个关键字(它的词法形式)与一个文本进行匹配,并返回关键字是否匹配(就像搜索引擎一样)
例如
text: I want some apples
keyword: apple
result: matched
keyword: banana
result: not matched
这是我的方法,标签总是为空
- (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;
【问题讨论】:
【参考方案1】:以下代码有效:
NSLinguisticTagger *tagger = [[NSLinguisticTagger alloc]
initWithTagSchemes:[NSArray arrayWithObjects:NSLinguisticTagSchemeNameTypeOrLexicalClass, NSLinguisticTagSchemeLemma, nil]
options:(NSLinguisticTaggerOmitWhitespace | NSLinguisticTaggerOmitPunctuation)];
NSString *text = @"i'm apples";
[tagger setString:text];
[tagger enumerateTagsInRange:NSMakeRange(0, [text length])
scheme:NSLinguisticTagSchemeNameTypeOrLexicalClass
options:(NSLinguisticTaggerOmitWhitespace | NSLinguisticTaggerOmitPunctuation)
usingBlock:^(NSString *tag, NSRange tokenRange, NSRange sentenceRange, BOOL *stop)
NSString *token = [text substringWithRange:tokenRange];
NSString *lemma = [tagger tagAtIndex:tokenRange.location scheme:NSLinguisticTagSchemeLemma tokenRange:NULL sentenceRange:NULL];
if (lemma == nil)
lemma = token;
NSLog(@"%@, %@", token, lemma);
];
【讨论】:
你能在这里看看我的问题吗?非常令人沮丧..***.com/questions/48768919/…以上是关于iOS NSLinguisticTagger 在获取引理词干时总是返回 null的主要内容,如果未能解决你的问题,请参考以下文章
我们如何在 iOS 中制作自定义的 NSLinguisticTagger tagSchemes?
iOS NSLinguisticTagger 在获取引理词干时总是返回 null
如何在 Mac OSX 上更正 NSLinguisticTagger 函数并出现错误“Type 'String' has no member 'tokenType'”(该函数适用于 iOS)?
NSLinguisticTagger enumerateTagsInRange 在具有 NSLinguisticTagSchemeNameTypeOrLexicalClass 的设备上不起作用