【问题标题】:NSLinguisticTagger: Filter out Specified Token Depending on Tag TypeNSLinguisticTagger:根据标签类型过滤掉指定的标记
【发布时间】:2019-04-17 04:12:30
【问题描述】:

我正在尝试根据标记过滤掉特定的标记。当我运行我的代码时,我得到这个作为输出。我只想检索形容词并将其输出。有没有简单的方法可以做到这一点?

Hello: NSLinguisticTag(_rawValue: Interjection)
World: NSLinguisticTag(_rawValue: Noun)
this: NSLinguisticTag(_rawValue: Determiner)
is: NSLinguisticTag(_rawValue: Verb)
my: NSLinguisticTag(_rawValue: Determiner)
main: NSLinguisticTag(_rawValue: Adjective)
goal: NSLinguisticTag(_rawValue: Noun)

tokenizeText(inputtedText: "Hello World,这是我的主要目标,用这些词找出形容词、动词和名词")

【问题讨论】:

    标签: ios swift macos swift4 nslinguistictagger


    【解决方案1】:

    您可以在enumerateTags 闭包中简单地检查tag 是否属于.adjective 类型,如果是则继续:

    let sentence = "The yellow cat hunts the little gray mouse around the block"
    let options: NSLinguisticTagger.Options = [.omitWhitespace, .omitPunctuation, .joinNames]
    let schemes = NSLinguisticTagger.availableTagSchemes(forLanguage: "en")
    let tagger = NSLinguisticTagger(tagSchemes: schemes, options: Int(options.rawValue))
    tagger.string = sentence
    tagger.enumerateTags(in: NSRange(location: 0, length: sentence.count), scheme: .nameTypeOrLexicalClass, options: options) { (tag, tokenRange, _, _) in
        guard tag == .adjective, let adjectiveRange = Range(tokenRange, in: sentence) else { return }
        let adjectiveToken = sentence[adjectiveRange]
        print(adjectiveToken)
    }
    

    打印出来:

    黄色

    灰色

    编辑

    如果您想要多个标记类型的标记,您可以将标记存储在字典中,并将标记作为键:

    let sentence = "The yellow cat hunts the little gray mouse around the block"
    let options: NSLinguisticTagger.Options = [.omitWhitespace, .omitPunctuation, .joinNames]
    let schemes = NSLinguisticTagger.availableTagSchemes(forLanguage: "en")
    let tagger = NSLinguisticTagger(tagSchemes: schemes, options: Int(options.rawValue))
    tagger.string = sentence
    var tokens: [NSLinguisticTag: [String]] = [:]
    tagger.enumerateTags(in: NSRange(location: 0, length: sentence.count), scheme: .nameTypeOrLexicalClass, options: options) { (tag, tokenRange, _, _) in
        guard let tag = tag, let range = Range(tokenRange, in: sentence) else { return }
        let token = String(sentence[range])
        if tokens[tag] != nil {
            tokens[tag]!.append(token)
        } else {
            tokens[tag] = [token]
        }
    }
    print(tokens[.adjective])
    print(tokens[.noun])
    

    打印出来的:

    可选([“黄色”,“小”,“灰色”])
    可选([“猫”,“鼠标”, “阻止”])

    编辑#2

    如果您希望能够从文本中删除某些标签,您可以编写如下扩展:

    extension NSLinguisticTagger {
        func eliminate(unwantedTags: [NSLinguisticTag], from text: String, options: NSLinguisticTagger.Options) -> String {
            string = text
            var textWithoutUnwantedTags = ""
            enumerateTags(in: NSRange(location: 0, length: text.utf16.count), scheme: .nameTypeOrLexicalClass, options: options) { (tag, tokenRange, _, _) in
                guard
                    let tag = tag,
                    !unwantedTags.contains(tag),
                    let range = Range(tokenRange, in: text)
                    else { return }
                let token = String(text[range])
                textWithoutUnwantedTags += " \(token)"
            }
    
            return textWithoutUnwantedTags.trimmingCharacters(in: .whitespaces)
        }
    }
    

    那么你可以这样使用它:

    let sentence = "The yellow cat hunts the little gray mouse around the block"
    let options: NSLinguisticTagger.Options = [.omitWhitespace, .omitPunctuation, .joinNames]
    let schemes = NSLinguisticTagger.availableTagSchemes(forLanguage: "en")
    let tagger = NSLinguisticTagger(tagSchemes: schemes, options: Int(options.rawValue))
    
    let sentenceWithoutAdjectives = tagger.eliminate(unwantedTags: [.adjective], from: sentence, options: options)
    print(sentenceWithoutAdjectives)
    

    打印出来的:

    猫在街区周围猎杀老鼠

    【讨论】:

    • 如果我想添加名词动词等会发生什么?有没有办法将文本添加到地图中并相应地拉出文本?还是我只需要为我想要拉出的所有标签都这样做?
    • 不幸的是,由于NSLingusticTagger 的基于块的实现,您不能将filtermap 直接应用于标签。但是您可以枚举一次并根据标签存储令牌。请查看已编辑的答案作为示例。
    • 对所有问题表示歉意。基本上我的目标是打印出消除与标签关联的某些标记的句子。所以假设你的句子,如果我想去掉形容词,句子会打印除黄色小灰色之外的所有内容。
    • 就是这样。如果我也想排除名词,我可以在不需要的标签参数下传递名词吗?
    • 当然,您可以过滤掉任何您想要的标签。只需将它们放在unwantedTags 数组参数中即可。您还可以一次过滤掉多个标签。例如,这会过滤掉名词和形容词:tagger.eliminate(unwantedTags: [.adjective, .noun], from: sentence, options: options)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    • 1970-01-01
    • 2016-05-10
    • 2016-12-24
    • 2015-06-26
    • 2021-03-29
    • 2010-09-06
    相关资源
    最近更新 更多