1

I need to get persons name from a sentence.

Example : My Name is David Bonds and i live in new york. and I want to extract name David Bonds.

My Name is is definitely will be in every sentence. but after the name it can contains rest of the sentence or may be nothing. from this answer I was able to get to the point of My Name is. but it will print out rest of all the sentence. i want to make sure it will grab only next two words only.

 if let range = conversation.range(of: "My Name is") {
    let name = conversation.substring(from: range.upperBound).trimmingCharacters(in: .whitespacesAndNewlines)
    print(name)
 }
Abizern
  • 146,289
  • 39
  • 203
  • 257
Im Batman
  • 1,842
  • 1
  • 31
  • 48

6 Answers6

7

It's almost time for Swift 4, iOS 11 in which using NSLinguisticTagger is a bit easier.

So, for future reference, you can use NSLinguisticTagger to extract a name from a sentence. This doesn't depend on the name following a named token, or a two word name.

This is from an Xcode 9 Playground

import UIKit

let sentence = "My Name is David Bonds and I live in new york."

// Create the tagger's options and language scheme
let options: NSLinguisticTagger.Options = [.omitWhitespace, .omitPunctuation, .joinNames]
let schemes = NSLinguisticTagger.availableTagSchemes(forLanguage: "en")

// Create a tagger
let tagger = NSLinguisticTagger(tagSchemes: schemes, options: Int(options.rawValue))
tagger.string = sentence
let range = NSRange(location: 0, length: sentence.count)

// Enumerate the found tags. In this case print a name if it is found.
tagger.enumerateTags(in: range, unit: .word, scheme: .nameType, options: options) { (tag, tokenRange, _) in
    guard let tag = tag, tag == .personalName else { return }
    let name = (sentence as NSString).substring(with: tokenRange)
    print(name) // -> Prints "David Bonds" to the console.
}
Abizern
  • 146,289
  • 39
  • 203
  • 257
  • 1
    Damnit! You got there just before me. I was about to post a similar answer. Worth noting that this is possible in iOS 5 and above but slightly harder to use :D – Fogmeister Aug 24 '17 at 10:06
  • 3
    Do you know how many times that's happened to me? The sun shines on everyone sometimes :) – Abizern Aug 24 '17 at 10:07
3

When you have rest of the text you can separate it by " ". Then first and secont elements are first and last name

let array = text.components(separatedBy: " ")

//first name
print(array[0])

//last name
print(array[1])
2

You could implement it as follows:

let myString = "My Name is David Bonds and i live in new york."

// all words after "My Name is"
let words = String(myString.characters.dropFirst(11)).components(separatedBy: " ")

let name = words[0] + " " + words[1]

print(name) // David Bonds

As a remark, dropFirst(11) should works fine if you are pretty sure that "My Name is " should be before the name, since its number of character is 11.

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
1

Use below code

let sen = "My Name is David Bonds and i live in new york."

let arrSen = sen.components(separatedBy: "My Name is ")
print(arrSen)

let sen0 = arrSen[1]

let arrsen0 = sen0.components(separatedBy: " ")
print("\(arrsen0[0]) \(arrsen0[1])")

Output:

enter image description here

dahiya_boy
  • 9,298
  • 1
  • 30
  • 51
0

You can remove the prefix string "My Name is David " and then separate it by " ".

var sentence = "My Name is David Bonds and"
let prefix = "My Name is "

sentence.removeSubrange(sentence.range(of: prefix)!)
let array = sentence.components(separatedBy: " ")
print("Name: ", array[0],array[1])  // Name:  David Bonds
Rahul Kumar
  • 3,009
  • 2
  • 16
  • 22
0

Depending on how you want the data:

let string = "My Name is David Bonds and i live in new york."

let names = string.components(separatedBy: " ")[3...4]
let name  = names.joined(separator: " ")
XmasRights
  • 1,427
  • 13
  • 21