Displaying index, tokens of NLTokenizer in Swift

458 views Asked by At

I am using the NLTokenizer to display the list of tokens in swift playground.

enter image description here

How can i display the index numbers before the tokens ?

Like:

1.Introduction
2.to
3.Natural
4.Language
5.Processing

2

There are 2 answers

0
Subramanian Mariappan On BEST ANSWER

You can try the snippet below.

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])")
}
0
Mahendra On

You can use a variable for this.(Here you can start index with 1)

var index = 0
tokenizer.enumerateTokens(in: str.startIndex..<str.endIndex) { (range, token) -> Bool in
  print("Index: \(index).\(str[range])")
  index += 1
  return true
}

Output

Index: 0. Introduction
Index: 1. to
Index: 2. Natural
Index: 3. Language
Index: 4. Processing