[英]NSLinguisticTagger: Filter out Specified Token Depending on Tag Type
我正在尝试根据标签来过滤掉特定的令牌。 运行代码时,将其作为输出。 我只想检索形容词并将其输出。 是否有捷径可寻?
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:“您好,这是我的主要目标,使用这些单词并找出形容词,动词和名词”)
您可以在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])
打印出:
可选([[黄色],“小”,“灰色”])
可选([[“ 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)
打印出:
猫在方块周围搜寻鼠标
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.