美文网首页iOS
iOS - 自然语言NSLinguisticTagger

iOS - 自然语言NSLinguisticTagger

作者: Mitchell | 来源:发表于2015-10-23 11:31 被阅读856次
    • 自然语言筛选代码及注释:
        //1.创建语句
        NSString * question = @"What is the weather in San Francisco?";
        //2.创建筛选条件
        NSLinguisticTaggerOptions options = NSLinguisticTaggerOmitWhitespace | NSLinguisticTaggerJoinNames |NSLinguisticTaggerOmitPunctuation;
        //3.创建自然语言标签
        NSLinguisticTagger * tagger = [[NSLinguisticTagger alloc]initWithTagSchemes:[NSLinguisticTagger availableTagSchemesForLanguage:@"en"] options:options];
        //4.给标签附字符串
        tagger.string = question;
        //5.执行筛选
        [tagger enumerateTagsInRange:NSMakeRange(0, question.length) scheme:NSLinguisticTagSchemeNameTypeOrLexicalClass  options:options usingBlock:^(NSString * _Nonnull tag, NSRange tokenRange, NSRange sentenceRange, BOOL * _Nonnull stop) {
           
            //6.获取结果
            NSString*token = [question substringWithRange:tokenRange];
    
            //7.打印结果
            NSLog(@"%@:%@",token,tag);
        }];
    
    • 打印结果
        /**
         *  打印结果
         2015-10-23 10:07:04.599 NSLinguisticTaggerDemo[34466:941126] What:Pronoun
         2015-10-23 10:07:04.600 NSLinguisticTaggerDemo[34466:941126] is:Verb
         2015-10-23 10:07:04.600 NSLinguisticTaggerDemo[34466:941126] the:Determiner
         2015-10-23 10:07:04.600 NSLinguisticTaggerDemo[34466:941126] weather:Noun
         2015-10-23 10:07:04.600 NSLinguisticTaggerDemo[34466:941126] in:Preposition
         2015-10-23 10:07:04.600 NSLinguisticTaggerDemo[34466:941126] San Francisco:PlaceName
         */
    

    相关文章

      网友评论

        本文标题:iOS - 自然语言NSLinguisticTagger

        本文链接:https://www.haomeiwen.com/subject/femthttx.html