【发布时间】:2018-11-20 02:06:06
【问题描述】:
我目前正在查看 Swift 的 NSLinguisticTagger。出于测试目的,我使用了来自 appcoda Introduction to Natural Language Processing 的代码。
对于英语,它按预期工作并在教程中描述。但是当我在英语以外的语言上使用 NSLinguisticTagger 时,词形还原、词性和命名实体识别不会产生有用的结果。对于命名实体识别,我可以理解这一点,但对于前两个选项,我认为至少应该有一个基本的结果。我是否错过了特定语言的设置,或者 NSLinguisticTagger 在用于英语以外的语言时仅适用于语言检测和标记化?
这是 Sai Kambampati 在他的教程中使用的代码:
import Foundation
let quote = "Here's to the crazy ones. The misfits. The rebels. The troublemakers. The round pegs in the square holes. The ones who see things differently. They're not fond of rules. And they have no respect for the status quo. You can quote them, disagree with them, glorify or vilify them. About the only thing you can't do is ignore them. Because they change things. They push the human race forward. And while some may see them as the crazy ones, we see genius. Because the people who are crazy enough to think they can change the world, are the ones who do. - Steve Jobs (Founder of Apple Inc.)"
let tagger = NSLinguisticTagger(tagSchemes:[.tokenType, .language, .lexicalClass, .nameType, .lemma], options: 0)
let options: NSLinguisticTagger.Options = [.omitPunctuation, .omitWhitespace, .joinNames]
func determineLanguage(for text: String) {
tagger.string = text
let language = tagger.dominantLanguage
print("The language is \(language!)")
}
determineLanguage(for: quote)
func tokenizeText(for text: String) {
tagger.string = text
let range = NSRange(location: 0, length: text.utf16.count)
tagger.enumerateTags(in: range, unit: .word, scheme: .tokenType, options: options) { tag, tokenRange, stop in
let word = (text as NSString).substring(with: tokenRange)
print(word)
}
}
tokenizeText(for: quote)
func partsOfSpeech(for text: String) {
tagger.string = text
let range = NSRange(location: 0, length: text.utf16.count)
tagger.enumerateTags(in: range, unit: .word, scheme: .lexicalClass, options: options) { tag, tokenRange, _ in
if let tag = tag {
let word = (text as NSString).substring(with: tokenRange)
print("\(word): \(tag.rawValue)")
}
}
}
partsOfSpeech(for: quote)
func namedEntityRecognition(for text: String) {
tagger.string = text
let range = NSRange(location: 0, length: text.utf16.count)
let tags: [NSLinguisticTag] = [.personalName, .placeName, .organizationName]
tagger.enumerateTags(in: range, unit: .word, scheme: .nameType, options: options) { tag, tokenRange, stop in
if let tag = tag, tags.contains(tag) {
let name = (text as NSString).substring(with: tokenRange)
print("\(name): \(tag.rawValue)")
}
}
}
namedEntityRecognition(for: quote)
对于英文句子,结果完全符合预期。
例如对于词性标注和命名实体识别:
The:限定符
麻烦制造者:名词
The:限定符
轮:名词
钉子:名词
...
Apple Inc.:名词
史蒂夫·乔布斯:个人姓名
Apple Inc.:组织名称
但是对于一个德语句子
let quote = "Apple führt die Hitliste der Silicon-Valley-Unternehmen an, bei denen sich Ingenieure das Wohnen in der Nähe nicht mehr leisten können. Dahinter folgen das Portal Reddit (San Francisco), der Suchriese Google (Mountain View) und die sozialen Netzwerke Twitter (San Francisco) und Facebook (Menlo Park)"
只有语言检测和标记化似乎工作正常。对于仅“OtherWord”的词性标记和命名实体识别,根本不返回任何结果:
苹果:OtherWord
führt:OtherWord
死:OtherWord
Hitliste:OtherWord
...
是否有人尝试在英语以外的其他语言中使用该类,或者它仅在处理英文文本时才真正可用。除了应支持的语言列表之外,我找不到任何解释语言功能的 Apple 文档。还是我做错了什么?
非常感谢任何将我指向解决方案的评论。
孩子
【问题讨论】:
-
似乎我在使用原生 Swift 工具进行俄语词形还原时遇到了同样的问题。它与英语完美配合。但不适用于俄语。
-
我终于通过简单地重复导入Foundation模型解决了这个问题。不知何故,它在第一次运行时没有被识别出来。重复导入后,整个事情开始工作,它仍然有效。您也可以尝试将其粘贴到新的 Playground 中。
-
不,这个代码示例对我来说不适用于正确还原俄语文本的新操场。它确实识别了一些单词。其中一些没有(65%)。我从您的评论中了解到 - 您只是重复了“import Foundation”?
-
你是对的 - 我用一些俄语文本进行了尝试,但没有产生任何有用的结果。似乎关于 NER 或 POS 的俄语语言模型不完整。
-
当我使用中文时,它对pos也不起作用
标签: swift nlp nslinguistictagger