您如何獲得單個單詞標記的詞干形式?這是我的代碼。它適用于某些單詞,但不適用于其他單詞。
let text = "people" // works
// let text = "geese" // doesn't work
let tagger = NLTagger(tagSchemes: [.lemma])
tagger.string = text
let (tag, range) = tagger.tag(at: text.startIndex, unit: .word, scheme: .lemma)
let stemForm = tag?.rawValue ?? String(text[range])
但是,如果我對整個文本進行詞形還原,它就能夠找到所有單詞的詞干形式。
let text = "This is text with plurals such as geese, people, and millennia."
let tagger = NLTagger(tagSchemes: [.lemma])
tagger.string = text
var words: [String] = []
tagger.enumerateTags(in: text.startIndex..<text.endIndex, unit: .word, scheme: .lemma, options: [.omitWhitespace, .omitPunctuation]) { tag, range in
let stemForm = tag?.rawValue ?? String(text[range])
words = [stemForm]
return true
}
// this be text with plural such as goose person and millennium
words.joined(separator: " ")
另外,是否有可能反轉這個程序并找到一個詞干的復數形式?
uj5u.com熱心網友回復:
如果在標記文本之前設定文本的語言,它可以作業:
tagger.string = text
tagger.setLanguage(.english, range: text.startIndex..<text.endIndex)
let (tag, range) = tagger.tag(at: text.startIndex, unit: .word, scheme: .lemma)
在不設定語言的情況下,標注器會猜測語言。顯然,僅“鵝”本身資訊太少,無法猜測它是英語。如果您在dominantLanguage沒有明確設定語言的情況下進行檢查,它顯然是荷蘭語。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/510289.html
標籤:迅速细绳nlp
