创建一个学习模型并使用
参考自苹果官方文档
新建一个data.json 其内容如下
[
{
"tokens": ["AirPods", "are", "a", "fantastic", "Apple", "product", "."],
"labels": ["PROD", "NONE", "NONE", "NONE", "ORG", "NONE", "NONE"]
},
{
"tokens": ["The", "iPhone", "takes", "stunning", "photos", "."],
"labels": ["NONE", "PROD", "NONE", "NONE", "NONE", "NONE"]
},
{
"tokens": ["Start", "building", "a", "native", "Mac", "app", "from", "your", "current", "iPad", "app", "using", "Mac", "Catalyst", "."],
"labels": ["NONE", "NONE", "NONE", "NONE", "PROD", "NONE", "NONE", "NONE", "NONE", "PROD", "NONE", "NONE", "PROD", "PROD", "NONE"]
}
]
创建学习模型
import NaturalLanguage
import CreateML
import CoreML
// 这个路径是上边创建的json文件的路径
let url = URL(fileURLWithPath:"/Users/a111/Desktop/NLP/NLP/data.json")
let data = try! MLDataTable(contentsOf: url)
let (trainingData, testingData) = data.randomSplit(by: 0.8, seed: 5)
let wordTagger = try! MLWordTagger(trainingData: trainingData,
tokenColumn: "tokens",
labelColumn: "labels")
// Training accuracy as a percentage
let trainingAccuracy = (1.0 - wordTagger.trainingMetrics.taggingError) * 100
// Validation accuracy as a percentage
let validationAccuracy = (1.0 - wordTagger.validationMetrics.taggingError) * 100
let evaluationMetrics = wordTagger.evaluation(on: testingData,
tokenColumn: "tokens",
labelColumn: "labels")
let evaluationAccuracy = (1.0 - evaluationMetrics.taggingError) * 100
let metadata = MLModelMetadata(author: "Jane Appleseed",
shortDescription: "A model trained to tag Apple products.",
version: "1.0")
// 这个路径是模型文件的导出路径
try! wordTagger.write(to: URL(fileURLWithPath: "/Users/a111/Desktop/AppleTagger.mlmodel"),
metadata: metadata)
将上面导出的模型文件拖入到项目中
使用学习模型
let text = "The iPad is my favorite Apple product."
do {
let mlModel = try AppleTagger(configuration: MLModelConfiguration()).model
let customModel = try NLModel(mlModel: mlModel)
let customTagScheme = NLTagScheme("Apple")
let tagger = NLTagger(tagSchemes: [.nameType, customTagScheme])
tagger.string = text
tagger.setModels([customModel], forTagScheme: customTagScheme)
tagger.enumerateTags(in: text.startIndex..<text.endIndex, unit: .word,
scheme: customTagScheme, options: .omitWhitespace) { tag, tokenRange in
if let tag = tag {
print("\(text[tokenRange]): \(tag.rawValue)")
}
return true
}
} catch {
print(error)
}
控制台输出
The: NONE
iPad: PROD
is: NONE
my: NONE
favorite: NONE
Apple: NONE
product: NONE
.: NONE
Program ended with exit code: 0