Reputation: 1876
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)
}
Upvotes: 1
Views: 874
Reputation: 150615
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.
}
Edited September 2023
It's been a while since I wrote this answer and things have changed. NSLinguisticTagger
was deprecated in iOS 14 in favour of the new NaturalLanguage
framework
A modern version of doing this is now:
import UIKit
import NaturalLanguage
let text = "My Name is David Bonds and I live in new york."
// Create an NSTagger instance based on NameTypes
let tagger = NLTagger(tagSchemes: [.nameType])
tagger.string = text
// Configure the tagger
let options: NLTagger.Options = [.joinNames]
let tags: [NLTag] = [.personalName]
tagger.enumerateTags(in: text.startIndex..<text.endIndex, unit: .word, scheme: .nameType, options: options) { tag, tokenRange in
// Get the most likely tag, and print it if it's a named entity.
if let tag = tag,
tags.contains(tag) {
print("\(text[tokenRange])")
}
return true
}
Upvotes: 8
Reputation: 1509
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: " ")
Upvotes: 0
Reputation: 3157
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
Upvotes: 0
Reputation: 9503
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:
Upvotes: 1
Reputation: 31645
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.
Upvotes: 2
Reputation: 433
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])
Upvotes: 3