You can train MLWordTagger for this task.
Create a training data file (JSON) in this format.
[
{
"tokens": [
"Club game",
"at",
"Nehru Stadium Chennai",
"on",
"Saturday Morning"
],
"labels": [
"TITLE",
"NONE",
"LOCATION",
"NONE",
"TIME"
]
},
... other sample records...
]
You can train with the code below in Playground.
var trainingData = try MLDataTable(contentsOf: URL(fileURLWithPath: "/pathto..train.json"))
let model = try! MLWordTagger(trainingData: trainingData, tokenColumn: "tokens", labelColumn: "labels")
And then use this prediction method to predict each of your tokens in the sentence.
func prediction(from tokens: [MLWordTagger.Token]) throws -> [String]
This method return an array of tags for the tokens.
An alternative way of doing this is using NLTagger which is already able to detect place names, organization names but time.
import NaturalLanguage
let text = "Club game at Nehru Stadium, Chennai on Saturday Morning."
let tagger = NLTagger(tagSchemes: [.nameType])
tagger.string = text
let options: NLTagger.Options = [.omitPunctuation, .omitWhitespace, .joinNames]
let tags: [NLTag] = [.personalName, .placeName, .organizationName]
tagger.enumerateTags(in: text.startIndex..<text.endIndex, unit: .word, scheme: .nameType, options: options) { tag, tokenRange in
if let tag = tag, tags.contains(tag) {
print("\(text[tokenRange]): \(tag.rawValue)")
}
return true
}
This will return the output below, so you will only need to train model to detect the adverb of time.
Nehru Stadium: PlaceName
Chennai: OrganizationName