ios - NSLinguisticTagger:根据标记类型过滤掉指定的标记

标签 ios swift macos swift4 nslinguistictagger

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

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 这是我的主要目标,使用这些单词并找出形容词、动词和名词")

最佳答案

您可以简单地检查 tag 是否是 enumerateTags 闭包中的 .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)
}

打印出来:

yellow
little
gray

编辑

如果您想要不止一种标签类型的标记,您可以将标记存储在字典中,并将标记作为键:

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])

打印出:

Optional(["yellow", "little", "gray"])
Optional(["cat", "mouse", "block"])

编辑#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)

打印出:

The cat hunts the mouse around the block

关于ios - NSLinguisticTagger:根据标记类型过滤掉指定的标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53296067/

相关文章:

ios - 我在应用程序包中找到的 libswiftRemoteMirror.dylib 是什么?

ios - 我可以更改 UIPickerView 分隔符高度吗?

ios - 我们可以像 iOS 日历应用一样设置 iOS 通知标题时间吗?

swift - deinit 在 vi​​ewDidLoad 之前执行

reactjs - 'Invariant Violation : requireNativeComponent: "RNSScreen" was not found in the UIManager.' react native cli 错误

swift - 如何确定 Metal 设备 VR 支持

macos - Cocoa 文件打开事件 - 检测文件是否被打开

objective-c - NSMutableArray 每次只返回最后一个对象

ios - 是否可以将QRCode图像解码为值

ios - 在 uitableViewCell 的标签内显示随机表情符号