在 Swift 中显示 NLTokenizer 的索引和标记
Displaying index, tokens of NLTokenizer in Swift
我正在使用 NLTokenizer 在 swift playground 中显示令牌列表。
如何在标记前显示索引号?
喜欢:
1.Introduction
2.to
3.Natural
4.Language
5.Processing
你可以为此使用一个变量。(这里你可以从 1 开始索引)
var index = 0
tokenizer.enumerateTokens(in: str.startIndex..<str.endIndex) { (range, token) -> Bool in
print("Index: \(index).\(str[range])")
index += 1
return true
}
输出
Index: 0. Introduction
Index: 1. to
Index: 2. Natural
Index: 3. Language
Index: 4. Processing
您可以试试下面的代码片段。
let text = "Introduction to natural language processing"
let tokenizer = NLTokenizer(unit: .word)
tokenizer.string = text
for (index, range) in tokenizer.tokens(for: text.startIndex..<text.endIndex).enumerated() {
print("\(index + 1).\(text[range])")
}
我正在使用 NLTokenizer 在 swift playground 中显示令牌列表。
如何在标记前显示索引号?
喜欢:
1.Introduction
2.to
3.Natural
4.Language
5.Processing
你可以为此使用一个变量。(这里你可以从 1 开始索引)
var index = 0
tokenizer.enumerateTokens(in: str.startIndex..<str.endIndex) { (range, token) -> Bool in
print("Index: \(index).\(str[range])")
index += 1
return true
}
输出
Index: 0. Introduction
Index: 1. to
Index: 2. Natural
Index: 3. Language
Index: 4. Processing
您可以试试下面的代码片段。
let text = "Introduction to natural language processing"
let tokenizer = NLTokenizer(unit: .word)
tokenizer.string = text
for (index, range) in tokenizer.tokens(for: text.startIndex..<text.endIndex).enumerated() {
print("\(index + 1).\(text[range])")
}