Values from MLDataType in MLDataTable



我正在尝试捕获用于MLWordTagger的MLDataTable中的MLDataValues。以下是可以作为MLDataTable在中读取的JSON文件。

[
{
"tokens": ["My","shoes", "are,", "blue" ],
"labels": ["NONE","CLOTHING","NONE","COLOR"]
},
{
"tokens": ["Her","hat","is","big,","and","red"],
"labels": ["NONE","CLOTHING","NONE","NONE","NONE","COLOR"]
}
]

以下代码从Desktop读取文件并创建MLDataTable

import NaturalLanguage
import CreateML
import Foundation
let homeURL = FileManager.default.homeDirectoryForCurrentUser
let desktopURL = homeURL.appendingPathComponent("Desktop/short.json")
let training = try MLDataTable(contentsOf: desktopURL )
print("(training.size)")

打印确认表格已创建:

(rows: 2, columns: 2)

所以有两行表。每个表行都包含一行标签和一行标记。我试图从下面的一行中获取值:

training.rows[0].forEach { (key, value)  in
print("(key) *** (value)")
let test = value.sequenceValue?.dataValue
print("test:  (test)")
if let new = test {
print("new:  (new)")
//print("(new.stringValue![1])")
} else {
print("failed")
}
}

这会产生以下输出:

labels *** DataValue([DataValue("NONE"), DataValue("CLOTHING"),    DataValue("NONE"), DataValue("COLOR")])
test:  Optional([NONE, CLOTHING, NONE, COLOR])
new:  DataValue([DataValue("NONE"), DataValue("CLOTHING"), DataValue("NONE"), DataValue("COLOR")])
tokens *** DataValue([DataValue("My"), DataValue("shoes"), DataValue("are"), DataValue("blue")])
test:  Optional([My, shoes, are, blue])
new:  DataValue([DataValue("My"), DataValue("shoes"), DataValue("are"), DataValue("blue")])

"test"似乎接近我的实际需求,因为它是一个可选数组。然而,试图通过定义"new"来打开它是行不通的。"new"现在是DataValues。

此外,以下两个都失败了:

如果让new=测试?。sequenceValue?。dataValue.stringValue{如果让new=测试?。字符串值{

此外,尝试用以下内容打开"new"会得到零结果:

print("new:  (new.stringValue?.dataValue)")

我认为测试最接近我想要做的。比如说测试[1],但我收到一条消息,它不能被子脚本化。

显然,数据值是一个序列。我可以用以下代码解决我的问题:

training.rows[0].forEach { (key, value)  in
print("(key) *** (value)")
for aValue in value.sequenceValue! {
print("aValue:  (aValue.stringValue ?? "fail")")
}

最新更新