我正在尝试根据标记对特定标记进行过滤。运行代码时,将其作为输出。我只想检索形容词并将其输出。有没有简单的方法可以做到这一点?
<TableRow Background="WhiteSmoke">
<TableCell>
<Paragraph FontSize="24" FontWeight="Bold" Margin="0,28,0,0">
<Run FontWeight="Bold" Text="Teminat İade Şekli"/>
</Paragraph>
</TableCell>
<TableCell>
<Paragraph>
<Viewbox Height="24" Margin="0,15,0,0">
<RadioButton>Şubede Ödeme</RadioButton>
</Viewbox>
<Viewbox Height="24" Margin="0,15,0,0">
<RadioButton>Hesaba Ödeme</RadioButton>
</Viewbox>
</Paragraph>
</TableCell>
</TableRow>
tokenizeText(inputtedText:“您好,这是我的主要目标,要使用这些单词并找出形容词,动词和名词”)
答案 0 :(得分:1)
您可以简单地检查tag
闭包中的.adjective
是否为enumerateTags
类型,并且只有在以下情况下才继续:
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”, “阻止”])
EDIT#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)
哪个打印出来:
猫在方块周围搜寻鼠标