0

我遇到了用于文本的MLTextClassifier类的问题。CreateML

下面是代码片段:

import CreateML
import Foundation

let objURL = URL(fileURLWithPath: "/Users/jayprakashdubey/Desktop/headlines.json")

// 1. Load data from a JSON file
guard let newsJsonFileContent = try? MLDataTable(contentsOf: objURL) else {
    exit(0)
}

// 2. Make a train-test split
let (training, testing) = newsJsonFileContent.randomSplit(by: 0.8, seed: 5)

print("training: \(training.description)")

// 3. Create the model
if let objNewsClassifier = try? MLTextClassifier(trainingData: training, textColumn: "title", labelColumn: "category") {

   . . . 
}  else {
    print("Failed while classifying News - MLTextClassifier")
}

如果条件在上面的代码片段中总是失败。

下面是控制台日志playground

控制台日志的屏幕截图

尝试了在 Stackoverflow 上发布的所有解决方案,但没有一个有效。

注意:我使用的是 Xcode v11.3.1。

下面是 JSON 文件结构:

[
  {
    "text":"New 13-inch MacBook Pro comes with 6K monitor support, Dolby Atmos playback",
    "category":"Technology"
  },
     . . .
  {
    "text":"Apple Watch ECG detects signs of coronary ischemia missed by hospital ECG",
    "category":"Technology"
  }
]

有什么修复吗?

4

1 回答 1

1

不正确的 textColumn 和 labelColumn 值存在问题。交换了两者的值并且它起作用了。

下面是代码片段:

// 3. Create the model
if let objNewsClassifier = try? MLTextClassifier(trainingData: training, textColumn: "category", labelColumn: "title") {

   . . . 
}
于 2020-05-07T07:00:48.587 回答