- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在查看 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)
对于英文句子,结果完全符合预期。
例如对于词性标注和命名实体识别:
确定者
麻烦制造者:名词
确定者
圆:名词
钉子:名词
...
苹果公司:名词
史蒂夫·乔布斯:个人姓名
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
...
有没有人尝试过用英语以外的其他语言使用这个类,或者它只在处理英文文本时才真正有用。除了应支持的语言列表之外,我找不到任何解释语言功能的 Apple 文档。还是我做错了什么?
非常感谢任何指向我解决方案的评论。
克里德
最佳答案
我没有测试您的上述情况,但我附上了以下用于开发词性标注器的内容。它包括 setLanguage 命令和 setOthography 命令。 (后者,我还没有试验过)。
我的理解是标记器是识别语言并在需要时切换语言或者可以设置。此处使用的逻辑似乎并未完全揭示。我已经确定,如果可以的话,我的最佳做法是设置语言。在此代码中,语言存储为字符串语言。 (顺便说一句,就我而言,它是通过阅读一份更大的文档来完成的,该文档也可用。)
最后,我在本周有机会看到了这一点。我在苹果商店(美国)处理另一件事,观察到另一位顾客正在测试手机并讨论想用法语发消息。该技术展示了如果 iMessage 继续看到法语,它将如何开始理解。观察到这一点,我的想法是它确实有效,但如果可能的话,如果可以在外部进行切换会更好。
if let language = language {
// If language has a value, it is taken as a specification for the language of the text and set on the tagger.
let orthography = NSOrthography.defaultOrthography(forLanguage: language)
POStagger.setOrthography(orthography, range: range)
POStagger.setLanguage(NLLanguage(rawValue: language), range: range)
}
关于英语以外语言的 Swift NSLinguisticTagger 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50787286/
有没有办法在 .swift 文件(编译成 .swift 模块)中声明函数,如下所示: 你好.swift func hello_world() { println("hello world")
我正在尝试使用 xmpp_messenger_ios 和 XMPPFramework 在 iOS 上执行 MUC 这是加入房间的代码。 func createOrJoinRoomOnXMPP()
我想在我的应用程序上创建一个 3D Touch 快捷方式,我已经完成了有关快捷方式本身的所有操作,它显示正确,带有文本和图标。 当我运行这个快捷方式时,我的应用程序崩溃了,因为 AppDelegate
我的代码如下: let assetTag = Expression("asset_tag") let query2 = mdm.select(mdm[assetTag],os, mac, lastRe
我的 swift 代码如下所示 Family.arrayTuple:[(String,String)]? = [] Family.arrayTupleStorage:String? Family.ar
这是我的 JSON,当我读取 ord 和 uniq 数据时出现错误 let response2 : [String: Any] = ["Response":["status":"SUCCESS","
我想将 swift 扩展文件移动到 swift 包中。但是,将文件移动到 swift 包后,我遇到了这种错误: "Type 'NSAttributedString' has no member 'ma
使用CocoaPods,我们可以设置以下配置: pod 'SourceModel', :configurations => ['Debug'] 有什么方法可以用 Swift Package Manag
我正在 Xcode 中开发一个 swift 项目。我将其称为主要项目。我大部分都在工作。我在日期选择器、日期范围和日期数学方面遇到了麻烦,因此我开始了另一个名为 StarEndDate 的项目,其中只
这是 ObjectiveC 代码: CCSprite *progress = [CCSprite spriteWithImageNamed:@"progress.png"]; mProgressBar
我正在创建一个命令行工具,在 Xcode 中使用 Swift。我想使用一个类似于 grunt 的配置文件确实如此,但我希望它是像 Swift 包管理器的 package.swift 文件那样的快速代码
我假设这意味着使用系统上安装的任何 swift 运行脚本:#!/usr/bin/swift 如何指定脚本适用的解释器版本? 最佳答案 Cato可用于此: #!/usr/bin/env cato 1.2
代码说完全没问题,没有错误,但是当我去运行模拟器的时候,会出现这样的字样: (Swift.LazyMapCollection (_base:[ ] 我正在尝试创建一个显示报价的报价应用。 这是导入
是否可以在运行 Swift(例如 Perfect、Vapor、Kitura 等)的服务器上使用 RealmSwift 并使用它来存储数据? (我正在考虑尝试将其作为另一种解决方案的替代方案,例如 no
我刚开始学习编程,正在尝试完成 Swift 编程书中的实验。 它要求““编写一个函数,通过比较两个 Rank 值的原始值来比较它们。” enum Rank: Int { case Ace = 1 ca
在您将此问题标记为重复之前,我检查了 this question 它对我不起作用。 如何修复这个错误: error: SWIFT_VERSION '5.0' is unsupported, suppo
从 Xcode 9.3 开始,我在我的模型中使用“Swift.ImplicitlyUnwrappedOptional.some”包裹了我的字符串变量 我不知道这是怎么发生的,但它毁了我的应用程序! 我
这个问题在这里已经有了答案: How to include .swift file from other .swift file in an immediate mode? (2 个答案) 关闭 6
我正在使用 Swift Package Manager 创建一个应用程序,我需要知道构建项目的配置,即 Debug 或 Release。我试图避免使用 .xcodeproj 文件。请有人让我知道这是否
有一个带有函数定义的文件bar.swift: func bar() { println("bar") } 以及一个以立即模式运行的脚本foo.swift: #!/usr/bin/xcrun s
我是一名优秀的程序员,十分优秀!